Configuration.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. class Configuration implements ConfigurationInterface
  22. {
  23. private $debug;
  24. private $factories;
  25. public function __construct($debug = false, array $factories = array())
  26. {
  27. $this->debug = $debug;
  28. $this->factories = $factories;
  29. }
  30. public function getConfigTreeBuilder()
  31. {
  32. $tb = new TreeBuilder();
  33. $root = $tb
  34. ->root('jms_serializer', 'array')
  35. ->children()
  36. ;
  37. $this->addSerializersSection($root);
  38. $this->addMetadataSection($root);
  39. return $tb;
  40. }
  41. private function addSerializersSection(NodeBuilder $builder)
  42. {
  43. $builder
  44. ->arrayNode('property_naming')
  45. ->addDefaultsIfNotSet()
  46. ->children()
  47. ->scalarNode('id')->cannotBeEmpty()->end()
  48. ->scalarNode('separator')->defaultValue('_')->end()
  49. ->booleanNode('lower_case')->defaultTrue()->end()
  50. ->booleanNode('enable_cache')->defaultTrue()->end()
  51. ->end()
  52. ->end()
  53. ;
  54. $handlerNode = $builder
  55. ->arrayNode('handlers')
  56. ->addDefaultsIfNotSet()
  57. ->disallowNewKeysInSubsequentConfigs()
  58. ->children()
  59. ;
  60. foreach ($this->factories as $factory) {
  61. $factory->addConfiguration(
  62. $handlerNode->arrayNode($factory->getConfigKey())->canBeUnset());
  63. }
  64. }
  65. private function addMetadataSection(NodeBuilder $builder)
  66. {
  67. $builder
  68. ->arrayNode('metadata')
  69. ->addDefaultsIfNotSet()
  70. ->fixXmlConfig('directory', 'directories')
  71. ->children()
  72. ->scalarNode('cache')->defaultValue('file')->end()
  73. ->booleanNode('debug')->defaultValue($this->debug)->end()
  74. ->arrayNode('file_cache')
  75. ->addDefaultsIfNotSet()
  76. ->children()
  77. ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/jms_serializer')->end()
  78. ->end()
  79. ->end()
  80. ->booleanNode('auto_detection')->defaultTrue()->end()
  81. ->arrayNode('directories')
  82. ->prototype('array')
  83. ->children()
  84. ->scalarNode('path')->isRequired()->end()
  85. ->scalarNode('namespace_prefix')->defaultValue('')->end()
  86. ->end()
  87. ->end()
  88. ->end()
  89. ->end()
  90. ->end()
  91. ;
  92. }
  93. }