]> git.mxchange.org Git - friendica.git/blob - src/Core/Hooks/Util/HookFileManager.php
Add tests for HookFileManager
[friendica.git] / src / Core / Hooks / Util / HookFileManager.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\Capabilities\ICanLoadAddons;
25 use Friendica\Core\Hooks\Capabilities\HookType;
26 use Friendica\Core\Hooks\Capabilities\ICanRegisterInstances;
27 use Friendica\Core\Hooks\Exceptions\HookConfigException;
28
29 /**
30  * Manage all hooks.config.php files
31  */
32 class HookFileManager
33 {
34         const STATIC_DIR  = 'static';
35         const CONFIG_NAME = 'hooks';
36
37         /** @var ICanLoadAddons */
38         protected $addonLoader;
39         /** @var array */
40         protected $hookConfig = [];
41         /** @var string */
42         protected $basePath;
43
44         public function __construct(string $basePath, ICanLoadAddons $addonLoader)
45         {
46                 $this->basePath    = $basePath;
47                 $this->addonLoader = $addonLoader;
48         }
49
50         /**
51          * Loads all kinds of hooks and registers the corresponding instances
52          *
53          * @param ICanRegisterInstances $instanceRegister The instance register
54          *
55          * @return void
56          */
57         public function setupHooks(ICanRegisterInstances $instanceRegister)
58         {
59                 // In case it wasn't used before, reload the whole hook config
60                 if (empty($this->hookConfig)) {
61                         $this->reloadHookConfig();
62                 }
63
64                 foreach ($this->hookConfig as $hookType => $classList) {
65                         switch ($hookType) {
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);
72                                                                 }
73                                                         } else {
74                                                                 $instanceRegister->registerStrategy($interface, $dependencyName, $names);
75                                                         }
76                                                 }
77                                         }
78                                         break;
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);
84                                                         }
85                                                 } else {
86                                                         $instanceRegister->registerDecorator($interface, $decorators);
87                                                 }
88                                         }
89                                         break;
90                         }
91                 }
92         }
93
94         /**
95          * Reloads all hook config files into the config cache for later usage
96          *
97          * Merges all hook configs from every addon - if present - as well
98          *
99          * @return void
100          */
101         protected function reloadHookConfig()
102         {
103                 // load core hook config
104                 $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
105
106                 if (!file_exists($configFile)) {
107                         throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
108                 }
109
110                 $config = include $configFile;
111
112                 if (!is_array($config)) {
113                         throw new HookConfigException(sprintf('Error loading config file %s.', $configFile));
114                 }
115
116                 $this->hookConfig = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
117         }
118 }