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

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