]> git.mxchange.org Git - friendica.git/commitdiff
Inline AddonLoader for strategies into StrategiesFileManager
authorArt4 <art4@wlabs.de>
Thu, 15 May 2025 13:12:17 +0000 (13:12 +0000)
committerArt4 <art4@wlabs.de>
Thu, 15 May 2025 13:12:17 +0000 (13:12 +0000)
src/Core/Hooks/Util/StrategiesFileManager.php

index a876dc832ce73d5bb2fa1276c70b4c0b89851267..15b624b916c8d37c1b45533659b3e05af399256a 100644 (file)
@@ -7,9 +7,15 @@
 
 namespace Friendica\Core\Hooks\Util;
 
+use Friendica\Core\Addon\AddonHelper;
 use Friendica\Core\Addon\Capability\ICanLoadAddons;
+use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException;
+use Friendica\Core\Config\Capability\IManageConfigValues;
 use Friendica\Core\Hooks\Capability\ICanRegisterStrategies;
 use Friendica\Core\Hooks\Exceptions\HookConfigException;
+use Friendica\Core\Logger\Factory\LoggerFactory;
+use Friendica\Util\Strings;
+use Psr\Log\LoggerInterface;
 
 /**
  * Manage all strategies.config.php files
@@ -24,17 +30,15 @@ class StrategiesFileManager
        const STATIC_DIR           = 'static';
        const CONFIG_NAME          = 'strategies';
 
-       /** @var ICanLoadAddons */
-       protected $addonLoader;
-       /** @var array */
-       protected $config = [];
+       private AddonHelper $addonHelper;
+       protected array $config = [];
        /** @var string */
        protected $basePath;
 
-       public function __construct(string $basePath, ICanLoadAddons $addonLoader)
+       public function __construct(string $basePath, AddonHelper $addonHelper)
        {
                $this->basePath    = $basePath;
-               $this->addonLoader = $addonLoader;
+               $this->addonHelper = $addonHelper;
        }
 
        /**
@@ -69,7 +73,7 @@ class StrategiesFileManager
        public function loadConfig()
        {
                // load core hook config
-               $configFile = $this->basePath . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
+               $configFile = $this->addonHelper->getAddonPath() . '/' . static::STATIC_DIR . '/' . static::CONFIG_NAME . '.config.php';
 
                if (!file_exists($configFile)) {
                        throw new HookConfigException(sprintf('config file %s does not exist.', $configFile));
@@ -84,6 +88,57 @@ class StrategiesFileManager
                /**
                 * @deprecated 2025.02 Providing strategies via addons is deprecated and will be removed in 5 months.
                 */
-               $this->config = array_merge_recursive($config, $this->addonLoader->getActiveAddonConfig(static::CONFIG_NAME));
+               $this->config = array_merge_recursive($config, $this->getActiveAddonConfig(static::CONFIG_NAME));
+       }
+
+       /**
+        * @deprecated 2025.02 Providing strategies via addons is deprecated and will be removed in 5 months.
+        */
+       private function getActiveAddonConfig(string $configName): array
+       {
+               $this->addonHelper->loadAddons();
+
+               $addons       = $this->addonHelper->getEnabledAddons();
+               $returnConfig = [];
+
+               foreach ($addons as $addon) {
+                       $addonName = Strings::sanitizeFilePathItem(trim($addon));
+
+                       $configFile = $this->addonHelper->getAddonPath() . '/' . $addonName . '/' . static::STATIC_DIR . '/' . $configName . '.config.php';
+
+                       if (!file_exists($configFile)) {
+                               // Addon unmodified, skipping
+                               continue;
+                       }
+
+                       $config = include $configFile;
+
+                       if (!is_array($config)) {
+                               throw new AddonInvalidConfigFileException('Error loading config file ' . $configFile);
+                       }
+
+                       if ($configName === 'strategies') {
+                               foreach ($config as $classname => $rule) {
+                                       if ($classname === LoggerInterface::class) {
+                                               @trigger_error(sprintf(
+                                                       'Providing a strategy for `%s` is deprecated since 2025.02 and will stop working in 5 months, please provide an implementation for `%s` via `dependency.config.php` and remove the `strategies.config.php` file in the `%s` addon.',
+                                                       $classname,
+                                                       LoggerFactory::class,
+                                                       $addonName,
+                                               ), \E_USER_DEPRECATED);
+                                       } else {
+                                               @trigger_error(sprintf(
+                                                       'Providing strategies for `%s` via addons is deprecated since 2025.02 and will stop working in 5 months, please stop using this and remove the `strategies.config.php` file in the `%s` addon.',
+                                                       $classname,
+                                                       $addonName,
+                                               ), \E_USER_DEPRECATED);
+                                       }
+                               }
+                       }
+
+                       $returnConfig = array_merge_recursive($returnConfig, $config);
+               }
+
+               return $returnConfig;
        }
 }