Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load services.php before configuration to allow container customizations #45

Open
wants to merge 4 commits into
base: 2.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading