Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] added a simple way to replace a service by keep…
…ing a reference to the old one
- Loading branch information
1 parent
58bed5d
commit 1eb1f4d
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/DependencyInjection/Compiler/DecoratorServicePass.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,54 @@ | ||
<?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\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Alias; | ||
|
||
/** | ||
* Overwrites a service but keeps the overridden one. | ||
* | ||
* @author Christophe Coevoet <stof@notk.org> | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
*/ | ||
class DecoratorServicePass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if (!$decorated = $definition->getDecoratedService()) { | ||
continue; | ||
} | ||
|
||
list ($decorated, $renamedId) = $decorated; | ||
if (!$renamedId) { | ||
$renamedId = $id.'.inner'; | ||
} | ||
|
||
// we create a new alias/service for the service we are replacing | ||
// to be able to reference it in the new one | ||
if ($container->hasAlias($decorated)) { | ||
$alias = $container->getAlias($decorated); | ||
$public = $alias->isPublic(); | ||
$container->setAlias($renamedId, new Alias((string) $alias, false)); | ||
} else { | ||
$definition = $container->getDefinition($decorated); | ||
$public = $definition->isPublic(); | ||
$definition->setPublic(false); | ||
$container->setDefinition($renamedId, $definition); | ||
} | ||
|
||
$container->setAlias($decorated, new Alias($id, $public)); | ||
} | ||
} | ||
} |
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
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