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\Capability\ICanLoadAddons;
25 use Friendica\Core\Hooks\Capability\ICanRegisterStrategies;
26 use Friendica\Core\Hooks\Exceptions\HookConfigException;
29 * Manage all strategies.config.php files
31 class StrategiesFileManager
34 * The default hook-file-key of strategies
35 * -> it's an empty string to cover empty/missing config values
37 const STRATEGY_DEFAULT_KEY = '';
38 const STATIC_DIR = 'static';
39 const CONFIG_NAME = 'strategies';
41 /** @var ICanLoadAddons */
42 protected $addonLoader;
44 protected $config = [];
48 public function __construct(string $basePath, ICanLoadAddons $addonLoader)
50 $this->basePath = $basePath;
51 $this->addonLoader = $addonLoader;
55 * Loads all kinds of hooks and registers the corresponding instances
57 * @param ICanRegisterStrategies $instanceRegister The instance register
61 public function setupStrategies(ICanRegisterStrategies $instanceRegister)
63 foreach ($this->config as $interface => $strategy) {
64 foreach ($strategy as $dependencyName => $names) {
65 if (is_array($names)) {
66 foreach ($names as $name) {
67 $instanceRegister->registerStrategy($interface, $dependencyName, $name);
70 $instanceRegister->registerStrategy($interface, $dependencyName, $names);
77 * Reloads all hook config files into the config cache for later usage
79 * Merges all hook configs from every addon - if present - as well
83 public function loadConfig()
85 // load core hook config
86 $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
88 if (!file_exists($configFile)) {
89 throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
92 $config = include $configFile;
94 if (!is_array($config)) {
95 throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
98 $this->config = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));