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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sonata Project package.
  4.  *
  5.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sonata\CoreBundle\DependencyInjection;
  11. use JMS\Serializer\Handler\SubscribingHandlerInterface;
  12. use Sonata\CoreBundle\Form\FormHelper;
  13. use Sonata\CoreBundle\Form\Type\BooleanType;
  14. use Sonata\CoreBundle\Form\Type\CollectionType;
  15. use Sonata\CoreBundle\Form\Type\DateRangeType;
  16. use Sonata\CoreBundle\Form\Type\DateTimeRangeType;
  17. use Sonata\CoreBundle\Form\Type\EqualType;
  18. use Sonata\CoreBundle\Form\Type\ImmutableArrayType;
  19. use Sonata\CoreBundle\Form\Type\TranslatableChoiceType;
  20. use Sonata\CoreBundle\Serializer\BaseSerializerHandler;
  21. use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
  22. use Symfony\Component\Config\Definition\Processor;
  23. use Symfony\Component\Config\FileLocator;
  24. use Symfony\Component\DependencyInjection\ContainerBuilder;
  25. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  26. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  27. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  28. /**
  29.  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  30.  */
  31. class SonataCoreExtension extends Extension implements PrependExtensionInterface
  32. {
  33.     public function prepend(ContainerBuilder $container)
  34.     {
  35.         $configs $container->getExtensionConfig('sonata_admin');
  36.         foreach ($configs as $config) {
  37.             if (isset($config['options']['form_type'])) {
  38.                 $container->prependExtensionConfig(
  39.                     $this->getAlias(),
  40.                     ['form_type' => $config['options']['form_type']]
  41.                 );
  42.             }
  43.         }
  44.     }
  45.     public function load(array $configsContainerBuilder $container)
  46.     {
  47.         $processor = new Processor();
  48.         $configuration = new Configuration();
  49.         // NEXT_MAJOR : remove this if block
  50.         if (!interface_exists(SubscribingHandlerInterface::class)) {
  51.             /* Let's check for config values before the configuration is processed,
  52.              * otherwise we won't be able to tell,
  53.              * since there is a default value for this option. */
  54.             foreach ($configs as $config) {
  55.                 if (isset($config['serializer'])) {
  56.                     @trigger_error(<<<'EOT'
  57. Setting the sonata_core -> serializer -> formats option
  58. without having the jms/serializer library installed is deprecated since 3.1,
  59. and will not be supported in 4.0,
  60. because the configuration option will not be added in that case.
  61. EOT
  62.                     , E_USER_DEPRECATED);
  63.                 }
  64.             }
  65.         }
  66.         $config $processor->processConfiguration($configuration$configs);
  67.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  68.         $loader->load('date.xml');
  69.         $loader->load('flash.xml');
  70.         $loader->load('form_types.xml');
  71.         $loader->load('validator.xml');
  72.         $loader->load('twig.xml');
  73.         $loader->load('model_adapter.xml');
  74.         $loader->load('core.xml');
  75.         $loader->load('commands.xml');
  76.         $this->registerFlashTypes($container$config);
  77.         $container->setParameter('sonata.core.form_type'$config['form_type']);
  78.         $this->configureFormFactory($container$config);
  79.         if (\PHP_VERSION_ID 70000) {
  80.             $this->configureClassesToCompile();
  81.         }
  82.         $this->deprecateSlugify($container);
  83.         $this->configureSerializerFormats($config);
  84.     }
  85.     public function configureClassesToCompile()
  86.     {
  87.         $this->addClassesToCompile([
  88.             BooleanType::class,
  89.             CollectionType::class,
  90.             DateRangeType::class,
  91.             DateTimeRangeType::class,
  92.             EqualType::class,
  93.             ImmutableArrayType::class,
  94.             TranslatableChoiceType::class,
  95.         ]);
  96.     }
  97.     public function configureFormFactory(ContainerBuilder $container, array $config)
  98.     {
  99.         if (!$config['form']['mapping']['enabled'] || !class_exists(FormPass::class)) {
  100.             $container->removeDefinition('sonata.core.form.extension.dependency');
  101.             return;
  102.         }
  103.         @trigger_error(
  104.             '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.',
  105.             E_USER_DEPRECATED
  106.         );
  107.         $container->setParameter('sonata.core.form.mapping.type'$config['form']['mapping']['type']);
  108.         $container->setParameter('sonata.core.form.mapping.extension'$config['form']['mapping']['extension']);
  109.         FormHelper::registerFormTypeMapping($config['form']['mapping']['type']);
  110.         foreach ($config['form']['mapping']['extension'] as $ext => $idx) {
  111.             FormHelper::registerFormExtensionMapping($ext$idx);
  112.         }
  113.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  114.         $definition->replaceArgument(4FormHelper::getFormTypeMapping());
  115.         $definition $container->getDefinition('sonata.core.form.extension.dependency');
  116.         $definition->replaceArgument(5FormHelper::getFormExtensionMapping());
  117.     }
  118.     /**
  119.      * Registers flash message types defined in configuration to flash manager.
  120.      */
  121.     public function registerFlashTypes(ContainerBuilder $container, array $config)
  122.     {
  123.         $mergedConfig array_merge_recursive($config['flashmessage'], [
  124.             'success' => ['types' => [
  125.                 'success' => ['domain' => 'SonataCoreBundle'],
  126.                 'sonata_flash_success' => ['domain' => 'SonataAdminBundle'],
  127.                 'sonata_user_success' => ['domain' => 'SonataUserBundle'],
  128.                 'fos_user_success' => ['domain' => 'FOSUserBundle'],
  129.             ]],
  130.             'warning' => ['types' => [
  131.                 'warning' => ['domain' => 'SonataCoreBundle'],
  132.                 'sonata_flash_info' => ['domain' => 'SonataAdminBundle'],
  133.             ]],
  134.             'danger' => ['types' => [
  135.                 'error' => ['domain' => 'SonataCoreBundle'],
  136.                 'sonata_flash_error' => ['domain' => 'SonataAdminBundle'],
  137.                 'sonata_user_error' => ['domain' => 'SonataUserBundle'],
  138.             ]],
  139.         ]);
  140.         $types $cssClasses = [];
  141.         foreach ($mergedConfig as $typeKey => $typeConfig) {
  142.             $types[$typeKey] = $typeConfig['types'];
  143.             $cssClasses[$typeKey] = array_key_exists('css_class'$typeConfig) ? $typeConfig['css_class'] : $typeKey;
  144.         }
  145.         $identifier 'sonata.core.flashmessage.manager';
  146.         $definition $container->getDefinition($identifier);
  147.         $definition->replaceArgument(2$types);
  148.         $definition->replaceArgument(3$cssClasses);
  149.         $container->setDefinition($identifier$definition);
  150.     }
  151.     /**
  152.      * @param array $config
  153.      */
  154.     public function configureSerializerFormats($config)
  155.     {
  156.         if (interface_exists(SubscribingHandlerInterface::class)) {
  157.             BaseSerializerHandler::setFormats($config['serializer']['formats']);
  158.         }
  159.     }
  160.     protected function deprecateSlugify(ContainerBuilder $container)
  161.     {
  162.         $container->getDefinition('sonata.core.slugify.cocur')->setDeprecated(true);
  163.         $container->getDefinition('sonata.core.slugify.native')->setDeprecated(true);
  164.     }
  165. }