Configuration.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\DependencyInjection;
  18. use Symfony\Component\Config\Definition\Builder\NodeBuilder;
  19. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  20. use Symfony\Component\Config\Definition\ConfigurationInterface;
  21. use JMS\SerializerBundle\Exception\InvalidArgumentException;
  22. class Configuration implements ConfigurationInterface
  23. {
  24. private $debug;
  25. private $factories;
  26. /**
  27. * @param boolean $debug
  28. */
  29. public function __construct($debug = false, array $factories = array())
  30. {
  31. $this->debug = $debug;
  32. $this->factories = $factories;
  33. }
  34. public function getConfigTreeBuilder()
  35. {
  36. $tb = new TreeBuilder();
  37. $root = $tb
  38. ->root('jms_serializer', 'array')
  39. ->children()
  40. ;
  41. $this->addConstructorSection($root);
  42. $this->addSerializersSection($root);
  43. $this->addMetadataSection($root);
  44. $this->addVisitorsSection($root);
  45. return $tb;
  46. }
  47. private function addConstructorSection(NodeBuilder $builder)
  48. {
  49. $builder
  50. ->scalarNode('constructor')->defaultNull()->end();
  51. }
  52. private function addSerializersSection(NodeBuilder $builder)
  53. {
  54. $builder
  55. ->arrayNode('property_naming')
  56. ->addDefaultsIfNotSet()
  57. ->children()
  58. ->scalarNode('id')->cannotBeEmpty()->end()
  59. ->scalarNode('separator')->defaultValue('_')->end()
  60. ->booleanNode('lower_case')->defaultTrue()->end()
  61. ->booleanNode('enable_cache')->defaultTrue()->end()
  62. ->end()
  63. ->end()
  64. ;
  65. $handlerNode = $builder
  66. ->arrayNode('handlers')
  67. ->addDefaultsIfNotSet()
  68. ->disallowNewKeysInSubsequentConfigs()
  69. ->children()
  70. ;
  71. foreach ($this->factories as $factory) {
  72. $factory->addConfiguration(
  73. $handlerNode->arrayNode($factory->getConfigKey())->canBeUnset()
  74. );
  75. }
  76. }
  77. private function addMetadataSection(NodeBuilder $builder)
  78. {
  79. $builder
  80. ->arrayNode('metadata')
  81. ->addDefaultsIfNotSet()
  82. ->fixXmlConfig('directory', 'directories')
  83. ->children()
  84. ->scalarNode('cache')->defaultValue('file')->end()
  85. ->booleanNode('debug')->defaultValue($this->debug)->end()
  86. ->arrayNode('file_cache')
  87. ->addDefaultsIfNotSet()
  88. ->children()
  89. ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
  90. ->end()
  91. ->end()
  92. ->booleanNode('auto_detection')->defaultTrue()->end()
  93. ->arrayNode('directories')
  94. ->prototype('array')
  95. ->children()
  96. ->scalarNode('path')->isRequired()->end()
  97. ->scalarNode('namespace_prefix')->defaultValue('')->end()
  98. ->end()
  99. ->end()
  100. ->end()
  101. ->end()
  102. ->end()
  103. ;
  104. }
  105. private function addVisitorsSection(NodeBuilder $builder)
  106. {
  107. $builder
  108. ->arrayNode('visitors')
  109. ->addDefaultsIfNotSet()
  110. ->children()
  111. ->arrayNode('json')
  112. ->addDefaultsIfNotSet()
  113. ->children()
  114. ->scalarNode('options')
  115. ->defaultValue(0)
  116. ->beforeNormalization()
  117. ->ifArray()->then(function($v) {
  118. $options = 0;
  119. foreach ($v as $option) {
  120. if (is_numeric($option)) {
  121. $options |= (int) $option;
  122. } elseif (defined($option)) {
  123. $options |= constant($option);
  124. } else {
  125. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  126. }
  127. }
  128. return $options;
  129. })
  130. ->end()
  131. ->beforeNormalization()
  132. ->ifString()->then(function($v) {
  133. if (is_numeric($v)) {
  134. $value = (int) $v;
  135. } elseif (defined($v)) {
  136. $value = constant($v);
  137. } else {
  138. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  139. }
  140. return $value;
  141. })
  142. ->end()
  143. ->validate()
  144. ->always(function($v) {
  145. if (!is_int($v)) {
  146. throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
  147. }
  148. return $v;
  149. })
  150. ->end()
  151. ->end()
  152. ->end()
  153. ->end()
  154. ->arrayNode('xml')
  155. ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
  156. ->addDefaultsIfNotSet()
  157. ->children()
  158. ->arrayNode('doctype_whitelist')
  159. ->prototype('scalar')->end()
  160. ->end()
  161. ->end()
  162. ->end()
  163. ->end()
  164. ->end()
  165. ;
  166. }
  167. }