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() ?: '.';