Configuration.php 6.4 KB

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