Created
July 28, 2016 10:18
-
-
Save ebuildy/cc0741a9eb1f60bae637537789b3723d to your computer and use it in GitHub Desktop.
Manipulate container with Reflection
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
class MyContainerPass implements CompilerPassInterface | |
{ | |
/** | |
* @see Symfony\Component\DependencyInjection\Compiler.CompilerPassInterface::process() | |
*/ | |
public function process(ContainerBuilder $container) | |
{ | |
foreach($container->getServiceIds() as $id) | |
{ | |
// Filter services by ID starting by "report." | |
if (strpos($id, 'report.') === 0) | |
{ | |
$definition = $container->getDefinition($id); | |
$r = new \ReflectionClass($definition->getClass()); | |
if ($r->hasMethod('setHolderService')) | |
{ | |
$definition->addMethodCall('setHolderService', [new Reference('report.holder')]); | |
} | |
} | |
} | |
} | |
} |
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
class ReportBundle extends Bundle | |
{ | |
public function build(ContainerBuilder $container) | |
{ | |
parent::build($container); | |
$container->addCompilerPass(new AggregatedTaggedServicesPass()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by https://gist.github.com/jmikola/785089, this gist allows to add method call if service has a specified method.