Skip to content

[DependencyInjection] Sort the CompilerPass by priority #18022

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.2.0
-----

* allowed to prioritize compiler passes by introducing a third argument to `PassConfig::addPass()`, to `Compiler::addPass` and to `ContainerBuilder::addCompilerPass()`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing () on addPass


3.0.0
-----

Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/DependencyInjection/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,20 @@ public function getLoggingFormatter()
/**
* Adds a pass to the PassConfig.
*
* @param CompilerPassInterface $pass A compiler pass
* @param string $type The type of the pass
* @param CompilerPassInterface $pass A compiler pass
* @param string $type The type of the pass
* @param int $priority Used to sort the passes
*/
public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/**, $priority = 0*/)
{
$this->passConfig->addPass($pass, $type);
// For BC
if (func_num_args() >= 3) {
$priority = func_get_arg(2);
} else {
$priority = 0;
}

$this->passConfig->addPass($pass, $type, $priority);
}

/**
Expand Down
102 changes: 67 additions & 35 deletions src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct()
{
$this->mergePass = new MergeExtensionConfigurationPass();

$this->optimizationPasses = array(
$this->optimizationPasses = array(array(
new ExtensionCompilerPass(),
new ResolveDefinitionTemplatesPass(),
new DecoratorServicePass(),
Expand All @@ -51,9 +51,9 @@ public function __construct()
new AnalyzeServiceReferencesPass(true),
new CheckCircularReferencesPass(),
new CheckReferenceValidityPass(),
);
));

$this->removingPasses = array(
$this->removingPasses = array(array(
new RemovePrivateAliasesPass(),
new ReplaceAliasByActualDefinitionPass(),
new RemoveAbstractDefinitionsPass(),
Expand All @@ -64,98 +64,111 @@ public function __construct()
new RemoveUnusedDefinitionsPass(),
)),
new CheckExceptionOnInvalidReferenceBehaviorPass(),
);
));
}

/**
* Returns all passes in order to be processed.
*
* @return array An array of all passes to process
* @return CompilerPassInterface[]
*/
public function getPasses()
{
return array_merge(
array($this->mergePass),
$this->beforeOptimizationPasses,
$this->optimizationPasses,
$this->beforeRemovingPasses,
$this->removingPasses,
$this->afterRemovingPasses
$this->getBeforeOptimizationPasses(),
$this->getOptimizationPasses(),
$this->getBeforeRemovingPasses(),
$this->getRemovingPasses(),
$this->getAfterRemovingPasses()
);
}

/**
* Adds a pass.
*
* @param CompilerPassInterface $pass A Compiler pass
* @param string $type The pass type
* @param CompilerPassInterface $pass A Compiler pass
* @param string $type The pass type
* @param int $priority Used to sort the passes
*
* @throws InvalidArgumentException when a pass type doesn't exist
*/
public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION)
public function addPass(CompilerPassInterface $pass, $type = self::TYPE_BEFORE_OPTIMIZATION/*, $priority = 0*/)
{
// For BC
if (func_num_args() >= 3) {
$priority = func_get_arg(2);
} else {
$priority = 0;
}

$property = $type.'Passes';
if (!isset($this->$property)) {
throw new InvalidArgumentException(sprintf('Invalid type "%s".', $type));
}

$this->{$property}[] = $pass;
$passes = &$this->$property;

if (!isset($passes[$priority])) {
$passes[$priority] = array();
}
$passes[$priority][] = $pass;
}

/**
* Gets all passes for the AfterRemoving pass.
*
* @return array An array of passes
* @return CompilerPassInterface[]
*/
public function getAfterRemovingPasses()
{
return $this->afterRemovingPasses;
return $this->sortPasses($this->afterRemovingPasses);
}

/**
* Gets all passes for the BeforeOptimization pass.
*
* @return array An array of passes
* @return CompilerPassInterface[]
*/
public function getBeforeOptimizationPasses()
{
return $this->beforeOptimizationPasses;
return $this->sortPasses($this->beforeOptimizationPasses);
}

/**
* Gets all passes for the BeforeRemoving pass.
*
* @return array An array of passes
* @return CompilerPassInterface[]
*/
public function getBeforeRemovingPasses()
{
return $this->beforeRemovingPasses;
return $this->sortPasses($this->beforeRemovingPasses);
}

/**
* Gets all passes for the Optimization pass.
*
* @return array An array of passes
* @return CompilerPassInterface[]
*/
public function getOptimizationPasses()
{
return $this->optimizationPasses;
return $this->sortPasses($this->optimizationPasses);
}

