Commit 752615a3 authored by catch's avatar catch

Issue #3185917 by alexpott, longwave: Optimise TaggedHandlerPass

(cherry picked from commit d937a57a)
parent 76f55953
......@@ -38,6 +38,13 @@
*/
class TaggedHandlersPass implements CompilerPassInterface {
/**
* Service tag information keyed by tag name.
*
* @var array
*/
protected $tagCache = [];
/**
* {@inheritdoc}
*
......@@ -89,19 +96,22 @@ class TaggedHandlersPass implements CompilerPassInterface {
* If at least one tagged service is required but none are found.
*/
public function process(ContainerBuilder $container) {
// Avoid using ContainerBuilder::findTaggedServiceIds() as that we result in
// Avoid using ContainerBuilder::findTaggedServiceIds() as that results in
// additional iterations around all the service definitions.
foreach ($container->getDefinitions() as $consumer_id => $definition) {
$tags = $definition->getTags();
if (isset($tags['service_collector'])) {
foreach ($tags['service_collector'] as $pass) {
$this->processServiceCollectorPass($pass, $consumer_id, $container);
}
foreach ($container->getDefinitions() as $id => $definition) {
foreach ($definition->getTags() as $name => $info) {
$this->tagCache[$name][$id] = $info;
}
if (isset($tags['service_id_collector'])) {
foreach ($tags['service_id_collector'] as $pass) {
$this->processServiceIdCollectorPass($pass, $consumer_id, $container);
}
}
foreach ($this->tagCache['service_collector'] ?? [] as $consumer_id => $tags) {
foreach ($tags as $pass) {
$this->processServiceCollectorPass($pass, $consumer_id, $container);
}
}
foreach ($this->tagCache['service_id_collector'] ?? [] as $consumer_id => $tags) {
foreach ($tags as $pass) {
$this->processServiceIdCollectorPass($pass, $consumer_id, $container);
}
}
}
......@@ -158,7 +168,7 @@ protected function processServiceCollectorPass(array $pass, $consumer_id, Contai
// Find all tagged handlers.
$handlers = [];
$extra_arguments = [];
foreach ($container->findTaggedServiceIds($tag) as $id => $attributes) {
foreach ($this->tagCache[$tag] ?? [] as $id => $attributes) {
// Validate the interface.
$handler = $container->getDefinition($id);
if (!is_subclass_of($handler->getClass(), $interface)) {
......@@ -218,7 +228,7 @@ protected function processServiceIdCollectorPass(array $pass, $consumer_id, Cont
// Find all tagged handlers.
$handlers = [];
foreach ($container->findTaggedServiceIds($tag) as $id => $attributes) {
foreach ($this->tagCache[$tag] ?? [] as $id => $attributes) {
$handlers[$id] = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment