SonataCoreBundle.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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;
  11. use Sonata\CoreBundle\DependencyInjection\Compiler\AdapterCompilerPass;
  12. use Sonata\CoreBundle\DependencyInjection\Compiler\FormFactoryCompilerPass;
  13. use Sonata\CoreBundle\DependencyInjection\Compiler\StatusRendererCompilerPass;
  14. use Sonata\CoreBundle\Form\FormHelper;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\HttpKernel\Bundle\Bundle;
  17. class SonataCoreBundle extends Bundle
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function build(ContainerBuilder $container)
  23. {
  24. $container->addCompilerPass(new StatusRendererCompilerPass());
  25. $container->addCompilerPass(new AdapterCompilerPass());
  26. $container->addCompilerPass(new FormFactoryCompilerPass());
  27. $this->registerFormMapping();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function boot()
  33. {
  34. // not sur we need this at Runtime ...
  35. $this->registerFormMapping();
  36. }
  37. /**
  38. * Register form mapping information.
  39. */
  40. public function registerFormMapping()
  41. {
  42. // symfony
  43. FormHelper::registerFormTypeMapping(array(
  44. 'form' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
  45. 'birthday' => 'Symfony\Component\Form\Extension\Core\Type\BirthdayType',
  46. 'checkbox' => 'Symfony\Component\Form\Extension\Core\Type\CheckboxType',
  47. 'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
  48. 'collection' => 'Symfony\Component\Form\Extension\Core\Type\CollectionType',
  49. 'country' => 'Symfony\Component\Form\Extension\Core\Type\CountryType',
  50. 'date' => 'Symfony\Component\Form\Extension\Core\Type\DateType',
  51. 'datetime' => 'Symfony\Component\Form\Extension\Core\Type\DateTimeType',
  52. 'email' => 'Symfony\Component\Form\Extension\Core\Type\EmailType',
  53. 'file' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
  54. 'hidden' => 'Symfony\Component\Form\Extension\Core\Type\HiddenType',
  55. 'integer' => 'Symfony\Component\Form\Extension\Core\Type\IntegerType',
  56. 'language' => 'Symfony\Component\Form\Extension\Core\Type\LanguageType',
  57. 'locale' => 'Symfony\Component\Form\Extension\Core\Type\LocaleType',
  58. 'money' => 'Symfony\Component\Form\Extension\Core\Type\MoneyType',
  59. 'number' => 'Symfony\Component\Form\Extension\Core\Type\NumberType',
  60. 'password' => 'Symfony\Component\Form\Extension\Core\Type\PasswordType',
  61. 'percent' => 'Symfony\Component\Form\Extension\Core\Type\PercentType',
  62. 'radio' => 'Symfony\Component\Form\Extension\Core\Type\RadioType',
  63. 'repeated' => 'Symfony\Component\Form\Extension\Core\Type\RepeatedType',
  64. 'search' => 'Symfony\Component\Form\Extension\Core\Type\SearchType',
  65. 'textarea' => 'Symfony\Component\Form\Extension\Core\Type\TextareaType',
  66. 'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
  67. 'time' => 'Symfony\Component\Form\Extension\Core\Type\TimeType',
  68. 'timezone' => 'Symfony\Component\Form\Extension\Core\Type\TimezoneType',
  69. 'url' => 'Symfony\Component\Form\Extension\Core\Type\UrlType',
  70. 'button' => 'Symfony\Component\Form\Extension\Core\Type\ButtonType',
  71. 'submit' => 'Symfony\Component\Form\Extension\Core\Type\SubmitType',
  72. 'reset' => 'Symfony\Component\Form\Extension\Core\Type\ResetType',
  73. 'currency' => 'Symfony\Component\Form\Extension\Core\Type\CurrencyType',
  74. 'entity' => 'Symfony\Bridge\Doctrine\Form\Type\EntityType',
  75. ));
  76. // core bundle
  77. FormHelper::registerFormTypeMapping(array(
  78. 'sonata_type_immutable_array' => 'Sonata\CoreBundle\Form\Type\ImmutableArrayType',
  79. 'sonata_type_boolean' => 'Sonata\CoreBundle\Form\Type\BooleanType',
  80. 'sonata_type_collection' => 'Sonata\CoreBundle\Form\Type\CollectionType',
  81. 'sonata_type_translatable_choice' => 'Sonata\CoreBundle\Form\Type\TranslatableChoiceType',
  82. 'sonata_type_date_range' => 'Sonata\CoreBundle\Form\Type\DateRangeType',
  83. 'sonata_type_datetime_range' => 'Sonata\CoreBundle\Form\Type\DateTimeRangeType',
  84. 'sonata_type_date_picker' => 'Sonata\CoreBundle\Form\Type\DatePickerType',
  85. 'sonata_type_datetime_picker' => 'Sonata\CoreBundle\Form\Type\DateTimePickerType',
  86. 'sonata_type_date_range_picker' => 'Sonata\CoreBundle\Form\Type\DateRangePickerType',
  87. 'sonata_type_datetime_range_picker' => 'Sonata\CoreBundle\Form\Type\DateTimeRangePickerType',
  88. 'sonata_type_equal' => 'Sonata\CoreBundle\Form\Type\EqualType',
  89. 'sonata_type_color_selector' => 'Sonata\CoreBundle\Form\Type\ColorSelectorType',
  90. ));
  91. $formTypes = array(
  92. 'form.type_extension.form.http_foundation',
  93. 'form.type_extension.form.validator',
  94. 'form.type_extension.csrf',
  95. 'form.type_extension.form.data_collector',
  96. );
  97. if (class_exists('Nelmio\ApiDocBundle\Form\Extension\DescriptionFormTypeExtension')) {
  98. $formTypes[] = 'nelmio_api_doc.form.extension.description_form_type_extension';
  99. }
  100. FormHelper::registerFormExtensionMapping('form', $formTypes);
  101. FormHelper::registerFormExtensionMapping('repeated', array(
  102. 'form.type_extension.repeated.validator',
  103. ));
  104. FormHelper::registerFormExtensionMapping('submit', array(
  105. 'form.type_extension.submit.validator',
  106. ));
  107. if ($this->container && $this->container->hasParameter('sonata.core.form.mapping.type')) {
  108. // from configuration file
  109. FormHelper::registerFormTypeMapping($this->container->getParameter('sonata.core.form.mapping.type'));
  110. foreach ($this->container->getParameter('sonata.core.form.mapping.extension') as $ext => $types) {
  111. FormHelper::registerFormExtensionMapping($ext, $types);
  112. }
  113. }
  114. }
  115. }