-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Enable replacing of services #7040
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ServiceReplacementsPass.php
This file contains hidden or 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,62 @@ | ||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Alias; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
/** | ||
* Overwrites a service with an alias to a new service. | ||
* | ||
* @author Dariusz Górecki <darek.krk@gmail.com> | ||
* @author Christophe Coevoet <stof@notk.org> | ||
*/ | ||
class ServiceReplacementsPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$replacements = array(); | ||
|
||
foreach ($container->findTaggedServiceIds('framework.service_replacer') as $serviceId => $tag) { | ||
if (!isset($tag[0]['replaces'])) { | ||
continue; | ||
} | ||
|
||
$oldId = $tag[0]['replaces']; | ||
$newId = isset($tag[0]['renameTo']) | ||
? $tag[0]['renameTo'] | ||
: $oldId.'.orig'; | ||
|
||
if ($container->hasAlias($oldId)) { | ||
// Service we're overwriting is an alias. | ||
// Register a private alias for this service to inject it as the parent | ||
$container->setAlias($newId, new Alias($oldId, false)); | ||
} else { | ||
// Service we're overwriting is a definition. | ||
// Register it again as a private service to inject it as the parent. | ||
$definition = $container->getDefinition($oldId); | ||
$definition->setPublic(false); | ||
// Replacing definition should be properly tagged | ||
// so it's processed instead of old one. | ||
// Old service looses their tags. | ||
$definition->clearTags(); | ||
$container->setDefinition($newId, $definition); | ||
} | ||
|
||
$container->setAlias($oldId, $serviceId); | ||
} | ||
} | ||
} |
This file contains hidden or 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
176 changes: 176 additions & 0 deletions
176
...Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/ServiceReplacementsPassTest.php
This file contains hidden or 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,176 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Alias; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ServiceReplacementsPass; | ||
|
||
class ServiceReplacementsPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testReplacemntOfAlias() | ||
{ | ||
$service = array( | ||
'my_replacemnt.service' => array(0 => array('replaces' => 'service.alias')), | ||
); | ||
|
||
$alias = new Alias('service.alias', false); | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
|
||
$container->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('framework.service_replacer') | ||
->will($this->returnValue($service)); | ||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->with('service.alias') | ||
->will($this->returnValue(true)); | ||
$container->expects($this->at(2)) | ||
->method('setAlias') | ||
->with( | ||
'service.alias.orig', $alias | ||
); | ||
$container->expects($this->at(3)) | ||
->method('setAlias') | ||
->with( | ||
'service.alias', 'my_replacemnt.service' | ||
); | ||
|
||
$pass = new ServiceReplacementsPass(); | ||
$pass->process($container); | ||
} | ||
|
||
public function testReplacemntOfAliasWithRename() | ||
{ | ||
$service = array( | ||
'my_replacemnt.service' => array(0 => array( | ||
'replaces' => 'service.alias', | ||
'renameTo' => 'old.service.alias', | ||
)), | ||
); | ||
|
||
$alias = new Alias('service.alias', false); | ||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
|
||
$container->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('framework.service_replacer') | ||
->will($this->returnValue($service)); | ||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->with('service.alias') | ||
->will($this->returnValue(true)); | ||
$container->expects($this->at(2)) | ||
->method('setAlias') | ||
->with( | ||
'old.service.alias', $alias | ||
); | ||
$container->expects($this->at(3)) | ||
->method('setAlias') | ||
->with( | ||
'service.alias', 'my_replacemnt.service' | ||
); | ||
|
||
$pass = new ServiceReplacementsPass(); | ||
$pass->process($container); | ||
} | ||
|
||
public function testReplacemntOfService() | ||
{ | ||
$service = array( | ||
'my_replacemnt.service' => array(0 => array('replaces' => 'service.to.replace')), | ||
); | ||
|
||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition', array( | ||
'clearTags', 'setPublic' | ||
), array('service.to.replace')); | ||
|
||
$container->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('framework.service_replacer') | ||
->will($this->returnValue($service)); | ||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->with('service.to.replace') | ||
->will($this->returnValue(false)); | ||
$container->expects($this->once()) | ||
->method('getDefinition') | ||
->with('service.to.replace') | ||
->will($this->returnValue($definition)); | ||
$container->expects($this->once()) | ||
->method('setDefinition') | ||
->with( | ||
'service.to.replace.orig', $definition | ||
); | ||
$container->expects($this->once()) | ||
->method('setAlias') | ||
->with( | ||
'service.to.replace', 'my_replacemnt.service' | ||
); | ||
|
||
$definition->expects($this->once()) | ||
->method('setPublic') | ||
->with(false); | ||
$definition->expects($this->once()) | ||
->method('clearTags'); | ||
|
||
$pass = new ServiceReplacementsPass(); | ||
$pass->process($container); | ||
} | ||
|
||
public function testReplacemntOfServiceWithRename() | ||
{ | ||
$service = array( | ||
'my_replacemnt.service' => array(0 => array( | ||
'replaces' => 'service.to.replace', | ||
'renameTo' => 'old.service.to.replace' | ||
)), | ||
); | ||
|
||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
$definition = $this->getMock('Symfony\Component\DependencyInjection\Definition', array( | ||
'clearTags', 'setPublic' | ||
), array('service.to.replace')); | ||
|
||
$container->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('framework.service_replacer') | ||
->will($this->returnValue($service)); | ||
$container->expects($this->once()) | ||
->method('hasAlias') | ||
->with('service.to.replace') | ||
->will($this->returnValue(false)); | ||
$container->expects($this->once()) | ||
->method('getDefinition') | ||
->with('service.to.replace') | ||
->will($this->returnValue($definition)); | ||
$container->expects($this->once()) | ||
->method('setDefinition') | ||
->with( | ||
'old.service.to.replace', $definition | ||
); | ||
$container->expects($this->once()) | ||
->method('setAlias') | ||
->with( | ||
'service.to.replace', 'my_replacemnt.service' | ||
); | ||
|
||
$definition->expects($this->once()) | ||
->method('setPublic') | ||
->with(false); | ||
$definition->expects($this->once()) | ||
->method('clearTags'); | ||
|
||
$pass = new ServiceReplacementsPass(); | ||
$pass->process($container); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to throw exception here?