vendor/knplabs/knp-menu-bundle/src/DependencyInjection/Compiler/AddVotersPass.php line 43

Open in your IDE?
  1. <?php
  2. namespace Knp\Bundle\MenuBundle\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  6. use Symfony\Component\DependencyInjection\Reference;
  7. /**
  8.  * This compiler pass registers the voters in the Matcher.
  9.  *
  10.  * @author Christophe Coevoet <stof@notk.org>
  11.  *
  12.  * @internal
  13.  */
  14. class AddVotersPass implements CompilerPassInterface
  15. {
  16.     public function process(ContainerBuilder $container)
  17.     {
  18.         if (!$container->hasDefinition('knp_menu.matcher')) {
  19.             return;
  20.         }
  21.         $definition $container->getDefinition('knp_menu.matcher');
  22.         $listener $container->getDefinition('knp_menu.listener.voters');
  23.         $hasRequestAwareVoter false;
  24.         $voters = [];
  25.         foreach ($container->findTaggedServiceIds('knp_menu.voter') as $id => $tags) {
  26.             // Process only the first tag. Registering the same voter multiple time
  27.             // does not make any sense, and this allows user to overwrite the tag added
  28.             // by the autoconfiguration to change the priority (autoconfigured tags are
  29.             // always added at the end of the list).
  30.             $tag $tags[0];
  31.             $priority = isset($tag['priority']) ? (int) $tag['priority'] : 0;
  32.             $voters[$priority][] = new Reference($id);
  33.             if (isset($tag['request']) && $tag['request']) {
  34.                 @trigger_error('Using the "request" attribute of the "knp_menu.voter" tag is deprecated since version 2.2. Inject the RequestStack in the voter instead.'E_USER_DEPRECATED);
  35.                 $hasRequestAwareVoter true;
  36.                 $listener->addMethodCall('addVoter', [new Reference($id)]);
  37.             }
  38.         }
  39.         if (!$hasRequestAwareVoter) {
  40.             $container->removeDefinition('knp_menu.listener.voters');
  41.         }
  42.         if (empty($voters)) {
  43.             return;
  44.         }
  45.         krsort($voters);
  46.         $sortedVoters = \call_user_func_array('array_merge'$voters);
  47.         if (class_exists(IteratorArgument::class)) {
  48.             $definition->replaceArgument(0, new IteratorArgument($sortedVoters));
  49.         } else {
  50.             // BC layer for Symfony DI < 3.3
  51.             $definition->replaceArgument(0$sortedVoters);
  52.         }
  53.     }
  54. }