vendor/sonata-project/block-bundle/src/DependencyInjection/Configuration.php line 71

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\BlockBundle\DependencyInjection;
  12. use Symfony\Component\Config\Definition\BaseNode;
  13. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  14. use Symfony\Component\Config\Definition\ConfigurationInterface;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. /**
  17.  * This is the class that validates and merges configuration from your app/config files.
  18.  *
  19.  * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
  20.  */
  21. final class Configuration implements ConfigurationInterface
  22. {
  23.     /**
  24.      * @var array<string, string>
  25.      */
  26.     private $defaultContainerTemplates;
  27.     /**
  28.      * NEXT_MAJOR: remove this member.
  29.      *
  30.      * @var bool
  31.      */
  32.     private $httpCacheDisabled false;
  33.     /**
  34.      * @param array<string, string> $defaultContainerTemplates
  35.      */
  36.     public function __construct(array $defaultContainerTemplates)
  37.     {
  38.         $this->defaultContainerTemplates $defaultContainerTemplates;
  39.     }
  40.     public function getConfigTreeBuilder(): TreeBuilder
  41.     {
  42.         $treeBuilder = new TreeBuilder('sonata_block');
  43.         $node $treeBuilder->getRootNode();
  44.         $node
  45.             ->fixXmlConfig('default_context')
  46.             ->fixXmlConfig('template')
  47.             ->fixXmlConfig('block')
  48.             ->fixXmlConfig('block_by_class')
  49.             ->validate()
  50.                 ->always(function (&$value) {
  51.                     foreach ($value['blocks'] as &$block) {
  52.                         if (=== \count($block['contexts'])) {
  53.                             $block['contexts'] = $value['default_contexts'];
  54.                         }
  55.                     }
  56.                     // NEXT_MAJOR: remove this block
  57.                     if (true !== $this->httpCacheDisabled) {
  58.                         @trigger_error(
  59.                             'Not setting the "sonata_block.http_cache" config option to false is deprecated since sonata-project/block-bundle 4.11 and will fail in 5.0.',
  60.                             \E_USER_DEPRECATED
  61.                         );
  62.                     } else {
  63.                         $value['http_cache'] = false;
  64.                     }
  65.                     return $value;
  66.                 })
  67.             ->end()
  68.             ->children()
  69.                 ->arrayNode('profiler')
  70.                     ->addDefaultsIfNotSet()
  71.                     ->children()
  72.                         ->scalarNode('enabled')->defaultValue('%kernel.debug%')->end()
  73.                         ->scalarNode('template')->defaultValue('@SonataBlock/Profiler/block.html.twig')->end()
  74.                     ->end()
  75.                 ->end()
  76.                 ->arrayNode('default_contexts')
  77.                     ->prototype('scalar')->end()
  78.                 ->end()
  79.                 ->scalarNode('context_manager')->defaultValue('sonata.block.context_manager.default')->end()
  80.                 // NEXT_MAJOR: deprecate option and only allow setting it to false
  81.                 ->arrayNode('http_cache')
  82.                     ->addDefaultsIfNotSet()
  83.                     ->beforeNormalization()
  84.                         ->always(function ($v) {
  85.                             if (false === $v) {
  86.                                 $this->httpCacheDisabled true;
  87.                                 return [];
  88.                             }
  89.                             return $v;
  90.                         })
  91.                     ->end()
  92.                     ->children()
  93.                         // NEXT_MAJOR: remove option
  94.                         ->scalarNode('handler')->defaultValue('sonata.block.cache.handler.default')->end()
  95.                         // NEXT_MAJOR: remove option
  96.                         ->booleanNode('listener')->defaultTrue()->end()
  97.                     ->end()
  98.                 ->end()
  99.                 ->arrayNode('templates')
  100.                     ->addDefaultsIfNotSet()
  101.                     ->children()
  102.                         ->scalarNode('block_base')->defaultNull()->end()
  103.                         ->scalarNode('block_container')->defaultNull()->end()
  104.                     ->end()
  105.                 ->end()
  106.                 ->arrayNode('container')
  107.                     ->info('block container configuration')
  108.                     ->addDefaultsIfNotSet()
  109.                     ->fixXmlConfig('type''types')
  110.                     ->fixXmlConfig('template''templates')
  111.                     ->children()
  112.                         ->arrayNode('types')
  113.                             ->info('container service ids')
  114.                             ->isRequired()
  115.                             // add default value to well know users of BlockBundle
  116.                             ->defaultValue(['sonata.block.service.container''sonata.page.block.container''sonata.dashboard.block.container''cmf.block.container''cmf.block.slideshow'])
  117.                             ->prototype('scalar')->end()
  118.                         ->end()
  119.                         ->arrayNode('templates')
  120.                             ->info('container templates')
  121.                             ->isRequired()
  122.                             ->defaultValue($this->defaultContainerTemplates)
  123.                             ->prototype('scalar')->end()
  124.                         ->end()
  125.                     ->end()
  126.                 ->end()
  127.                 ->arrayNode('blocks')
  128.                     ->info('configuration per block service')
  129.                     ->useAttributeAsKey('id')
  130.                     ->prototype('array')
  131.                         ->fixXmlConfig('context')
  132.                         ->fixXmlConfig('template')
  133.                         ->fixXmlConfig('setting')
  134.                         ->children()
  135.                             ->arrayNode('contexts')
  136.                                 ->prototype('scalar')->end()
  137.                             ->end()
  138.                             ->arrayNode('templates')
  139.                                 ->prototype('array')
  140.                                     ->children()
  141.                                         ->scalarNode('name')->cannotBeEmpty()->end()
  142.                                         ->scalarNode('template')->cannotBeEmpty()->end()
  143.                                     ->end()
  144.                                 ->end()
  145.                             ->end()
  146.                             // NEXT_MAJOR: remove cache option
  147.                             ->scalarNode('cache')
  148.                                 ->defaultValue('sonata.cache.noop')
  149.                                 ->setDeprecated(
  150.                                     ...$this->getDeprecationMessage(
  151.                                         'The "cache" option for configuring blocks is deprecated since sonata-project/block-bundle 4.11 and will be removed in 5.0.',
  152.                                         '4.11'
  153.                                     )
  154.                                 )
  155.                             ->end()
  156.                             ->arrayNode('settings')
  157.                                 ->info('default settings')
  158.                                 ->useAttributeAsKey('id')
  159.                                 ->prototype('scalar')->end()
  160.                             ->end()
  161.                             ->arrayNode('exception')
  162.                                 ->children()
  163.                                     ->scalarNode('filter')->defaultNull()->end()
  164.                                     ->scalarNode('renderer')->defaultNull()->end()
  165.                                 ->end()
  166.                             ->end()
  167.                         ->end()
  168.                     ->end()
  169.                 ->end()
  170.                 ->arrayNode('blocks_by_class')
  171.                     ->info('configuration per block class')
  172.                     ->useAttributeAsKey('class')
  173.                     ->prototype('array')
  174.                         ->fixXmlConfig('setting')
  175.                         ->children()
  176.                             ->scalarNode('cache')
  177.                                 ->defaultValue('sonata.cache.noop')
  178.                                 ->setDeprecated(
  179.                                     ...$this->getDeprecationMessage(
  180.                                         'The "cache" option for configuring blocks_by_class is deprecated since sonata-project/block-bundle 4.11 and will be removed in 5.0.',
  181.                                         '4.11'
  182.                                     )
  183.                                 )
  184.                             ->end()
  185.                             ->arrayNode('settings')
  186.                                 ->info('default settings')
  187.                                 ->useAttributeAsKey('id')
  188.                                 ->prototype('scalar')->end()
  189.                             ->end()
  190.                         ->end()
  191.                     ->end()
  192.                 ->end()
  193.                 ->arrayNode('exception')
  194.                     ->addDefaultsIfNotSet()
  195.                     ->fixXmlConfig('filter')
  196.                     ->fixXmlConfig('renderer')
  197.                     ->children()
  198.                         ->arrayNode('default')
  199.                             ->addDefaultsIfNotSet()
  200.                             ->children()
  201.                                 ->scalarNode('filter')->defaultValue('debug_only')->end()
  202.                                 ->scalarNode('renderer')->defaultValue('throw')->end()
  203.                             ->end()
  204.                         ->end()
  205.                         ->arrayNode('filters')
  206.                             ->useAttributeAsKey('id')
  207.                             ->prototype('scalar')->end()
  208.                             ->defaultValue([
  209.                                 'debug_only' => 'sonata.block.exception.filter.debug_only',
  210.                                 'ignore_block_exception' => 'sonata.block.exception.filter.ignore_block_exception',
  211.                                 'keep_all' => 'sonata.block.exception.filter.keep_all',
  212.                                 'keep_none' => 'sonata.block.exception.filter.keep_none',
  213.                             ])
  214.                         ->end()
  215.                         ->arrayNode('renderers')
  216.                             ->useAttributeAsKey('id')
  217.                             ->prototype('scalar')->end()
  218.                             ->defaultValue([
  219.                                 'inline' => 'sonata.block.exception.renderer.inline',
  220.                                 'inline_debug' => 'sonata.block.exception.renderer.inline_debug',
  221.                                 'throw' => 'sonata.block.exception.renderer.throw',
  222.                             ])
  223.                         ->end()
  224.                     ->end()
  225.                 ->end()
  226.             ->end();
  227.         return $treeBuilder;
  228.     }
  229.     /**
  230.      * @param array<string, mixed> $config
  231.      *
  232.      * @return Configuration
  233.      */
  234.     public function getConfiguration(array $configContainerBuilder $container)
  235.     {
  236.         return new self([]);
  237.     }
  238.     /**
  239.      * @return string[]
  240.      */
  241.     private function getDeprecationMessage(string $messagestring $version): array
  242.     {
  243.         // @phpstan-ignore-next-line
  244.         if (method_exists(BaseNode::class, 'getDeprecation')) {
  245.             return [
  246.                 'sonata-project/block-bundle',
  247.                 $version,
  248.                 $message,
  249.             ];
  250.         }
  251.         return [$message];
  252.     }
  253. }