Skip to content

Commit

Permalink
Support the use of application profile specific services
Browse files Browse the repository at this point in the history
Symfony has the `when@{profile-name}` syntax to load/override services
for an application profile (Symfony calls it environment).

This ensures that users are able to have that functionality if they
choose to.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Nov 10, 2024
1 parent d9928fb commit cff59c1
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ $testContainer = $builder->getTestContainer();
* `ContainerBuilder#setDumpDir()`: Configures the directory to be used to dump the cache files
* `ContainerBuilder#setParameter()`: Configures a dynamic parameter
* `ContainerBuilder#setBaseClass()`: Modifies which class should be used as base class for the container
* `ContainerBuilder#setProfileName()`: Enables the loading of application profile-specific services via the `when@{profile-name}` syntax

## Configuration file example

Expand All @@ -63,7 +64,8 @@ if (getenv('APP_PROFILE') !== 'prod') {
$builder->enableDebugging();
}

return $builder->setDumpDir($projectRoot . '/var/tmp')
return $builder->setProfileName(getenv('APP_PROFILE'))
->setDumpDir($projectRoot . '/var/tmp')
->setParameter('app.basedir', $projectRoot)
->addFile(__DIR__ . '/container.xml')
->getContainer();
Expand Down
5 changes: 5 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function addDelayedPass(
*/
public function addPackage(string $className, array $constructArguments = []): Builder;

/**
* Configures the application profile name to support profile-specific services
*/
public function setProfileName(string $profileName): Builder;

/**
* Mark the container to be used as development mode
*
Expand Down
13 changes: 13 additions & 0 deletions src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class ContainerConfiguration

private string $dumpDir;

private ?string $profileName = null;

/**
* phpcs:disable Generic.Files.LineLength
*
Expand All @@ -51,6 +53,16 @@ public function __construct(
$this->dumpDir = sys_get_temp_dir();
}

public function setProfileName(string $profileName): void
{
$this->profileName = $profileName;
}

public function profileName(): ?string
{
return $this->profileName;
}

/** @return Package[] */
public function getPackages(): array
{
Expand Down Expand Up @@ -183,6 +195,7 @@ public function getDumpFile(): string
{
return $this->dumpDir . DIRECTORY_SEPARATOR
. strtolower(str_replace('\\', '_', $this->namespace)) . DIRECTORY_SEPARATOR
. ($this->profileName !== null ? $this->profileName . DIRECTORY_SEPARATOR : '')
. self::CLASS_NAME . '.php';
}

Expand Down
7 changes: 7 additions & 0 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ public function addPackage(string $className, array $constructArguments = []): B
return $this;
}

public function setProfileName(string $profileName): Builder
{
$this->config->setProfileName($profileName);

return $this;
}

public function useDevelopmentMode(): Builder
{
return $this->enableDebugging();
Expand Down
8 changes: 6 additions & 2 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function initializeContainer(ContainerConfiguration $config): SymfonyBuil
$container = new $this->builderClass();
$container->addResource(new FileResource($this->configurationFile));

$loader = $this->getLoader($container, $config->getPaths());
$loader = $this->getLoader($container, $config->getPaths(), $config->profileName());

foreach ($config->getFiles() as $file) {
$loader->load($file);
Expand All @@ -61,5 +61,9 @@ public function initializeContainer(ContainerConfiguration $config): SymfonyBuil
}

/** @param string[] $paths */
abstract public function getLoader(SymfonyBuilder $container, array $paths): LoaderInterface;
abstract public function getLoader(

Check failure on line 64 in src/Generator.php

View workflow job for this annotation

GitHub Actions / Backwards compatibility check

Parameter profileName was added to Method getLoader() of class Lcobucci\DependencyInjection\Generator
SymfonyBuilder $container,
array $paths,
?string $profileName = null,
): LoaderInterface;
}
8 changes: 4 additions & 4 deletions src/Generators/Delegating.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
final class Delegating extends Generator
{
/** @inheritDoc */
public function getLoader(SymfonyBuilder $container, array $paths): LoaderInterface
public function getLoader(SymfonyBuilder $container, array $paths, ?string $profileName = null): LoaderInterface
{
$locator = new FileLocator($paths);

return new DelegatingLoader(
new LoaderResolver(
[
new XmlFileLoader($container, $locator),
new YamlFileLoader($container, $locator),
new PhpFileLoader($container, $locator),
new XmlFileLoader($container, $locator, $profileName),
new YamlFileLoader($container, $locator, $profileName),
new PhpFileLoader($container, $locator, $profileName),
],
),
);
Expand Down
3 changes: 2 additions & 1 deletion src/Generators/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
final class Php extends Generator
{
/** @inheritDoc */
public function getLoader(SymfonyBuilder $container, array $paths): LoaderInterface
public function getLoader(SymfonyBuilder $container, array $paths, ?string $profileName = null): LoaderInterface
{
return new PhpFileLoader(
$container,
new FileLocator($paths),
$profileName,
);
}
}
3 changes: 2 additions & 1 deletion src/Generators/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
final class Xml extends Generator
{
/** @inheritDoc */
public function getLoader(SymfonyBuilder $container, array $paths): LoaderInterface
public function getLoader(SymfonyBuilder $container, array $paths, ?string $profileName = null): LoaderInterface
{
return new XmlFileLoader(
$container,
new FileLocator($paths),
$profileName,
);
}
}
3 changes: 2 additions & 1 deletion src/Generators/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
final class Yaml extends Generator
{
/** @inheritDoc */
public function getLoader(SymfonyBuilder $container, array $paths): LoaderInterface
public function getLoader(SymfonyBuilder $container, array $paths, ?string $profileName = null): LoaderInterface
{
return new YamlFileLoader(
$container,
new FileLocator($paths),
$profileName,
);
}
}
25 changes: 25 additions & 0 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ public function getDumpFileShouldReturnTheFullPathOfDumpFile(): void
);
}

#[PHPUnit\Test]
public function getDumpFileShouldIncludeProfileNameWhenItIsSet(): void
{
$config = new ContainerConfiguration('Me\\MyApp');
$config->setProfileName('prod');

self::assertEquals(
sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'me_myapp' . DIRECTORY_SEPARATOR
. 'prod' . DIRECTORY_SEPARATOR . 'AppContainer.php',
$config->getDumpFile(),
);
}

#[PHPUnit\Test]
public function getDumpOptionsShouldReturnTheDumpingInformation(): void
{
Expand Down Expand Up @@ -357,4 +370,16 @@ public function withAddedNamespaceShouldModifyTheNamespaceOfANewInstanceOnly():
self::assertSame('Me\\MyApp\\AppContainer', $config->getClassName());
self::assertSame('Me\\MyApp\\Testing\\AppContainer', $other->getClassName());
}

#[PHPUnit\Test]
public function profileNameShouldBeConfigurable(): void
{
$config = new ContainerConfiguration('Me\\MyApp');

self::assertNull($config->profileName());

$config->setProfileName('test');

self::assertSame('test', $config->profileName());
}
}
9 changes: 9 additions & 0 deletions test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,13 @@ public function getTestContainerShouldGenerateAndReturnTheContainer(): void
self::assertCount(1, $compilerPasses);
self::assertSame($this->parameterBag, $compilerPasses[0][0]);
}

#[PHPUnit\Test]
public function profileNameShouldBeConfigurable(): void
{
$builder = new ContainerBuilder($this->config, $this->generator, $this->parameterBag);
$builder->setProfileName('testing');

self::assertSame('testing', $this->config->profileName());
}
}

0 comments on commit cff59c1

Please sign in to comment.