Configuration.php 7.1 KB

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