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

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('date.xml');
  87.         $loader->load('flash.xml');
  88.         $loader->load('form_types.xml');
  89.         $loader->load('model_adapter.xml');
  90.         $loader->load('twig.xml');
  91.         $loader->load('validator.xml');
  92.         if (!isset($bundles['SonataFormBundle'])) {
  93.             $loader->load('form/date.xml');
  94.             $loader->load('form/form_types.xml');
  95.             $loader->load('form/validator.xml');
  96.         }
  97.         if (!isset($bundles['SonataTwigBundle'])) {
  98.             $loader->load('twig/flash.xml');
  99.             $loader->load('twig/twig.xml');
  100.         }
  101.         $this->registerFlashTypes($container$config);
  102.         $container->setParameter('sonata.core.form_type'$config['form_type']);
  103.         $this->configureFormFactory($container$config);
  104.         if (\PHP_VERSION_ID 70000) {
  105.             $this->configureClassesToCompile();
  106.         }
  107.         $this->configureSerializerFormats($config);
  108.     }
  109.     public function configureClassesToCompile()
  110.     {
  111.         $this->addClassesToCompile([
  112.             BooleanType::class,
  113.             CollectionType::class,
  114.             DateRangeType::class,
  115.             DateTimeRangeType::class,
  116.             EqualType::class,
  117.             ImmutableArrayType::class,
  118.             TranslatableChoiceType::class,
  119.         ]);
  120.     }
  121.     public function configureFormFactory(ContainerBuilder $container, array $config)
  122.     {
  123.         if (!$config['form']['mapping']['enabled'] || !class_exists(FormPass::class)) {
  124.             $container->removeDefinition('sonata.core.form.extension.dependency');
  125.             return;
  126.         }
  127.         @trigger_error(
  128.             '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.',
  129.             E_USER_DEPRECATED
  130.         );
  131.         $container->setParameter('sonata.core.form.mapping.type'$config['form']['mapping']['type']);
  132.         $container->setParameter('sonata.core.form.mapping.extension'$config['form']['mapping']['extension']);
  133.         FormHelper::registerFormTypeMapping($config['form']['mapping']['type']);
  134.         foreach ($config['form']['mapping']['extension'] as $ext => $idx) {
  135.             FormHelper::registerFormExtensionMapping($ext$idx);
  136.         }
  137.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  138.         $definition->replaceArgument(4FormHelper::getFormTypeMapping());
  139.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  140.         $definition->replaceArgument(5FormHelper::getFormExtensionMapping());
  141.     }
  142.     /**
  143.      * Registers flash message types defined in configuration to flash manager.
  144.      */
  145.     public function registerFlashTypes(ContainerBuilder $container, array $config)
  146.     {
  147.         $mergedConfig array_merge_recursive($config['flashmessage'], [
  148.             'success' => ['types' => [
  149.                 'success' => ['domain' => 'SonataCoreBundle'],
  150.                 'sonata_flash_success' => ['domain' => 'SonataAdminBundle'],
  151.                 'sonata_user_success' => ['domain' => 'SonataUserBundle'],
  152.                 'fos_user_success' => ['domain' => 'FOSUserBundle'],
  153.             ]],
  154.             'warning' => ['types' => [
  155.                 'warning' => ['domain' => 'SonataCoreBundle'],
  156.                 'sonata_flash_info' => ['domain' => 'SonataAdminBundle'],
  157.             ]],
  158.             'danger' => ['types' => [
  159.                 'error' => ['domain' => 'SonataCoreBundle'],
  160.                 'sonata_flash_error' => ['domain' => 'SonataAdminBundle'],
  161.                 'sonata_user_error' => ['domain' => 'SonataUserBundle'],
  162.             ]],
  163.         ]);
  164.         $types $cssClasses = [];
  165.         foreach ($mergedConfig as $typeKey => $typeConfig) {
  166.             $types[$typeKey] = $typeConfig['types'];
  167.             $cssClasses[$typeKey] = \array_key_exists('css_class'$typeConfig) ? $typeConfig['css_class'] : $typeKey;
  168.         }
  169.         $identifier 'sonata.twig.flashmessage.manager';
  170.         $definition $container->getDefinition($identifier);
  171.         $definition->replaceArgument(2$types);
  172.         $definition->replaceArgument(3$cssClasses);
  173.         $container->setDefinition($identifier$definition);
  174.     }
  175.     /**
  176.      * @param array $config
  177.      */
  178.     public function configureSerializerFormats($config)
  179.     {
  180.         if (interface_exists(SubscribingHandlerInterface::class)) {
  181.             BaseSerializerHandler::setFormats($config['serializer']['formats']);
  182.         }
  183.     }
  184. }