Skip to content

Instantly share code, notes, and snippets.

@ebuildy
Created July 28, 2016 10:18
Show Gist options
  • Save ebuildy/cc0741a9eb1f60bae637537789b3723d to your computer and use it in GitHub Desktop.
Save ebuildy/cc0741a9eb1f60bae637537789b3723d to your computer and use it in GitHub Desktop.
Manipulate container with Reflection
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')]);
}
}
}
}
}
class ReportBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new AggregatedTaggedServicesPass());
}
}
@ebuildy
Copy link
Author

ebuildy commented Jul 28, 2016

Inspired by https://gist.github.com/jmikola/785089, this gist allows to add method call if service has a specified method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment