Configuration.php 6.4 KB

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