Skip to content

Commit

Permalink
Merge pull request #5 from lcobucci/implement-delayed-passes
Browse files Browse the repository at this point in the history
Implement delayed passes
  • Loading branch information
lcobucci committed Nov 16, 2017
2 parents 4d85492 + 6d8f076 commit fbffb83
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function addPass(
string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION
): Builder;

public function addDelayedPass(
string $className,
array $constructArguments = [],
string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION
): Builder;

/**
* Mark the container to be used as development mode
*/
Expand Down
13 changes: 11 additions & 2 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyBuilder;
use Symfony\Component\DependencyInjection\Dumper\DumperInterface;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
Expand Down Expand Up @@ -52,8 +53,16 @@ private function configurePassList(
SymfonyBuilder $container,
ContainerConfiguration $config
): void {
foreach ($config->getPassList() as $pass) {
$container->addCompilerPass(...$pass);
foreach ($config->getPassList() as $passConfig) {
[$pass, $type] = $passConfig;

if (! $pass instanceof CompilerPassInterface) {
[$className, $constructArguments] = $pass;

$pass = new $className(...$constructArguments);
}

$container->addCompilerPass($pass, $type);
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/Config/ContainerConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getFiles(): array
return $this->files;
}

public function addFile(string $file)
public function addFile(string $file): void
{
$this->files[] = $file;
}
Expand All @@ -65,16 +65,24 @@ public function getPassList(): array
public function addPass(
CompilerPassInterface $pass,
string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION
) {
): void {
$this->passList[] = [$pass, $type];
}

public function addDelayedPass(
string $className,
array $constructArguments,
string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION
): void {
$this->passList[] = [[$className, $constructArguments], $type];
}

public function getPaths(): array
{
return $this->paths;
}

public function addPath(string $path)
public function addPath(string $path): void
{
$this->paths[] = $path;
}
Expand All @@ -84,7 +92,7 @@ public function getBaseClass(): ?string
return $this->baseClass;
}

public function setBaseClass(string $baseClass)
public function setBaseClass(string $baseClass): void
{
$this->baseClass = $baseClass;
}
Expand All @@ -94,7 +102,7 @@ public function getDumpDir(): string
return $this->dumpDir;
}

public function setDumpDir(string $dumpDir)
public function setDumpDir(string $dumpDir): void
{
$this->dumpDir = rtrim($dumpDir, DIRECTORY_SEPARATOR);
}
Expand Down
12 changes: 11 additions & 1 deletion src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public function addPass(
return $this;
}

public function addDelayedPass(
string $className,
array $constructArguments = [],
string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION
): Builder {
$this->config->addDelayedPass($className, $constructArguments, $type);

return $this;
}

public function useDevelopmentMode(): Builder
{
$this->parameterBag->set('app.devmode', true);
Expand Down Expand Up @@ -123,7 +133,7 @@ public function getContainer(): ContainerInterface
$this->config,
new ConfigCache(
$this->config->getDumpFile(),
$this->parameterBag->get('app.devmode')
(bool) $this->parameterBag->get('app.devmode')
)
);
}
Expand Down
21 changes: 21 additions & 0 deletions test/Config/ContainerConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Lcobucci\DependencyInjection\Config;

use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

/**
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
Expand Down Expand Up @@ -100,6 +102,25 @@ public function addPassShouldAppendANewHandlerToTheList(): void
self::assertAttributeSame([[$this->pass, 'beforeOptimization']], 'passList', $config);
}

/**
* @test
*
* @covers \Lcobucci\DependencyInjection\Config\ContainerConfiguration::addDelayedPass
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration::__construct
*/
public function addDelayedPassShouldAppendANewCompilerPassToTheList(): void
{
$config = new ContainerConfiguration();
$config->addDelayedPass(ParameterBag::class, ['a' => 'b']);

self::assertAttributeSame(
[[[ParameterBag::class, ['a' => 'b']], 'beforeOptimization']],
'passList',
$config
);
}

/**
* @test
*
Expand Down
20 changes: 20 additions & 0 deletions test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ public function addPassShouldAppendANewHandlerOnTheListAndReturnSelf(): void
self::assertContains([$pass, PassConfig::TYPE_BEFORE_OPTIMIZATION], $this->config->getPassList());
}

/**
* @test
*
* @covers \Lcobucci\DependencyInjection\ContainerBuilder::addDelayedPass
*
* @uses \Lcobucci\DependencyInjection\ContainerBuilder::__construct
* @uses \Lcobucci\DependencyInjection\ContainerBuilder::setDefaultConfiguration
*
* @uses \Lcobucci\DependencyInjection\Config\ContainerConfiguration
* @uses \Lcobucci\DependencyInjection\Compiler\ParameterBag
*/
public function addDelayedPassShouldAppendANewHandlerOnTheListAndReturnSelf(): void
{
$builder = new ContainerBuilder($this->config, $this->generator, $this->parameterBag);
$pass = get_class($this->createMock(CompilerPassInterface::class));

self::assertSame($builder, $builder->addDelayedPass($pass));
self::assertContains([[$pass, []], PassConfig::TYPE_BEFORE_OPTIMIZATION], $this->config->getPassList());
}

/**
* @test
*
Expand Down

0 comments on commit fbffb83

Please sign in to comment.