From 152d06ce2ab75f497655cf471493b54dc116f5d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Fri, 31 May 2024 10:56:24 +0200 Subject: [PATCH 1/3] Load services.php before configuration to allow container customizations --- .../ServiceContainerBuilder.php | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php index 798b669d..8a5ff9ab 100644 --- a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php +++ b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php @@ -24,6 +24,8 @@ final class ServiceContainerBuilder private ?SplFileInfo $configFile = null; private ?SplFileInfo $cacheFile = null; + private const DEPTRAC_INTERNAL_CONFIG_PATH = __DIR__.'/../../../config'; + public function __construct(private readonly string $workingDirectory) {} public function withConfig(?string $configFile): self @@ -79,6 +81,9 @@ public function build(string|false|null $cacheOverride, bool $clearCache): Conta $container->registerExtension(new DeptracExtension()); $container->setParameter('projectDirectory', $this->workingDirectory); + + self::loadServices($container); + if (null !== $this->configFile) { self::loadConfiguration($container, $this->configFile); } @@ -95,7 +100,10 @@ public function build(string|false|null $cacheOverride, bool $clearCache): Conta $this->withCache($cache); } - self::loadServices($container, $this->cacheFile); + if (null !== $this->cacheFile) { + self::loadCache($container, $this->cacheFile); + } + $container->compile(true); return $container; @@ -108,22 +116,26 @@ private static function registerCompilerPasses(ContainerBuilder $container): voi } /** - * @throws CacheFileException * @throws CannotLoadConfiguration */ - private static function loadServices(ContainerBuilder $container, ?SplFileInfo $cacheFile): void + private static function loadServices(ContainerBuilder $container): void { - $loader = new PhpFileLoader($container, new FileLocator([__DIR__.'/../../../config'])); + $loader = new PhpFileLoader($container, new FileLocator([self::DEPTRAC_INTERNAL_CONFIG_PATH])); try { $loader->load('services.php'); } catch (Exception $exception) { throw CannotLoadConfiguration::fromServices('services.php', $exception->getMessage()); } + } - if (!$cacheFile instanceof SplFileInfo) { - return; - } + /** + * @throws CacheFileException + * @throws CannotLoadConfiguration + */ + private static function loadCache(ContainerBuilder $container, SplFileInfo $cacheFile): void + { + $loader = new PhpFileLoader($container, new FileLocator([self::DEPTRAC_INTERNAL_CONFIG_PATH])); if (!file_exists($cacheFile->getPathname())) { $dirname = $cacheFile->getPath() ?: '.'; From 627dae0b2b7170b63de3f1a8439e0a7e901e13a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Fri, 7 Jun 2024 11:29:03 +0200 Subject: [PATCH 2/3] Load cache.php services together with other services, to provide consistent override behavior --- .../ServiceContainerBuilder.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php index 8a5ff9ab..e4c6d935 100644 --- a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php +++ b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php @@ -127,16 +127,19 @@ private static function loadServices(ContainerBuilder $container): void } catch (Exception $exception) { throw CannotLoadConfiguration::fromServices('services.php', $exception->getMessage()); } + + try { + $loader->load('cache.php'); + } catch (Exception $exception) { + throw CannotLoadConfiguration::fromCache('cache.php', $exception->getMessage()); + } } /** * @throws CacheFileException - * @throws CannotLoadConfiguration */ private static function loadCache(ContainerBuilder $container, SplFileInfo $cacheFile): void { - $loader = new PhpFileLoader($container, new FileLocator([self::DEPTRAC_INTERNAL_CONFIG_PATH])); - if (!file_exists($cacheFile->getPathname())) { $dirname = $cacheFile->getPath() ?: '.'; @@ -155,11 +158,6 @@ private static function loadCache(ContainerBuilder $container, SplFileInfo $cach } $container->setParameter('cache_file', $cacheFile->getPathname()); - try { - $loader->load('cache.php'); - } catch (Exception $exception) { - throw CannotLoadConfiguration::fromCache('cache.php', $exception->getMessage()); - } } /** From f82298717f7646ef1c1c6a1b4817d1bc4b5692ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Ols=CC=8Cavsky=CC=81?= Date: Thu, 20 Jun 2024 15:14:48 +0200 Subject: [PATCH 3/3] Revert "Load cache.php services together with other services, to provide consistent override behavior" This reverts commit 627dae0b2b7170b63de3f1a8439e0a7e901e13a5. --- .../ServiceContainerBuilder.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php index e4c6d935..8a5ff9ab 100644 --- a/src/Supportive/DependencyInjection/ServiceContainerBuilder.php +++ b/src/Supportive/DependencyInjection/ServiceContainerBuilder.php @@ -127,19 +127,16 @@ private static function loadServices(ContainerBuilder $container): void } catch (Exception $exception) { throw CannotLoadConfiguration::fromServices('services.php', $exception->getMessage()); } - - try { - $loader->load('cache.php'); - } catch (Exception $exception) { - throw CannotLoadConfiguration::fromCache('cache.php', $exception->getMessage()); - } } /** * @throws CacheFileException + * @throws CannotLoadConfiguration */ private static function loadCache(ContainerBuilder $container, SplFileInfo $cacheFile): void { + $loader = new PhpFileLoader($container, new FileLocator([self::DEPTRAC_INTERNAL_CONFIG_PATH])); + if (!file_exists($cacheFile->getPathname())) { $dirname = $cacheFile->getPath() ?: '.'; @@ -158,6 +155,11 @@ private static function loadCache(ContainerBuilder $container, SplFileInfo $cach } $container->setParameter('cache_file', $cacheFile->getPathname()); + try { + $loader->load('cache.php'); + } catch (Exception $exception) { + throw CannotLoadConfiguration::fromCache('cache.php', $exception->getMessage()); + } } /**