Configuration.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Definition\ConfigurationInterface;
  14. /**
  15. * This is the class that validates and merges configuration from your app/config files.
  16. *
  17. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  18. * @author Alexander <iam.asm89@gmail.com>
  19. */
  20. class Configuration implements ConfigurationInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function getConfigTreeBuilder()
  26. {
  27. $treeBuilder = new TreeBuilder();
  28. $rootNode = $treeBuilder->root('sonata_core');
  29. $this->addFlashMessageSection($rootNode);
  30. $this->addSerializerFormats($rootNode);
  31. $rootNode
  32. ->children()
  33. ->arrayNode('form')
  34. ->addDefaultsIfNotSet()
  35. ->children()
  36. ->arrayNode('mapping')
  37. ->addDefaultsIfNotSet()
  38. ->children()
  39. ->booleanNode('enabled')
  40. ->defaultValue(true)
  41. ->end()
  42. ->arrayNode('type')
  43. ->useAttributeAsKey('id')
  44. ->defaultValue(array())
  45. ->prototype('scalar')->end()
  46. ->end()
  47. ->arrayNode('extension')
  48. ->useAttributeAsKey('id')
  49. ->defaultValue(array())
  50. ->prototype('array')
  51. ->prototype('scalar')->end()
  52. ->end()
  53. ->end()
  54. ->end()
  55. ->end()
  56. ->end()
  57. ->end()
  58. ->end()
  59. ;
  60. return $treeBuilder;
  61. }
  62. /**
  63. * Returns configuration for flash messages.
  64. *
  65. * @param ArrayNodeDefinition $node
  66. */
  67. private function addFlashMessageSection(ArrayNodeDefinition $node)
  68. {
  69. $node
  70. ->children()
  71. ->scalarNode('form_type')
  72. ->defaultValue('standard')
  73. ->validate()
  74. ->ifNotInArray($validFormTypes = array('standard', 'horizontal'))
  75. ->thenInvalid(sprintf(
  76. 'The form_type option value must be one of %s',
  77. $validFormTypesString = implode(', ', $validFormTypes)
  78. ))
  79. ->end()
  80. ->info(sprintf('Must be one of %s', $validFormTypesString))
  81. ->end()
  82. ->arrayNode('flashmessage')
  83. ->useAttributeAsKey('message')
  84. ->prototype('array')
  85. ->children()
  86. ->scalarNode('css_class')->end()
  87. ->arrayNode('types')
  88. ->useAttributeAsKey('type')
  89. ->prototype('array')
  90. ->children()
  91. ->scalarNode('domain')->defaultValue('SonataCoreBundle')->end()
  92. ->end()
  93. ->end()
  94. ->end()
  95. ->end()
  96. ->end()
  97. ->end()
  98. ->end()
  99. ;
  100. }
  101. /**
  102. * Returns configuration for serializer formats.
  103. *
  104. * @param ArrayNodeDefinition $node
  105. */
  106. private function addSerializerFormats(ArrayNodeDefinition $node)
  107. {
  108. // NEXT_MAJOR : do not execute this if jms/serializer is missing
  109. $node
  110. ->children()
  111. ->arrayNode('serializer')
  112. ->addDefaultsIfNotSet()
  113. ->children()
  114. ->arrayNode('formats')
  115. ->prototype('scalar')->end()
  116. ->defaultValue(array('json', 'xml', 'yml'))
  117. ->info('Default serializer formats, will be used while getting subscribing methods.')
  118. ->end()
  119. ->end()
  120. ->end()
  121. ->end()
  122. ;
  123. }
  124. }