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

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