vendor/sonata-project/block-bundle/src/DependencyInjection/Compiler/TweakCompilerPass.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sonata Project package.
  4.  *
  5.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sonata\BlockBundle\DependencyInjection\Compiler;
  11. use Sonata\BlockBundle\Naming\ConvertFromFqcn;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. /**
  17.  * Link the block service to the Page Manager.
  18.  *
  19.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20.  */
  21. class TweakCompilerPass implements CompilerPassInterface
  22. {
  23.     /**
  24.      * {@inheritdoc}
  25.      */
  26.     public function process(ContainerBuilder $container)
  27.     {
  28.         $manager $container->getDefinition('sonata.block.manager');
  29.         $registry $container->getDefinition('sonata.block.menu.registry');
  30.         $parameters $container->getParameter('sonata_block.blocks');
  31.         $blockTypes $container->getParameter('sonata_blocks.block_types');
  32.         foreach ($container->findTaggedServiceIds('sonata.block') as $id => $tags) {
  33.             $definition $container->getDefinition($id);
  34.             $definition->setPublic(true);
  35.             if (!$definition->isAutowired()) {
  36.                 $this->replaceBlockName($container$definition$id);
  37.             }
  38.             $blockId $id;
  39.             // Only convert class service names
  40.             if (false !== strpos($blockId'\\')) {
  41.                 $convert = (new ConvertFromFqcn());
  42.                 $blockId $convert($blockId);
  43.             }
  44.             // Skip manual defined blocks
  45.             if (!isset($blockTypes[$blockId])) {
  46.                 $contexts $this->getContextFromTags($tags);
  47.                 $blockTypes[$blockId] = [
  48.                     'context' => $contexts,
  49.                 ];
  50.             }
  51.             $manager->addMethodCall('add', [$id$id, isset($parameters[$id]) ? $parameters[$id]['contexts'] : []]);
  52.         }
  53.         foreach ($container->findTaggedServiceIds('knp_menu.menu') as $id => $tags) {
  54.             foreach ($tags as $attributes) {
  55.                 if (empty($attributes['alias'])) {
  56.                     throw new \InvalidArgumentException(sprintf('The alias is not defined in the "knp_menu.menu" tag for the service "%s"'$id));
  57.                 }
  58.                 $registry->addMethodCall('add', [$attributes['alias']]);
  59.             }
  60.         }
  61.         $services = [];
  62.         foreach ($container->findTaggedServiceIds('sonata.block.loader') as $id => $tags) {
  63.             $services[] = new Reference($id);
  64.         }
  65.         $container->getDefinition('sonata.block.loader.service')->replaceArgument(0$blockTypes);
  66.         $container->getDefinition('sonata.block.loader.chain')->replaceArgument(0$services);
  67.         $this->applyContext($container);
  68.     }
  69.     /**
  70.      * Apply configurations to the context manager.
  71.      *
  72.      * @param ContainerBuilder $container
  73.      */
  74.     public function applyContext(ContainerBuilder $container)
  75.     {
  76.         $definition $container->findDefinition('sonata.block.context_manager');
  77.         foreach ($container->getParameter('sonata_block.blocks') as $service => $settings) {
  78.             if (\count($settings['settings']) > 0) {
  79.                 $definition->addMethodCall('addSettingsByType', [$service$settings['settings'], true]);
  80.             }
  81.         }
  82.         foreach ($container->getParameter('sonata_block.blocks_by_class') as $class => $settings) {
  83.             if (\count($settings['settings']) > 0) {
  84.                 $definition->addMethodCall('addSettingsByClass', [$class$settings['settings'], true]);
  85.             }
  86.         }
  87.     }
  88.     /**
  89.      * Replaces the empty service name with the service id.
  90.      */
  91.     private function replaceBlockName(ContainerBuilder $containerDefinition $definition$id)
  92.     {
  93.         $arguments $definition->getArguments();
  94.         // Replace empty block id with service id
  95.         if (empty($arguments) || == \strlen($arguments[0])) {
  96.             // NEXT_MAJOR: Remove the if block when Symfony 2.8 support will be dropped.
  97.             if (method_exists($definition'setArgument')) {
  98.                 $definition->setArgument(0$id);
  99.                 return;
  100.             }
  101.             $definition->replaceArgument(0$id);
  102.             return;
  103.         }
  104.         if ($id != $arguments[0] && !== strpos(
  105.             $container->getParameterBag()->resolveValue($definition->getClass()),
  106.             'Sonata\\BlockBundle\\Block\\Service\\'
  107.         )) {
  108.             // NEXT_MAJOR: Remove deprecation notice
  109.             @trigger_error(
  110.                 sprintf('Using service id %s different from block id %s is deprecated since 3.3 and will be removed in 4.0.'$id$arguments[0]),
  111.                 E_USER_DEPRECATED
  112.             );
  113.         }
  114.     }
  115.     /**
  116.      * @param string[][]
  117.      *
  118.      * @return string[]
  119.      */
  120.     private function getContextFromTags(array $tags)
  121.     {
  122.         return array_filter(array_map(function (array $attribute) {
  123.             if (array_key_exists('context'$attribute) && \is_string($attribute['context'])) {
  124.                 return $attribute['context'];
  125.             }
  126.             return null;
  127.         }, $tags));
  128.     }
  129. }