Configuration.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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->addSerializersSection($root);
  42. $this->addMetadataSection($root);
  43. $this->addVisitorsSection($root);
  44. return $tb;
  45. }
  46. private function addSerializersSection(NodeBuilder $builder)
  47. {
  48. $builder
  49. ->arrayNode('property_naming')
  50. ->addDefaultsIfNotSet()
  51. ->children()
  52. ->scalarNode('id')->cannotBeEmpty()->end()
  53. ->scalarNode('separator')->defaultValue('_')->end()
  54. ->booleanNode('lower_case')->defaultTrue()->end()
  55. ->booleanNode('enable_cache')->defaultTrue()->end()
  56. ->end()
  57. ->end()
  58. ;
  59. $handlerNode = $builder
  60. ->arrayNode('handlers')
  61. ->addDefaultsIfNotSet()
  62. ->disallowNewKeysInSubsequentConfigs()
  63. ->children()
  64. ;
  65. foreach ($this->factories as $factory) {
  66. $factory->addConfiguration(
  67. $handlerNode->arrayNode($factory->getConfigKey())->canBeUnset()
  68. );
  69. }
  70. }
  71. private function addMetadataSection(NodeBuilder $builder)
  72. {
  73. $builder
  74. ->arrayNode('metadata')
  75. ->addDefaultsIfNotSet()
  76. ->fixXmlConfig('directory', 'directories')
  77. ->children()
  78. ->scalarNode('cache')->defaultValue('file')->end()
  79. ->booleanNode('debug')->defaultValue($this->debug)->end()
  80. ->arrayNode('file_cache')
  81. ->addDefaultsIfNotSet()
  82. ->children()
  83. ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
  84. ->end()
  85. ->end()
  86. ->booleanNode('auto_detection')->defaultTrue()->end()
  87. ->arrayNode('directories')
  88. ->prototype('array')
  89. ->children()
  90. ->scalarNode('path')->isRequired()->end()
  91. ->scalarNode('namespace_prefix')->defaultValue('')->end()
  92. ->end()
  93. ->end()
  94. ->end()
  95. ->end()
  96. ->end()
  97. ;
  98. }
  99. private function addVisitorsSection(NodeBuilder $builder)
  100. {
  101. $builder
  102. ->arrayNode('visitors')
  103. ->addDefaultsIfNotSet()
  104. ->children()
  105. ->arrayNode('json')
  106. ->addDefaultsIfNotSet()
  107. ->children()
  108. ->scalarNode('options')
  109. ->defaultValue(0)
  110. ->beforeNormalization()
  111. ->ifArray()->then(function($v) {
  112. $options = 0;
  113. foreach ($v as $option) {
  114. if (is_numeric($option)) {
  115. $options |= (int) $option;
  116. } elseif (defined($option)) {
  117. $options |= constant($option);
  118. } else {
  119. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  120. }
  121. }
  122. return $options;
  123. })
  124. ->end()
  125. ->beforeNormalization()
  126. ->ifString()->then(function($v) {
  127. if (is_numeric($v)) {
  128. $value = (int) $v;
  129. } elseif (defined($v)) {
  130. $value = constant($v);
  131. } else {
  132. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  133. }
  134. return $value;
  135. })
  136. ->end()
  137. ->validate()
  138. ->always(function($v) {
  139. if (!is_int($v)) {
  140. throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
  141. }
  142. return $v;
  143. })
  144. ->end()
  145. ->end()
  146. ->end()
  147. ->end()
  148. ->arrayNode('xml')
  149. ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
  150. ->addDefaultsIfNotSet()
  151. ->children()
  152. ->arrayNode('doctype_whitelist')
  153. ->prototype('scalar')->end()
  154. ->end()
  155. ->end()
  156. ->end()
  157. ->end()
  158. ->end()
  159. ;
  160. }
  161. }