Commit 752615a3 authored by catch's avatar catch
Browse files

Issue #3185917 by alexpott, longwave: Optimise TaggedHandlerPass

(cherry picked from commit d937a57a)
parent 76f55953
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -38,6 +38,13 @@
 */
class TaggedHandlersPass implements CompilerPassInterface {

  /**
   * Service tag information keyed by tag name.
   *
   * @var array
   */
  protected $tagCache = [];

  /**
   * {@inheritdoc}
   *
@@ -89,22 +96,25 @@ 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) {
    foreach ($container->getDefinitions() as $id => $definition) {
      foreach ($definition->getTags() as $name => $info) {
        $this->tagCache[$name][$id] = $info;
      }
    }

    foreach ($this->tagCache['service_collector'] ?? [] as $consumer_id => $tags) {
      foreach ($tags as $pass) {
        $this->processServiceCollectorPass($pass, $consumer_id, $container);
      }
    }
      if (isset($tags['service_id_collector'])) {
        foreach ($tags['service_id_collector'] as $pass) {
    foreach ($this->tagCache['service_id_collector'] ?? [] as $consumer_id => $tags) {
      foreach ($tags as $pass) {
        $this->processServiceIdCollectorPass($pass, $consumer_id, $container);
      }
    }
  }
  }

  /**
   * Processes a service collector service pass.
@@ -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;
    }