Skip to content

Commit

Permalink
draft: use TYPO3's YamlFileLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
brotkrueml committed Oct 8, 2024
1 parent 53a7583 commit acc3183
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
45 changes: 10 additions & 35 deletions Classes/Configuration/ConfigurationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,39 @@
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;

/**
* @internal
*/
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());

Check failure on line 30 in Classes/Configuration/ConfigurationFinder.php

View workflow job for this annotation

GitHub Actions / Code Quality

Class TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader does not have a constructor and must be instantiated without any parameters.

This comment has been minimized.

Copy link
@tbal

tbal Oct 9, 2024

YamlFileLoader has no constructor, the logger needs so be set using the setter when instantiated manually:

        $yamlFileLoader = new YamlFileLoader();
        $yamlFileLoader->setLogger(new NullLogger());

Without that change i constantly get the this exception (e.g. when clearing cache through typo3 cli):

Uncaught TYPO3 Exception Call to a member function load() on null
thrown in file /var/www/html/vendor/brotkrueml/typo3-matomo-widgets/Classes/Configuration/ConfigurationFinder.php
in line 39

This comment has been minimized.

Copy link
@brotkrueml

brotkrueml Dec 6, 2024

Author Owner

@tbal Well, YamlFileLoader with v12.4 has no constructor, whereas YamlFileLoader in v13.4 has ;-)

The LoggerAwareInterface was removed in favor of injecting the logger via constructor.

$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;
}
Expand All @@ -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);
Expand All @@ -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'] ?? []);
Expand Down Expand Up @@ -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<array{scope: string, idDimension: int|string, title?: string, description?: string}> $configurations
* @return CustomDimension[]
Expand Down
18 changes: 16 additions & 2 deletions Tests/Unit/Configuration/ConfigurationFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -71,6 +84,7 @@ public static function tearDownAfterClass(): void

protected function tearDown(): void
{
unset($GLOBALS['TYPO3_CONF_VARS']['SYS']['yamlLoader']['placeholderProcessors']);
self::tearDownAfterClass();
}

Expand Down

0 comments on commit acc3183

Please sign in to comment.