Adhere feedback
- rename hooks.config.php to strategies.config.php - change all corresponding classes and tests
This commit is contained in:
parent
e659a03140
commit
cba656383e
12 changed files with 72 additions and 121 deletions
|
@ -32,10 +32,10 @@ interface ICanCreateInstances
|
|||
* The instance will be build based on the registered strategy and the (unique) name
|
||||
*
|
||||
* @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 string $strategy 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 create(string $class, string $name, array $arguments = []): object;
|
||||
public function create(string $class, string $strategy, array $arguments = []): object;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
|||
/**
|
||||
* Register strategies for given classes
|
||||
*/
|
||||
interface ICanRegisterInstances
|
||||
interface ICanRegisterStrategies
|
||||
{
|
||||
/**
|
||||
* Register a class(strategy) for a given interface with a unique name.
|
||||
|
@ -36,7 +36,7 @@ interface ICanRegisterInstances
|
|||
* @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.
|
||||
* @param ?string $name An arbitrary identifier for the given strategy, which will be used for factories, dependency injections etc.
|
||||
*
|
||||
* @return $this This interface for chain-calls
|
||||
*
|
|
@ -23,31 +23,31 @@ namespace Friendica\Core\Hooks\Model;
|
|||
|
||||
use Dice\Dice;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
||||
use Friendica\Core\Hooks\Util\HookFileManager;
|
||||
use Friendica\Core\Hooks\Util\StrategiesFileManager;
|
||||
|
||||
/**
|
||||
* This class represents an instance register, which uses Dice for creation
|
||||
*
|
||||
* @see Dice
|
||||
*/
|
||||
class DiceInstanceManager implements ICanCreateInstances, ICanRegisterInstances
|
||||
class DiceInstanceManager implements ICanCreateInstances, ICanRegisterStrategies
|
||||
{
|
||||
protected $instance = [];
|
||||
|
||||
/** @var Dice */
|
||||
protected $dice;
|
||||
|
||||
public function __construct(Dice $dice, HookFileManager $hookFileManager)
|
||||
public function __construct(Dice $dice, StrategiesFileManager $strategiesFileManager)
|
||||
{
|
||||
$this->dice = $dice;
|
||||
$hookFileManager->setupHooks($this);
|
||||
$strategiesFileManager->setupStrategies($this);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterInstances
|
||||
public function registerStrategy(string $interface, string $class, ?string $name = null): ICanRegisterStrategies
|
||||
{
|
||||
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));
|
||||
|
@ -59,12 +59,12 @@ class DiceInstanceManager implements ICanCreateInstances, ICanRegisterInstances
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public function create(string $class, string $name, array $arguments = []): object
|
||||
public function create(string $class, string $strategy, 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));
|
||||
if (empty($this->instance[$class][$strategy])) {
|
||||
throw new HookInstanceException(sprintf('The class with the name %s isn\'t registered for the class or interface %s', $strategy, $class));
|
||||
}
|
||||
|
||||
return $this->dice->create($this->instance[$class][$name], $arguments);
|
||||
return $this->dice->create($this->instance[$class][$strategy], $arguments);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,22 +22,21 @@
|
|||
namespace Friendica\Core\Hooks\Util;
|
||||
|
||||
use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
|
||||
use Friendica\Core\Hooks\Capabilities\BehavioralHookType;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
|
||||
use Friendica\Core\Hooks\Capabilities\ICanRegisterStrategies;
|
||||
use Friendica\Core\Hooks\Exceptions\HookConfigException;
|
||||
|
||||
/**
|
||||
* Manage all hooks.config.php files
|
||||
* Manage all strategies.config.php files
|
||||
*/
|
||||
class HookFileManager
|
||||
class StrategiesFileManager
|
||||
{
|
||||
const STATIC_DIR = 'static';
|
||||
const CONFIG_NAME = 'hooks';
|
||||
const CONFIG_NAME = 'strategies';
|
||||
|
||||
/** @var ICanLoadAddons */
|
||||
protected $addonLoader;
|
||||
/** @var array */
|
||||
protected $hookConfig = [];
|
||||
protected $config = [];
|
||||
/** @var string */
|
||||
protected $basePath;
|
||||
|
||||
|
@ -50,32 +49,21 @@ class HookFileManager
|
|||
/**
|
||||
* Loads all kinds of hooks and registers the corresponding instances
|
||||
*
|
||||
* @param ICanRegisterInstances $instanceRegister The instance register
|
||||
* @param ICanRegisterStrategies $instanceRegister The instance register
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setupHooks(ICanRegisterInstances $instanceRegister)
|
||||
public function setupStrategies(ICanRegisterStrategies $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 BehavioralHookType::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);
|
||||
}
|
||||
}
|
||||
foreach ($this->config as $interface => $strategy) {
|
||||
foreach ($strategy as $dependencyName => $names) {
|
||||
if (is_array($names)) {
|
||||
foreach ($names as $name) {
|
||||
$instanceRegister->registerStrategy($interface, $dependencyName, $name);
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
$instanceRegister->registerStrategy($interface, $dependencyName, $names);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +75,7 @@ class HookFileManager
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function reloadHookConfig()
|
||||
public function loadConfig()
|
||||
{
|
||||
// load core hook config
|
||||
$configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
|
||||
|
@ -102,6 +90,6 @@ class HookFileManager
|
|||
throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
|
||||
}
|
||||
|
||||
$this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
|
||||
$this->config = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue