vendor/sonata-project/core-bundle/src/CoreBundle/DependencyInjection/SonataCoreExtension.php line 87

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\CoreBundle\DependencyInjection;
  12. use JMS\Serializer\Handler\SubscribingHandlerInterface;
  13. use Sonata\CoreBundle\Form\FormHelper;
  14. use Sonata\CoreBundle\Form\Type\TranslatableChoiceType;
  15. use Sonata\Doctrine\Bridge\Symfony\Bundle\SonataDoctrineBundle;
  16. use Sonata\Form\Serializer\BaseSerializerHandler;
  17. use Sonata\Form\Type\BooleanType;
  18. use Sonata\Form\Type\CollectionType;
  19. use Sonata\Form\Type\DateRangeType;
  20. use Sonata\Form\Type\DateTimeRangeType;
  21. use Sonata\Form\Type\EqualType;
  22. use Sonata\Form\Type\ImmutableArrayType;
  23. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
  24. use Symfony\Component\Config\Definition\Processor;
  25. use Symfony\Component\Config\FileLocator;
  26. use Symfony\Component\DependencyInjection\ContainerBuilder;
  27. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  28. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  29. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  30. /**
  31.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  32.  *
  33.  * @deprecated since sonata-project/core-bundle 3.19, to be removed in 4.0.
  34.  */
  35. class SonataCoreExtension extends Extension implements PrependExtensionInterface
  36. {
  37.     public function prepend(ContainerBuilder $container)
  38.     {
  39.         $configs $container->getExtensionConfig('sonata_admin');
  40.         foreach ($configs as $config) {
  41.             if (isset($config['options']['form_type'])) {
  42.                 $container->prependExtensionConfig(
  43.                     $this->getAlias(),
  44.                     ['form_type' => $config['options']['form_type']]
  45.                 );
  46.             }
  47.         }
  48.     }
  49.     public function load(array $configsContainerBuilder $container)
  50.     {
  51.         $processor = new Processor();
  52.         $configuration = new Configuration();
  53.         // NEXT_MAJOR : remove this if block
  54.         if (!interface_exists(SubscribingHandlerInterface::class)) {
  55.             /* Let's check for config values before the configuration is processed,
  56.              * otherwise we won't be able to tell,
  57.              * since there is a default value for this option. */
  58.             foreach ($configs as $config) {
  59.                 if (isset($config['serializer'])) {
  60.                     @trigger_error(<<<'EOT'
  61. Setting the sonata_core -> serializer -> formats option
  62. without having the jms/serializer library installed is deprecated since 3.1,
  63. and will not be supported in 4.0,
  64. because the configuration option will not be added in that case.
  65. EOT
  66.                     , E_USER_DEPRECATED);
  67.                 }
  68.             }
  69.         }
  70.         $config $processor->processConfiguration($configuration$configs);
  71.         $bundles $container->getParameter('kernel.bundles');
  72.         if (!isset($bundles['SonataDoctrineBundle'])) {
  73.             // NEXT_MAJOR remove the alias, throw an exception
  74.             @trigger_error(sprintf(
  75.                 'Not registering bundle "%s" is deprecated since 3.12.0, registering it will be mandatory in 4.0',
  76.                 SonataDoctrineBundle::class
  77.             ), E_USER_DEPRECATED);
  78.             $container->setAlias(
  79.                 'sonata.doctrine.model.adapter.chain',
  80.                 'sonata.core.model.adapter.chain'
  81.             );
  82.         }
  83.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  84.         $loader->load('commands.xml');
  85.         $loader->load('core.xml');
  86.         $loader->load('form_types.xml');
  87.         $loader->load('model_adapter.xml');
  88.         $loader->load('validator.xml');
  89.         $loader->load('twig.xml');
  90.         if (!isset($bundles['SonataFormBundle'])) {
  91.             $loader->load('form/date.xml');
  92.             $loader->load('form/form_types.xml');
  93.             $loader->load('form/validator.xml');
  94.         }
  95.         if (!isset($bundles['SonataTwigBundle'])) {
  96.             $loader->load('twig/flash.xml');
  97.             $loader->load('twig/twig.xml');
  98.             $this->registerFlashTypes($container$config);
  99.         }
  100.         $container->setParameter('sonata.core.form'$config['form']);
  101.         $container->setParameter('sonata.core.form_type'$config['form_type']);
  102.         $container->setParameter('sonata.core.flashmessage'$config['flashmessage']);
  103.         $container->setParameter('sonata.core.serializer'$config['serializer']);
  104.         $this->configureFormFactory($container$config);
  105.         if (\PHP_VERSION_ID 70000) {
  106.             $this->configureClassesToCompile();
  107.         }
  108.         $this->configureSerializerFormats($config);
  109.     }
  110.     public function configureClassesToCompile()
  111.     {
  112.         $this->addClassesToCompile([
  113.             BooleanType::class,
  114.             CollectionType::class,
  115.             DateRangeType::class,
  116.             DateTimeRangeType::class,
  117.             EqualType::class,
  118.             ImmutableArrayType::class,
  119.             TranslatableChoiceType::class,
  120.         ]);
  121.     }
  122.     public function configureFormFactory(ContainerBuilder $container, array $config)
  123.     {
  124.         if (!$config['form']['mapping']['enabled'] || !class_exists(FormPass::class)) {
  125.             $container->removeDefinition('sonata.core.form.extension.dependency');
  126.             return;
  127.         }
  128.         @trigger_error(
  129.             'Relying on the form mapping feature is deprecated since 3.7 and will be removed in 4.0. Please set the "sonata_core.form.mapping.enabled" configuration node to false to avoid this message.',
  130.             E_USER_DEPRECATED
  131.         );
  132.         $container->setParameter('sonata.core.form.mapping.type'$config['form']['mapping']['type']);
  133.         $container->setParameter('sonata.core.form.mapping.extension'$config['form']['mapping']['extension']);
  134.         FormHelper::registerFormTypeMapping($config['form']['mapping']['type']);
  135.         foreach ($config['form']['mapping']['extension'] as $ext => $idx) {
  136.             FormHelper::registerFormExtensionMapping($ext$idx);
  137.         }
  138.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  139.         $definition->replaceArgument(4FormHelper::getFormTypeMapping());
  140.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  141.         $definition->replaceArgument(5FormHelper::getFormExtensionMapping());
  142.     }
  143.     /**
  144.      * Registers flash message types defined in configuration to flash manager.
  145.      */
  146.     public function registerFlashTypes(ContainerBuilder $container, array $config)
  147.     {
  148.         $mergedConfig array_merge_recursive($config['flashmessage'], [
  149.             'success' => ['types' => [
  150.                 'success' => ['domain' => 'SonataCoreBundle'],
  151.                 'sonata_flash_success' => ['domain' => 'SonataAdminBundle'],
  152.                 'sonata_user_success' => ['domain' => 'SonataUserBundle'],
  153.                 'fos_user_success' => ['domain' => 'FOSUserBundle'],
  154.             ]],
  155.             'warning' => ['types' => [
  156.                 'warning' => ['domain' => 'SonataCoreBundle'],
  157.                 'sonata_flash_info' => ['domain' => 'SonataAdminBundle'],
  158.             ]],
  159.             'danger' => ['types' => [
  160.                 'error' => ['domain' => 'SonataCoreBundle'],
  161.                 'sonata_flash_error' => ['domain' => 'SonataAdminBundle'],
  162.                 'sonata_user_error' => ['domain' => 'SonataUserBundle'],
  163.             ]],
  164.         ]);
  165.         $types $cssClasses = [];
  166.         foreach ($mergedConfig as $typeKey => $typeConfig) {
  167.             $types[$typeKey] = $typeConfig['types'];
  168.             $cssClasses[$typeKey] = \array_key_exists('css_class'$typeConfig) ? $typeConfig['css_class'] : $typeKey;
  169.         }
  170.         $identifier 'sonata.twig.flashmessage.manager';
  171.         $definition $container->getDefinition($identifier);
  172.         $definition->replaceArgument(2$types);
  173.         $definition->replaceArgument(3$cssClasses);
  174.         $container->setDefinition($identifier$definition);
  175.     }
  176.     /**
  177.      * @param array $config
  178.      */
  179.     public function configureSerializerFormats($config)
  180.     {
  181.         if (interface_exists(SubscribingHandlerInterface::class)) {
  182.             BaseSerializerHandler::setFormats($config['serializer']['formats']);
  183.         }
  184.     }
  185. }