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/5] 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/5] 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/5] 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()); + } } /** From ac7b421d95d812669dc5ab19b5c671639f18eb86 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Fri, 15 Nov 2024 13:37:15 +0100 Subject: [PATCH 4/5] ServiceContainerBuilderTest: test service override --- .../Supportive/DependencyInjection/CustomPhpParser.php | 10 ++++++++++ .../ServiceContainerBuilderTest.php | 6 +++++- .../Supportive/DependencyInjection/config/custom.yaml | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/Supportive/DependencyInjection/CustomPhpParser.php create mode 100644 tests/Supportive/DependencyInjection/config/custom.yaml diff --git a/tests/Supportive/DependencyInjection/CustomPhpParser.php b/tests/Supportive/DependencyInjection/CustomPhpParser.php new file mode 100644 index 00000000..d27d5b09 --- /dev/null +++ b/tests/Supportive/DependencyInjection/CustomPhpParser.php @@ -0,0 +1,10 @@ +withConfig(__DIR__.'/config/custom.yaml'); $container = $builder->build(null, false); + // test service override is possible + self::assertSame(CustomPhpParser::class, $container->getDefinition(NikicPhpParser::class)->getClass()); + self::assertTrue($container->getParameter('ignore_uncovered_internal_classes')); self::assertSame( ['types' => ['class', 'function']], diff --git a/tests/Supportive/DependencyInjection/config/custom.yaml b/tests/Supportive/DependencyInjection/config/custom.yaml new file mode 100644 index 00000000..94540b54 --- /dev/null +++ b/tests/Supportive/DependencyInjection/config/custom.yaml @@ -0,0 +1,3 @@ +services: + Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser: + class: Tests\Qossmic\Deptrac\Supportive\DependencyInjection\CustomPhpParser From ac09e003290070c88e21960fe92c10857a622b55 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Fri, 15 Nov 2024 13:40:11 +0100 Subject: [PATCH 5/5] cs fix --- tests/Supportive/DependencyInjection/CustomPhpParser.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/Supportive/DependencyInjection/CustomPhpParser.php b/tests/Supportive/DependencyInjection/CustomPhpParser.php index d27d5b09..9b0f20d5 100644 --- a/tests/Supportive/DependencyInjection/CustomPhpParser.php +++ b/tests/Supportive/DependencyInjection/CustomPhpParser.php @@ -4,7 +4,4 @@ use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser; -class CustomPhpParser extends NikicPhpParser -{ - -} +class CustomPhpParser extends NikicPhpParser {}