]> git.mxchange.org Git - friendica.git/blob - src/Core/Hooks/Util/StrategiesFileManager.php
Added a lot of constants :-)
[friendica.git] / src / Core / Hooks / Util / StrategiesFileManager.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Core\Hooks\Util;
23
24 use Friendica\Core\Addon\Capability\ICanLoadAddons;
25 use Friendica\Core\Hooks\Capability\ICanRegisterStrategies;
26 use Friendica\Core\Hooks\Exceptions\HookConfigException;
27
28 /**
29  * Manage all strategies.config.php files
30  */
31 class StrategiesFileManager
32 {
33         /**
34          * The default hook-file-key of strategies
35          * -> it's an empty string to cover empty/missing config values
36          */
37         const STRATEGY_DEFAULT_KEY = '';
38         const STATIC_DIR  = 'static';
39         const CONFIG_NAME = 'strategies';
40
41         /** @var ICanLoadAddons */
42         protected $addonLoader;
43         /** @var array */
44         protected $config = [];
45         /** @var string */
46         protected $basePath;
47
48         public function __construct(string $basePath, ICanLoadAddons $addonLoader)
49         {
50                 $this->basePath    = $basePath;
51                 $this->addonLoader = $addonLoader;
52         }
53
54         /**
55          * Loads all kinds of hooks and registers the corresponding instances
56          *
57          * @param ICanRegisterStrategies $instanceRegister The instance register
58          *
59          * @return void
60          */
61         public function setupStrategies(ICanRegisterStrategies $instanceRegister)
62         {
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);
68                                         }
69                                 } else {
70                                         $instanceRegister->registerStrategy($interface, $dependencyName, $names);
71                                 }
72                         }
73                 }
74         }
75
76         /**
77          * Reloads all hook config files into the config cache for later usage
78          *
79          * Merges all hook configs from every addon - if present - as well
80          *
81          * @return void
82          */
83         public function loadConfig()
84         {
85                 // load core hook config
86                 $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
87
88                 if (!file_exists($configFile)) {
89                         throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
90                 }
91
92                 $config = include $configFile;
93
94                 if (!is_array($config)) {
95                         throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
96                 }
97
98                 $this->config = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
99         }
100 }