. * */ 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\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 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); } } } 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 exist.', $configFile)); } $config = include $configFile; if (!is_array($config)) { throw new HookConfigException(sprintf('Error loading config file %s.', $configFile)); } $this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME)); } }