From acc31835098ee96adfb210d7ab07a651c16df657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20M=C3=BCller?= <2566282+brotkrueml@users.noreply.github.com> Date: Tue, 8 Oct 2024 19:20:03 +0200 Subject: [PATCH] draft: use TYPO3's YamlFileLoader --- Classes/Configuration/ConfigurationFinder.php | 45 +++++-------------- .../Configuration/ConfigurationFinderTest.php | 18 +++++++- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/Classes/Configuration/ConfigurationFinder.php b/Classes/Configuration/ConfigurationFinder.php index 049fbae..c56f856 100644 --- a/Classes/Configuration/ConfigurationFinder.php +++ b/Classes/Configuration/ConfigurationFinder.php @@ -14,9 +14,10 @@ use Brotkrueml\MatomoWidgets\Domain\Entity\CustomDimension; use Brotkrueml\MatomoWidgets\Domain\Validation\CustomDimensionConfigurationValidator; use Brotkrueml\MatomoWidgets\Extension; +use Psr\Log\NullLogger; 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,35 +25,28 @@ */ 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*)\)%$/'; - public static function buildConfigurations(string $configPath, bool $isMatomoIntegrationAvailable): Configurations { + $yamlFileLoader = new YamlFileLoader(new NullLogger()); $configurationsArray = []; foreach (self::getConfigurationFiles($configPath) as $file) { $realFile = $file->getRealPath(); if ($realFile === false) { continue; } - $siteConfiguration = Yaml::parseFile($realFile); + $realFile = GeneralUtility::fixWindowsFilePath($realFile); + $siteConfiguration = $yamlFileLoader->load($realFile); $considerMatomoIntegration = $isMatomoIntegrationAvailable && ($siteConfiguration['matomoWidgetsConsiderMatomoIntegration'] ?? false); if ($considerMatomoIntegration) { $url = $siteConfiguration['matomoIntegrationUrl'] ?? ''; - $idSite = (string) ($siteConfiguration['matomoIntegrationSiteId'] ?? 0); + $idSite = (int) ($siteConfiguration['matomoIntegrationSiteId'] ?? 0); } else { $url = $siteConfiguration['matomoWidgetsUrl'] ?? ''; - $idSite = (string) ($siteConfiguration['matomoWidgetsIdSite'] ?? 0); + $idSite = (int) ($siteConfiguration['matomoWidgetsIdSite'] ?? 0); } - $url = self::resolveEnvironmentVariable($url); - $idSite = (int) self::resolveEnvironmentVariable($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'] ?? []); @@ -128,24 +121,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[] diff --git a/Tests/Unit/Configuration/ConfigurationFinderTest.php b/Tests/Unit/Configuration/ConfigurationFinderTest.php index c0291db..846e18f 100644 --- a/Tests/Unit/Configuration/ConfigurationFinderTest.php +++ b/Tests/Unit/Configuration/ConfigurationFinderTest.php @@ -14,18 +14,31 @@ use Brotkrueml\MatomoWidgets\Configuration\Configuration; use Brotkrueml\MatomoWidgets\Configuration\ConfigurationFinder; use PHPUnit\Framework\Attributes\CoversClass; -use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; +use TYPO3\CMS\Core\Configuration\Processor\Placeholder\EnvVariableProcessor; +use TYPO3\CMS\Core\Configuration\Processor\Placeholder\ValueFromReferenceArrayProcessor; use TYPO3\CMS\Core\Core\ApplicationContext; use TYPO3\CMS\Core\Core\Environment; #[CoversClass(ConfigurationFinder::class)] -#[RunTestsInSeparateProcesses] final class ConfigurationFinderTest extends TestCase { private static string $configPath; + protected function setUp(): void + { + // Configuration from EXT:core/Configuration/DefaultConfiguration.php + $GLOBALS['TYPO3_CONF_VARS']['SYS']['yamlLoader']['placeholderProcessors'] = [ + EnvVariableProcessor::class => [], + ValueFromReferenceArrayProcessor::class => [ + 'after' => [ + EnvVariableProcessor::class, + ], + ], + ]; + } + public static function setUpBeforeClass(): void { Environment::initialize( @@ -71,6 +84,7 @@ public static function tearDownAfterClass(): void protected function tearDown(): void { + unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['yamlLoader']['placeholderProcessors']); self::tearDownAfterClass(); }