Skip to content

Commit

Permalink
Load default services before configuration (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Nov 16, 2024
1 parent cc702b2 commit d88d325
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/Supportive/DependencyInjection/ServiceContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
Expand All @@ -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() ?: '.';
Expand Down
7 changes: 7 additions & 0 deletions tests/Supportive/DependencyInjection/CustomPhpParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tests\Qossmic\Deptrac\Supportive\DependencyInjection;

use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser;

class CustomPhpParser extends NikicPhpParser {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace Tests\Qossmic\Deptrac\Supportive\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser;
use Qossmic\Deptrac\Supportive\DependencyInjection\ServiceContainerBuilder;

final class ServiceContainerBuilderTest extends TestCase
{
public function testBuildsContainerWithDefaultParameters(): void
{
$builder = new ServiceContainerBuilder(__DIR__);
$builder = (new ServiceContainerBuilder(__DIR__))->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']],
Expand Down
3 changes: 3 additions & 0 deletions tests/Supportive/DependencyInjection/config/custom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
Qossmic\Deptrac\Core\Ast\Parser\NikicPhpParser\NikicPhpParser:
class: Tests\Qossmic\Deptrac\Supportive\DependencyInjection\CustomPhpParser

0 comments on commit d88d325

Please sign in to comment.