SonataCoreExtension.php 7.4 KB

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