From 1177724b28ecf2fc35852fe3b5b6cf98b997c00c Mon Sep 17 00:00:00 2001 From: Tilo Baller Date: Wed, 25 Sep 2024 12:42:10 +0200 Subject: [PATCH] feat: use typo3 core YamlFileLoader This way imports in sites config yaml files are processed. --- Classes/Configuration/ConfigurationFinder.php | 47 ++++++------------- .../Configuration/ConfigurationFinderTest.php | 20 ++++++++ 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/Classes/Configuration/ConfigurationFinder.php b/Classes/Configuration/ConfigurationFinder.php index 049fbae..8931c83 100644 --- a/Classes/Configuration/ConfigurationFinder.php +++ b/Classes/Configuration/ConfigurationFinder.php @@ -16,7 +16,7 @@ use Brotkrueml\MatomoWidgets\Extension; use Symfony\Component\Finder\Exception\DirectoryNotFoundException; use Symfony\Component\Finder\Finder; -use Symfony\Component\Yaml\Yaml; +use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; use TYPO3\CMS\Core\Utility\GeneralUtility; /** @@ -24,12 +24,7 @@ */ final class ConfigurationFinder { - /** - * Regex pattern for allowed characters in environment variables - * @see https://regex101.com/r/hIXvef/1 - * @see https://stackoverflow.com/questions/2821043/allowed-characters-in-linux-environment-variable-names - */ - private const ENV_VAR_REGEX = '/^%env\(([a-zA-Z_]\w*)\)%$/'; + private static YamlFileLoader $yamlFileLoader; public static function buildConfigurations(string $configPath, bool $isMatomoIntegrationAvailable): Configurations { @@ -39,7 +34,7 @@ public static function buildConfigurations(string $configPath, bool $isMatomoInt if ($realFile === false) { continue; } - $siteConfiguration = Yaml::parseFile($realFile); + $siteConfiguration = self::getYamlFileLoader()->load(GeneralUtility::fixWindowsFilePath($realFile)); $considerMatomoIntegration = $isMatomoIntegrationAvailable && ($siteConfiguration['matomoWidgetsConsiderMatomoIntegration'] ?? false); @@ -51,8 +46,7 @@ public static function buildConfigurations(string $configPath, bool $isMatomoInt $url = $siteConfiguration['matomoWidgetsUrl'] ?? ''; $idSite = (string) ($siteConfiguration['matomoWidgetsIdSite'] ?? 0); } - $url = self::resolveEnvironmentVariable($url); - $idSite = (int) self::resolveEnvironmentVariable($idSite); + $idSite = (int) $idSite; if ($url === '') { continue; } @@ -65,10 +59,9 @@ public static function buildConfigurations(string $configPath, bool $isMatomoInt } else { $pagesNotFoundTemplate = $siteConfiguration['matomoWidgetsPagesNotFoundTemplate'] ?? ''; } - $pagesNotFoundTemplate = self::resolveEnvironmentVariable($pagesNotFoundTemplate); - $siteTitle = self::resolveEnvironmentVariable($siteConfiguration['matomoWidgetsTitle'] ?? ''); - $tokenAuth = self::resolveEnvironmentVariable($siteConfiguration['matomoWidgetsTokenAuth'] ?? ''); + $siteTitle = $siteConfiguration['matomoWidgetsTitle'] ?? ''; + $tokenAuth = $siteConfiguration['matomoWidgetsTokenAuth'] ?? ''; $pathSegments = \explode('/', $file->getPath()); $identifier = \end($pathSegments); @@ -80,7 +73,7 @@ public static function buildConfigurations(string $configPath, bool $isMatomoInt $activeWidgets = GeneralUtility::trimExplode( ',', - self::resolveEnvironmentVariable($siteConfiguration['matomoWidgetsActiveWidgets'] ?? ''), + $siteConfiguration['matomoWidgetsActiveWidgets'] ?? '', true, ); $customDimensions = self::buildCustomDimensions($siteConfiguration['matomoWidgetsCustomDimensions'] ?? []); @@ -109,6 +102,7 @@ private static function getConfigurationFiles(string $configPath): array $siteFiles = \iterator_to_array( Finder::create() ->in($configPath . '/sites/*') + ->depth(0) ->name('config.yaml'), ); } catch (DirectoryNotFoundException) { @@ -128,24 +122,6 @@ private static function getConfigurationFiles(string $configPath): array return [...$siteFiles, ...$additionalFiles]; } - /** - * This method is necessary as environment variables are not resolved when configuration - * is available in controllers. - */ - private static function resolveEnvironmentVariable(string $value): string - { - if (\preg_match(self::ENV_VAR_REGEX, $value, $matches) !== 1) { - return $value; - } - - $resolvedValue = \getenv($matches[1]); - if ($resolvedValue === false) { - return ''; - } - - return $resolvedValue; - } - /** * @param list $configurations * @return CustomDimension[] @@ -166,4 +142,11 @@ private static function buildCustomDimensions(array $configurations): array return $customDimensions; } + + private static function getYamlFileLoader(): YamlFileLoader + { + self::$yamlFileLoader ??= GeneralUtility::makeInstance(YamlFileLoader::class); + + return self::$yamlFileLoader; + } } diff --git a/Tests/Unit/Configuration/ConfigurationFinderTest.php b/Tests/Unit/Configuration/ConfigurationFinderTest.php index c0291db..5ee4f05 100644 --- a/Tests/Unit/Configuration/ConfigurationFinderTest.php +++ b/Tests/Unit/Configuration/ConfigurationFinderTest.php @@ -17,8 +17,12 @@ use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use TYPO3\CMS\Core\Configuration\ConfigurationManager; +use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; use TYPO3\CMS\Core\Core\ApplicationContext; use TYPO3\CMS\Core\Core\Environment; +use TYPO3\CMS\Core\Log\Logger; +use TYPO3\CMS\Core\Utility\GeneralUtility; #[CoversClass(ConfigurationFinder::class)] #[RunTestsInSeparateProcesses] @@ -26,6 +30,22 @@ final class ConfigurationFinderTest extends TestCase { private static string $configPath; + protected function setUp(): void + { + parent::setUp(); + + $configurationManager = new ConfigurationManager(); + $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration(); + + GeneralUtility::addInstance( + YamlFileLoader::class, + GeneralUtility::makeInstance( + YamlFileLoader::class, + $this->createMock(Logger::class), + ), + ); + } + public static function setUpBeforeClass(): void { Environment::initialize(