Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #21327 [DI] Factorize compiler passes around new AbstractRecu…
…rsivePass (nicolas-grekas) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Factorize compiler passes around new AbstractRecursivePass | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - This PR introduces an AbstractRecursivePass that is able to visit the definition tree recursively everywhere a Definition or a Reference can be nested. All existing compiler passes that need recursivity are updated to leverage this new class. This remove a bunch of boilerplate that was previously repeated all over. It also fixes compiler passes that eg missed looking at configurators for no reason (this "fix" is a new feature really). It then applies recursivity to AutowirePass, that does not handle it today, but should. I'm happy that the net result is a loss of 153 lines :) Commits ------- 6acb80f [DI] Factorize compiler passes around new AbstractRecursivePass
- Loading branch information
Showing
11 changed files
with
329 additions
and
483 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
abstract class AbstractRecursivePass implements CompilerPassInterface | ||
{ | ||
protected $container; | ||
protected $currentId; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
|
||
try { | ||
$this->processValue($container->getDefinitions(), true); | ||
} finally { | ||
$this->container = null; | ||
} | ||
} | ||
|
||
/** | ||
* Processes a value found in a definition tree. | ||
* | ||
* @param mixed $value | ||
* @param bool $isRoot | ||
* | ||
* @return mixed The processed value | ||
*/ | ||
protected function processValue($value, $isRoot = false) | ||
{ | ||
if (is_array($value)) { | ||
foreach ($value as $k => $v) { | ||
if ($isRoot) { | ||
$this->currentId = $k; | ||
} | ||
if ($v !== $processedValue = $this->processValue($v, $isRoot)) { | ||
$value[$k] = $processedValue; | ||
} | ||
} | ||
} elseif ($value instanceof ArgumentInterface) { | ||
$value->setValues($this->processValue($value->getValues())); | ||
} elseif ($value instanceof Definition) { | ||
$value->setArguments($this->processValue($value->getArguments())); | ||
$value->setProperties($this->processValue($value->getProperties())); | ||
$value->setMethodCalls($this->processValue($value->getMethodCalls())); | ||
|
||
if ($v = $value->getFactory()) { | ||
$value->setFactory($this->processValue($v)); | ||
} | ||
if ($v = $value->getConfigurator()) { | ||
$value->setConfigurator($this->processValue($v)); | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.