mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-12 01:54:26 +02:00
Introduce dynamic hook loading
- Dynamically load addon files - Dynamically load hooks - Rewrite Logger-logic to use new hook logic (Monolog is working again)
This commit is contained in:
parent
ff092833ae
commit
14b76e48f0
39 changed files with 1163 additions and 469 deletions
39
src/Core/Hooks/Capabilities/HookType.php
Normal file
39
src/Core/Hooks/Capabilities/HookType.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Capabilities;
|
||||
|
||||
interface HookType
|
||||
{
|
||||
/**
|
||||
* Defines the key for the list of strategy-hooks.
|
||||
*
|
||||
* @see https://refactoring.guru/design-patterns/strategy
|
||||
*/
|
||||
const STRATEGY = 'strategy';
|
||||
/**
|
||||
* Defines the key for the list of decorator-hooks.
|
||||
*
|
||||
* @see https://refactoring.guru/design-patterns/decorator
|
||||
*/
|
||||
const DECORATOR = 'decorator';
|
||||
const EVENT = 'event';
|
||||
}
|
59
src/Core/Hooks/Capabilities/ICanCreateInstances.php
Normal file
59
src/Core/Hooks/Capabilities/ICanCreateInstances.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Capabilities;
|
||||
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
|
||||
/**
|
||||
* creates special instance and decorator treatments for given classes
|
||||
*/
|
||||
interface ICanCreateInstances
|
||||
{
|
||||
/**
|
||||
* Returns a new instance of a given class for the corresponding name
|
||||
*
|
||||
* The instance will be build based on the registered strategy and the (unique) name
|
||||
*
|
||||
* In case, there are registered decorators for this class as well, all decorators of the list will be wrapped
|
||||
* around the instance before returning it
|
||||
*
|
||||
* @param string $class The fully-qualified name of the given class or interface which will get returned
|
||||
* @param string $name An arbitrary identifier to find a concrete instance strategy.
|
||||
* @param array $arguments Additional arguments, which can be passed to the constructor of "$class" at runtime
|
||||
*
|
||||
* @return object The concrete instance of the type "$class"
|
||||
*/
|
||||
public function createWithName(string $class, string $name, array $arguments = []): object;
|
||||
|
||||
/**
|
||||
* Returns a new instance of a given class
|
||||
*
|
||||
* In case, there are registered decorators for this class as well, all decorators of the list will be wrapped
|
||||
* around the instance before returning it
|
||||
*
|
||||
* @param string $class The fully-qualified name of the given class or interface which will get returned
|
||||
* @param array $arguments Additional arguments, which can be passed to the constructor of "$class" at runtime
|
||||
*
|
||||
* @return object The concrete instance of the type "$class"
|
||||
*/
|
||||
public function create(string $class, array $arguments = []): object;
|
||||
}
|
|
@ -21,61 +21,43 @@
|
|||
|
||||
namespace Friendica\Core\Hooks\Capabilities;
|
||||
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
||||
|
||||
/**
|
||||
* Managing special instance and decorator treatments for classes
|
||||
* Register strategies and decorator/treatment handling for given classes
|
||||
*/
|
||||
interface ICanManageInstances
|
||||
interface ICanRegisterInstances
|
||||
{
|
||||
/**
|
||||
* Register a class(strategy) for a given interface with a unique name.
|
||||
*
|
||||
* @see https://refactoring.guru/design-patterns/strategy
|
||||
*
|
||||
* @param string $interface The interface, which the given class implements
|
||||
* @param string $name An arbitrary identifier for the given class, which will be used for factories, dependency injections etc.
|
||||
* @param string $class The fully-qualified given class name
|
||||
* @param ?array $arguments Additional arguments, which can be passed to the constructor
|
||||
* @param string $interface The interface, which the given class implements
|
||||
* @param string $class The fully-qualified given class name
|
||||
* A placeholder for dependencies is possible as well
|
||||
* @param ?string $name An arbitrary identifier for the given class, which will be used for factories, dependency injections etc.
|
||||
*
|
||||
* @return $this This interface for chain-calls
|
||||
*
|
||||
* @throws HookRegisterArgumentException in case the given class for the interface isn't valid or already set
|
||||
*/
|
||||
public function registerStrategy(string $interface, string $name, string $class, array $arguments = null): self;
|
||||
public function registerStrategy(string $interface, string $class, ?string $name = null): self;
|
||||
|
||||
/**
|
||||
* Register a new decorator for a given class or interface
|
||||
* @see https://refactoring.guru/design-patterns/decorator
|
||||
*
|
||||
* @see https://refactoring.guru/design-patterns/decorator
|
||||
*
|
||||
* @note Decorator attach new behaviors to classes without changing them or without letting them know about it.
|
||||
*
|
||||
* @param string $class The fully-qualified class or interface name, which gets decorated by a class
|
||||
* @param string $decoratorClass The fully-qualified name of the class which mimics the given class or interface and adds new functionality
|
||||
* @param array $arguments Additional arguments, which can be passed to the constructor of "decoratorClass"
|
||||
* A placeholder for dependencies is possible as well
|
||||
*
|
||||
* @return $this This interface for chain-calls
|
||||
*
|
||||
* @throws HookRegisterArgumentException in case the given class for the class or interface isn't valid
|
||||
*/
|
||||
public function registerDecorator(string $class, string $decoratorClass, array $arguments = []): self;
|
||||
|
||||
/**
|
||||
* Returns a new instance of a given class for the corresponding name
|
||||
*
|
||||
* The instance will be build based on the registered strategy and the (unique) name
|
||||
*
|
||||
* In case, there are registered decorators for this class as well, all decorators of the list will be wrapped
|
||||
* around the instance before returning it
|
||||
*
|
||||
* @param string $class The fully-qualified name of the given class or interface which will get returned
|
||||
* @param string $name An arbitrary identifier to find a concrete instance strategy.
|
||||
* @param array $arguments Additional arguments, which can be passed to the constructor of "$class" at runtime
|
||||
*
|
||||
* @return object The concrete instance of the type "$class"
|
||||
*
|
||||
* @throws HookInstanceException In case the class cannot get created
|
||||
*/
|
||||
public function getInstance(string $class, string $name, array $arguments = []): object;
|
||||
public function registerDecorator(string $class, string $decoratorClass): self;
|
||||
}
|
|
@ -19,11 +19,12 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Capabilities;
|
||||
namespace Friendica\Core\Hooks\Exceptions;
|
||||
|
||||
/**
|
||||
* All classes, implementing this interface are valid Strategies for Hook calls
|
||||
*/
|
||||
interface IAmAStrategy
|
||||
class HookConfigException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", \Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
111
src/Core/Hooks/Model/DiceInstanceManager.php
Normal file
111
src/Core/Hooks/Model/DiceInstanceManager.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Model;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
||||
use Friendica\Core\Hooks\Util\HookFileManager;
|
||||
|
||||
/**
|
||||
* This class represents an instance register, which uses Dice for creation
|
||||
*
|
||||
* @see Dice
|
||||
*/
|
||||
class DiceInstanceManager implements ICanCreateInstances, ICanRegisterInstances
|
||||
{
|
||||
protected $instance = [];
|
||||
protected $decorator = [];
|
||||
|
||||
/** @var Dice */
|
||||
protected $dice;
|
||||
|
||||
public function __construct(Dice $dice, HookFileManager $hookFileManager)
|
||||
{
|
||||
$this->dice = $dice;
|
||||
$hookFileManager->setupHooks($this);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterInstances
|
||||
{
|
||||
if (!empty($this->instance[$interface][$name])) {
|
||||
throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface));
|
||||
}
|
||||
|
||||
$this->instance[$interface][$name] = $class;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function registerDecorator(string $class, string $decoratorClass): ICanRegisterInstances
|
||||
{
|
||||
$this->decorator[$class][] = $decoratorClass;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function create(string $class, array $arguments = []): object
|
||||
{
|
||||
$instanceClassName = $class;
|
||||
$instanceRule = $this->dice->getRule($instanceClassName) ?? [];
|
||||
|
||||
$instanceRule = array_replace_recursive($instanceRule, [
|
||||
'constructParams' => $arguments,
|
||||
]);
|
||||
|
||||
$this->dice = $this->dice->addRule($instanceClassName, $instanceRule);
|
||||
|
||||
foreach ($this->decorator[$class] ?? [] as $decorator) {
|
||||
$dependencyRule = $this->dice->getRule($decorator);
|
||||
for ($i = 0; $i < count($dependencyRule['call'] ?? []); $i++) {
|
||||
$dependencyRule['call'][$i][1] = [[Dice::INSTANCE => $instanceClassName]];
|
||||
}
|
||||
$dependencyRule['constructParams'] = $arguments;
|
||||
$dependencyRule['substitutions'] = [
|
||||
$class => $instanceClassName,
|
||||
];
|
||||
|
||||
$this->dice = $this->dice->addRule($decorator, $dependencyRule);
|
||||
|
||||
$instanceClassName = $decorator;
|
||||
}
|
||||
|
||||
return $this->dice->create($instanceClassName);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function createWithName(string $class, string $name, array $arguments = []): object
|
||||
{
|
||||
if (empty($this->instance[$class][$name])) {
|
||||
throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $name, $class));
|
||||
}
|
||||
|
||||
$instanceClassName = $this->instance[$class][$name];
|
||||
|
||||
return $this->create($instanceClassName, $arguments);
|
||||
}
|
||||
}
|
|
@ -1,104 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Model;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\Core\Hooks\Capabilities\IAmAStrategy;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanManageInstances;
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
class InstanceManager implements ICanManageInstances
|
||||
{
|
||||
protected $instance = [];
|
||||
protected $instanceArguments = [];
|
||||
protected $decorator = [];
|
||||
|
||||
/** @var Dice */
|
||||
protected $dice;
|
||||
|
||||
public function __construct(Dice $dice)
|
||||
{
|
||||
$this->dice = $dice;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function registerStrategy(string $interface, string $name, string $class, array $arguments = null): ICanManageInstances
|
||||
{
|
||||
if (!is_a($class, $interface, true)) {
|
||||
throw new HookRegisterArgumentException(sprintf('%s is not a valid class for the interface %s', $class, $interface));
|
||||
}
|
||||
|
||||
if (!is_a($class, IAmAStrategy::class, true)) {
|
||||
throw new HookRegisterArgumentException(sprintf('%s does not inherit from the marker interface %s', $class, IAmAStrategy::class));
|
||||
}
|
||||
|
||||
if (!empty($this->instance[$interface][$name])) {
|
||||
throw new HookRegisterArgumentException(sprintf('A class with the name %s is already set for the interface %s', $name, $interface));
|
||||
}
|
||||
|
||||
$this->instance[$interface][$name] = $class;
|
||||
$this->instanceArguments[$interface][$name] = $arguments;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function registerDecorator(string $class, string $decoratorClass, array $arguments = []): ICanManageInstances
|
||||
{
|
||||
if (!is_a($decoratorClass, $class, true)) {
|
||||
throw new HookRegisterArgumentException(sprintf('%s is not a valid substitution for the given class or interface %s', $decoratorClass, $class));
|
||||
}
|
||||
|
||||
$this->decorator[$class][] = [
|
||||
'class' => $decoratorClass,
|
||||
'arguments' => $arguments,
|
||||
];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function getInstance(string $class, string $name, array $arguments = []): object
|
||||
{
|
||||
if (empty($this->instance[$class][$name])) {
|
||||
throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $name, $class));
|
||||
}
|
||||
|
||||
$instance = $this->dice->create($this->instance[$class][$name], array_merge($this->instanceArguments[$class][$name] ?? [], $arguments));
|
||||
|
||||
foreach ($this->decorator[$class] ?? [] as $decorator) {
|
||||
$this->dice = $this->dice->addRule($class, [
|
||||
'instanceOf' => $decorator['class'],
|
||||
'constructParams' => empty($decorator['arguments']) ? null : $decorator['arguments'],
|
||||
/// @todo maybe support call structures for hooks as well in a later stage - could make factory calls easier
|
||||
'call' => null,
|
||||
'substitutions' => [$class => $instance],
|
||||
]);
|
||||
|
||||
$instance = $this->dice->create($class);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
}
|
118
src/Core/Hooks/Util/HookFileManager.php
Normal file
118
src/Core/Hooks/Util/HookFileManager.php
Normal file
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2023, the Friendica project
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Hooks\Util;
|
||||
|
||||
use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
|
||||
use Friendica\Core\Hooks\Capabilities\HookType;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
|
||||
use Friendica\Core\Hooks\Exceptions\HookConfigException;
|
||||
|
||||
/**
|
||||
* Manage all hooks.config.php files
|
||||
*/
|
||||
class HookFileManager
|
||||
{
|
||||
const STATIC_DIR = 'static';
|
||||
const CONFIG_NAME = 'hooks';
|
||||
|
||||
/** @var ICanLoadAddons */
|
||||
protected $addonLoader;
|
||||
/** @var array */
|
||||
protected $hookConfig = [];
|
||||
/** @var string */
|
||||
protected $basePath;
|
||||
|
||||
public function __construct(string $basePath, ICanLoadAddons $addonLoader)
|
||||
{
|
||||
$this->basePath = $basePath;
|
||||
$this->addonLoader = $addonLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all kinds of hooks and registers the corresponding instances
|
||||
*
|
||||
* @param ICanRegisterInstances $instanceRegister The instance register
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setupHooks(ICanRegisterInstances $instanceRegister)
|
||||
{
|
||||
// In case it wasn't used before, reload the whole hook config
|
||||
if (empty($this->hookConfig)) {
|
||||
$this->reloadHookConfig();
|
||||
}
|
||||
|
||||
foreach ($this->hookConfig as $hookType => $classList) {
|
||||
switch ($hookType) {
|
||||
case HookType::STRATEGY:
|
||||
foreach ($classList as $interface => $strategy) {
|
||||
foreach ($strategy as $dependencyName => $names) {
|
||||
if (is_array($names)) {
|
||||
foreach ($names as $name) {
|
||||
$instanceRegister->registerStrategy($interface, $dependencyName, $name);
|
||||
}
|
||||
} else {
|
||||
$instanceRegister->registerStrategy($interface, $dependencyName, $names);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case HookType::DECORATOR:
|
||||
foreach ($classList as $interface => $decorators) {
|
||||
if (is_array($decorators)) {
|
||||
foreach ($decorators as $decorator) {
|
||||
$instanceRegister->registerDecorator($interface, $decorator);
|
||||
}
|
||||
} else {
|
||||
$instanceRegister->registerDecorator($interface, $decorators);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads all hook config files into the config cache for later usage
|
||||
*
|
||||
* Merges all hook configs from every addon - if present - as well
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function reloadHookConfig()
|
||||
{
|
||||
// load core hook config
|
||||
$configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
|
||||
|
||||
if (!file_exists($configFile)) {
|
||||
throw new HookConfigException(sprintf('config file %s does not exit.', $configFile));
|
||||
}
|
||||
|
||||
$config = include $configFile;
|
||||
|
||||
if (!is_array($config)) {
|
||||
throw new HookConfigException('Error loading config file ' . $configFile);
|
||||
}
|
||||
|
||||
$this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue