Configuration.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. private function addMetadataSection(NodeBuilder $builder)
  71. {
  72. $builder
  73. ->arrayNode('metadata')
  74. ->addDefaultsIfNotSet()
  75. ->fixXmlConfig('directory', 'directories')
  76. ->children()
  77. ->scalarNode('cache')->defaultValue('file')->end()
  78. ->booleanNode('debug')->defaultValue($this->debug)->end()
  79. ->arrayNode('file_cache')
  80. ->addDefaultsIfNotSet()
  81. ->children()
  82. ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
  83. ->end()
  84. ->end()
  85. ->booleanNode('auto_detection')->defaultTrue()->end()
  86. ->arrayNode('directories')
  87. ->prototype('array')
  88. ->children()
  89. ->scalarNode('path')->isRequired()->end()
  90. ->scalarNode('namespace_prefix')->defaultValue('')->end()
  91. ->end()
  92. ->end()
  93. ->end()
  94. ->end()
  95. ->end()
  96. ;
  97. }
  98. private function addVisitorsSection(NodeBuilder $builder)
  99. {
  100. $builder
  101. ->arrayNode('visitors')
  102. ->addDefaultsIfNotSet()
  103. ->children()
  104. ->arrayNode('json')
  105. ->addDefaultsIfNotSet()
  106. ->children()
  107. ->scalarNode('options')
  108. ->defaultValue(0)
  109. ->beforeNormalization()
  110. ->ifArray()->then(function($v) {
  111. $options = 0;
  112. foreach ($v as $option) {
  113. if (is_numeric($option)) {
  114. $options |= (int) $option;
  115. } elseif (defined($option)) {
  116. $options |= constant($option);
  117. } else {
  118. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  119. }
  120. }
  121. return $options;
  122. })
  123. ->end()
  124. ->beforeNormalization()
  125. ->ifString()->then(function($v) {
  126. if (is_numeric($v)) {
  127. $value = (int) $v;
  128. } elseif (defined($v)) {
  129. $value = constant($v);
  130. } else {
  131. throw new InvalidArgumentException('Expected either an integer representing one of the JSON_ constants, or a string of the constant itself.');
  132. }
  133. return $value;
  134. })
  135. ->end()
  136. ->validate()
  137. ->always(function($v) {
  138. if (!is_int($v)) {
  139. throw new InvalidArgumentException('Expected either integer value or a array of the JSON_ constants.');
  140. }
  141. return $v;
  142. })
  143. ->end()
  144. ->end()
  145. ->end()
  146. ->end()
  147. ->arrayNode('xml')
  148. ->fixXmlConfig('whitelisted-doctype', 'doctype_whitelist')
  149. ->addDefaultsIfNotSet()
  150. ->children()
  151. ->arrayNode('doctype_whitelist')
  152. ->prototype('scalar')->end()
  153. ->end()
  154. ->end()
  155. ->end()
  156. ->end()
  157. ->end()
  158. ;
  159. }
  160. }