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

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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15.  * Link the block service to the Page Manager.
  16.  *
  17.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  18.  */
  19. class TweakCompilerPass implements CompilerPassInterface
  20. {
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public function process(ContainerBuilder $container)
  25.     {
  26.         $manager $container->getDefinition('sonata.block.manager');
  27.         $registry $container->getDefinition('sonata.block.menu.registry');
  28.         $parameters $container->getParameter('sonata_block.blocks');
  29.         foreach ($container->findTaggedServiceIds('sonata.block') as $id => $tags) {
  30.             $definition $container->getDefinition($id);
  31.             $definition->setPublic(true);
  32.             $arguments $definition->getArguments();
  33.             // Replace empty block id with service id
  34.             if (empty($arguments) || == strlen($arguments[0])) {
  35.                 // NEXT_MAJOR: Remove the condition when Symfony 2.8 support will be dropped.
  36.                 if (method_exists($definition'setArgument')) {
  37.                     $definition->setArgument(0$id);
  38.                 } else {
  39.                     $definition->replaceArgument(0$id);
  40.                 }
  41.             } elseif ($id != $arguments[0] && !== strpos(
  42.                 $container->getParameterBag()->resolveValue($definition->getClass()),
  43.                 'Sonata\\BlockBundle\\Block\\Service\\'
  44.             )) {
  45.                 // NEXT_MAJOR: Remove deprecation notice
  46.                 @trigger_error(
  47.                     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]),
  48.                     E_USER_DEPRECATED
  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.chain')->replaceArgument(0$services);
  66.         $this->applyContext($container);
  67.     }
  68.     /**
  69.      * Apply configurations to the context manager.
  70.      *
  71.      * @param ContainerBuilder $container
  72.      */
  73.     public function applyContext(ContainerBuilder $container)
  74.     {
  75.         $definition $container->findDefinition('sonata.block.context_manager');
  76.         foreach ($container->getParameter('sonata_block.blocks') as $service => $settings) {
  77.             if (count($settings['settings']) > 0) {
  78.                 $definition->addMethodCall('addSettingsByType', [$service$settings['settings'], true]);
  79.             }
  80.         }
  81.         foreach ($container->getParameter('sonata_block.blocks_by_class') as $class => $settings) {
  82.             if (count($settings['settings']) > 0) {
  83.                 $definition->addMethodCall('addSettingsByClass', [$class$settings['settings'], true]);
  84.             }
  85.         }
  86.     }
  87. }