/**
* Gets all passes for the Removing pass.
*
* @return array An array of passes
* @return CompilerPassInterface[]
*/
public function getRemovingPasses()
{
return $this->removingPasses;
return $this->sortPasses($this->removingPasses);
}

/**
* Gets all passes for the Merge pass.
*
* @return array An array of passes
* @return CompilerPassInterface
*/
public function getMergePass()
{
Expand All @@ -175,50 +188,69 @@ public function setMergePass(CompilerPassInterface $pass)
/**
* Sets the AfterRemoving passes.
*
* @param array $passes An array of passes
* @param CompilerPassInterface[] $passes
*/
public function setAfterRemovingPasses(array $passes)
{
$this->afterRemovingPasses = $passes;
$this->afterRemovingPasses = array($passes);
}

/**
* Sets the BeforeOptimization passes.
*
* @param array $passes An array of passes
* @param CompilerPassInterface[] $passes
*/
public function setBeforeOptimizationPasses(array $passes)
{
$this->beforeOptimizationPasses = $passes;
$this->beforeOptimizationPasses = array($passes);
}

/**
* Sets the BeforeRemoving passes.
*
* @param array $passes An array of passes
* @param CompilerPassInterface[] $passes
*/
public function setBeforeRemovingPasses(array $passes)
{
$this->beforeRemovingPasses = $passes;
$this->beforeRemovingPasses = array($passes);
}

/**
* Sets the Optimization passes.
*
* @param array $passes An array of passes
* @param CompilerPassInterface[] $passes
*/
public function setOptimizationPasses(array $passes)
{
$this->optimizationPasses = $passes;
$this->optimizationPasses = array($passes);
}

/**
* Sets the Removing passes.
*
* @param array $passes An array of passes
* @param CompilerPassInterface[] $passes
*/
public function setRemovingPasses(array $passes)
{
$this->removingPasses = $passes;
$this->removingPasses = array($passes);
}

/**
* Sort passes by priority.
*
* @param array $passes CompilerPassInterface instances with their priority as key.
*
* @return CompilerPassInterface[]
*/
private function sortPasses(array $passes)
{
if (0 === count($passes)) {
return array();
}

krsort($passes);

// Flatten the array
return call_user_func_array('array_merge', $passes);
}
}
16 changes: 12 additions & 4 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,22 @@ public function loadFromExtension($extension, array $values = array())
/**
* Adds a compiler pass.
*
* @param CompilerPassInterface $pass A compiler pass
* @param string $type The type of compiler pass
* @param CompilerPassInterface $pass A compiler pass
* @param string $type The type of compiler pass
* @param int $priority Used to sort the passes
*
* @return ContainerBuilder The current instance
*/
public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION/**, $priority = 0*/)
{
$this->getCompiler()->addPass($pass, $type);
// For BC
if (func_num_args() >= 3) {
$priority = func_get_arg(2);
} else {
$priority = 0;
}

$this->getCompiler()->addPass($pass, $type, $priority);

$this->addObjectResource($pass);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Compiler;

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

/**
* @author Guilhem N <egetick@gmail.com>
*/
class PassConfigTest extends \PHPUnit_Framework_TestCase
{
public function testPassOrdering()
{
$config = new PassConfig();

$pass1 = $this->getMock(CompilerPassInterface::class);
$config->addPass($pass1, PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);

$pass2 = $this->getMock(CompilerPassInterface::class);
$config->addPass($pass2, PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);

$this->assertSame(array($pass2, $pass1), $config->getBeforeOptimizationPasses());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bridge\PhpUnit\ErrorAssert;
use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -284,10 +285,14 @@ public function testAddGetCompilerPass()
{
$builder = new ContainerBuilder();
$builder->setResourceTracking(false);
$builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
$builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));

$this->assertCount(count($builder->getCompiler()->getPassConfig()->getPasses()) - 1, $builderCompilerPasses);
$defaultPasses = $builder->getCompiler()->getPassConfig()->getPasses();
$builder->addCompilerPass($pass1 = $this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'), PassConfig::TYPE_BEFORE_OPTIMIZATION, -5);
$builder->addCompilerPass($pass2 = $this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);

$passes = $builder->getCompiler()->getPassConfig()->getPasses();
$this->assertCount(count($passes) - 2, $defaultPasses);
// Pass 1 is executed later
$this->assertTrue(array_search($pass1, $passes, true) > array_search($pass2, $passes, true));
}

public function testCreateService()
Expand Down