3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Hooks\Util;
24 use Friendica\Core\Addon\Capabilities\ICanLoadAddons;
25 use Friendica\Core\Hooks\Capabilities\HookType;
26 use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
27 use Friendica\Core\Hooks\Exceptions\HookConfigException;
30 * Manage all hooks.config.php files
34 const STATIC_DIR = 'static';
35 const CONFIG_NAME = 'hooks';
37 /** @var ICanLoadAddons */
38 protected $addonLoader;
40 protected $hookConfig = [];
44 public function __construct(string $basePath, ICanLoadAddons $addonLoader)
46 $this->basePath = $basePath;
47 $this->addonLoader = $addonLoader;
51 * Loads all kinds of hooks and registers the corresponding instances
53 * @param ICanRegisterInstances $instanceRegister The instance register
57 public function setupHooks(ICanRegisterInstances $instanceRegister)
59 // In case it wasn't used before, reload the whole hook config
60 if (empty($this->hookConfig)) {
61 $this->reloadHookConfig();
64 foreach ($this->hookConfig as $hookType => $classList) {
66 case HookType::STRATEGY:
67 foreach ($classList as $interface => $strategy) {
68 foreach ($strategy as $dependencyName => $names) {
69 if (is_array($names)) {
70 foreach ($names as $name) {
71 $instanceRegister->registerStrategy($interface, $dependencyName, $name);
74 $instanceRegister->registerStrategy($interface, $dependencyName, $names);
79 case HookType::DECORATOR:
80 foreach ($classList as $interface => $decorators) {
81 if (is_array($decorators)) {
82 foreach ($decorators as $decorator) {
83 $instanceRegister->registerDecorator($interface, $decorator);
86 $instanceRegister->registerDecorator($interface, $decorators);
95 * Reloads all hook config files into the config cache for later usage
97 * Merges all hook configs from every addon - if present - as well
101 protected function reloadHookConfig()
103 // load core hook config
104 $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
106 if (!file_exists($configFile)) {
107 throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
110 $config = include $configFile;
112 if (!is_array($config)) {
113 throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
116 $this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));