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

  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.      * NEXT_MAJOR: remove this member.
  25.      */
  26.     private bool $httpCacheDisabled false;
  27.     /**
  28.      * @param array<string, string> $defaultContainerTemplates
  29.      */
  30.     public function __construct(private array $defaultContainerTemplates)
  31.     {
  32.     }
  33.     public function getConfigTreeBuilder(): TreeBuilder
  34.     {
  35.         $treeBuilder = new TreeBuilder('sonata_block');
  36.         $node $treeBuilder->getRootNode();
  37.         $node
  38.             ->fixXmlConfig('default_context')
  39.             ->fixXmlConfig('template')
  40.             ->fixXmlConfig('block')
  41.             ->fixXmlConfig('block_by_class')
  42.             ->validate()
  43.                 ->always(function (&$value) {
  44.                     foreach ($value['blocks'] as &$block) {
  45.                         if (=== \count($block['contexts'])) {
  46.                             $block['contexts'] = $value['default_contexts'];
  47.                         }
  48.                     }
  49.                     // NEXT_MAJOR: remove this block
  50.                     if (true !== $this->httpCacheDisabled) {
  51.                         @trigger_error(
  52.                             '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.',
  53.                             \E_USER_DEPRECATED
  54.                         );
  55.                     } else {
  56.                         $value['http_cache'] = false;
  57.                     }
  58.                     return $value;
  59.                 })
  60.             ->end()
  61.             ->children()
  62.                 ->arrayNode('profiler')
  63.                     ->addDefaultsIfNotSet()
  64.                     ->children()
  65.                         ->scalarNode('enabled')->defaultValue('%kernel.debug%')->end()
  66.                         ->scalarNode('template')->defaultValue('@SonataBlock/Profiler/block.html.twig')->end()
  67.                     ->end()
  68.                 ->end()
  69.                 ->arrayNode('default_contexts')
  70.                     ->prototype('scalar')->end()
  71.                 ->end()
  72.                 ->scalarNode('context_manager')->defaultValue('sonata.block.context_manager.default')->end()
  73.                 // NEXT_MAJOR: deprecate option and only allow setting it to false
  74.                 ->arrayNode('http_cache')
  75.                     ->addDefaultsIfNotSet()
  76.                     ->beforeNormalization()
  77.                         ->always(function ($v) {
  78.                             if (false === $v) {
  79.                                 $this->httpCacheDisabled true;
  80.                                 return [];
  81.                             }
  82.                             return $v;
  83.                         })
  84.                     ->end()
  85.                     ->children()
  86.                         // NEXT_MAJOR: remove option
  87.                         ->scalarNode('handler')->defaultValue('sonata.block.cache.handler.default')->end()
  88.                         // NEXT_MAJOR: remove option
  89.                         ->booleanNode('listener')->defaultTrue()->end()
  90.                     ->end()
  91.                 ->end()
  92.                 ->arrayNode('templates')
  93.                     ->addDefaultsIfNotSet()
  94.                     ->children()
  95.                         ->scalarNode('block_base')->defaultNull()->end()
  96.                         ->scalarNode('block_container')->defaultNull()->end()
  97.                     ->end()
  98.                 ->end()
  99.                 ->arrayNode('container')
  100.                     ->info('block container configuration')
  101.                     ->addDefaultsIfNotSet()
  102.                     ->fixXmlConfig('type''types')
  103.                     ->fixXmlConfig('template''templates')
  104.                     ->children()
  105.                         ->arrayNode('types')
  106.                             ->info('container service ids')
  107.                             ->isRequired()
  108.                             // add default value to well know users of BlockBundle
  109.                             ->defaultValue(['sonata.block.service.container''sonata.page.block.container''sonata.dashboard.block.container''cmf.block.container''cmf.block.slideshow'])
  110.                             ->prototype('scalar')->end()
  111.                         ->end()
  112.                         ->arrayNode('templates')
  113.                             ->info('container templates')
  114.                             ->isRequired()
  115.                             ->defaultValue($this->defaultContainerTemplates)
  116.                             ->prototype('scalar')->end()
  117.                         ->end()
  118.                     ->end()
  119.                 ->end()
  120.                 ->arrayNode('blocks')
  121.                     ->info('configuration per block service')
  122.                     ->useAttributeAsKey('id')
  123.                     ->prototype('array')
  124.                         ->fixXmlConfig('context')
  125.                         ->fixXmlConfig('template')
  126.                         ->fixXmlConfig('setting')
  127.                         ->children()
  128.                             ->arrayNode('contexts')
  129.                                 ->prototype('scalar')->end()
  130.                             ->end()
  131.                             ->arrayNode('templates')
  132.                                 ->prototype('array')
  133.                                     ->children()
  134.                                         ->scalarNode('name')->cannotBeEmpty()->end()
  135.                                         ->scalarNode('template')->cannotBeEmpty()->end()
  136.                                     ->end()
  137.                                 ->end()
  138.                             ->end()
  139.                             // NEXT_MAJOR: remove cache option
  140.                             ->scalarNode('cache')
  141.                                 ->defaultValue('sonata.cache.noop')
  142.                                 ->setDeprecated(
  143.                                     ...$this->getDeprecationMessage(
  144.                                         'The "cache" option for configuring blocks is deprecated since sonata-project/block-bundle 4.11 and will be removed in 5.0.',
  145.                                         '4.11'
  146.                                     )
  147.                                 )
  148.                             ->end()
  149.                             ->arrayNode('settings')
  150.                                 ->info('default settings')
  151.                                 ->useAttributeAsKey('id')
  152.                                 ->prototype('scalar')->end()
  153.                             ->end()
  154.                             ->arrayNode('exception')
  155.                                 ->children()
  156.                                     ->scalarNode('filter')->defaultNull()->end()
  157.                                     ->scalarNode('renderer')->defaultNull()->end()
  158.                                 ->end()
  159.                             ->end()
  160.                         ->end()
  161.                     ->end()
  162.                 ->end()
  163.                 ->arrayNode('blocks_by_class')
  164.                     ->info('configuration per block class')
  165.                     ->useAttributeAsKey('class')
  166.                     ->prototype('array')
  167.                         ->fixXmlConfig('setting')
  168.                         ->children()
  169.                             ->scalarNode('cache')
  170.                                 ->defaultValue('sonata.cache.noop')
  171.                                 ->setDeprecated(
  172.                                     ...$this->getDeprecationMessage(
  173.                                         'The "cache" option for configuring blocks_by_class is deprecated since sonata-project/block-bundle 4.11 and will be removed in 5.0.',
  174.                                         '4.11'
  175.                                     )
  176.                                 )
  177.                             ->end()
  178.                             ->arrayNode('settings')
  179.                                 ->info('default settings')
  180.                                 ->useAttributeAsKey('id')
  181.                                 ->prototype('scalar')->end()
  182.                             ->end()
  183.                         ->end()
  184.                     ->end()
  185.                 ->end()
  186.                 ->arrayNode('exception')
  187.                     ->addDefaultsIfNotSet()
  188.                     ->fixXmlConfig('filter')
  189.                     ->fixXmlConfig('renderer')
  190.                     ->children()
  191.                         ->arrayNode('default')
  192.                             ->addDefaultsIfNotSet()
  193.                             ->children()
  194.                                 ->scalarNode('filter')->defaultValue('debug_only')->end()
  195.                                 ->scalarNode('renderer')->defaultValue('throw')->end()
  196.                             ->end()
  197.                         ->end()
  198.                         ->arrayNode('filters')
  199.                             ->useAttributeAsKey('id')
  200.                             ->prototype('scalar')->end()
  201.                             ->defaultValue([
  202.                                 'debug_only' => 'sonata.block.exception.filter.debug_only',
  203.                                 'ignore_block_exception' => 'sonata.block.exception.filter.ignore_block_exception',
  204.                                 'keep_all' => 'sonata.block.exception.filter.keep_all',
  205.                                 'keep_none' => 'sonata.block.exception.filter.keep_none',
  206.                             ])
  207.                         ->end()
  208.                         ->arrayNode('renderers')
  209.                             ->useAttributeAsKey('id')
  210.                             ->prototype('scalar')->end()
  211.                             ->defaultValue([
  212.                                 'inline' => 'sonata.block.exception.renderer.inline',
  213.                                 'inline_debug' => 'sonata.block.exception.renderer.inline_debug',
  214.                                 'throw' => 'sonata.block.exception.renderer.throw',
  215.                             ])
  216.                         ->end()
  217.                     ->end()
  218.                 ->end()
  219.             ->end();
  220.         return $treeBuilder;
  221.     }
  222.     /**
  223.      * @param array<string, mixed> $config
  224.      *
  225.      * @return Configuration
  226.      */
  227.     public function getConfiguration(array $configContainerBuilder $container)
  228.     {
  229.         return new self([]);
  230.     }
  231.     /**
  232.      * @return string[]
  233.      */
  234.     private function getDeprecationMessage(string $messagestring $version): array
  235.     {
  236.         // @phpstan-ignore-next-line
  237.         if (method_exists(BaseNode::class, 'getDeprecation')) {
  238.             return [
  239.                 'sonata-project/block-bundle',
  240.                 $version,
  241.                 $message,
  242.             ];
  243.         }
  244.         return [$message];
  245.     }
  246. }