Configuration.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ->children()
  58. ;
  59. foreach ($this->factories as $factory) {
  60. $factory->addConfiguration(
  61. $handlerNode->arrayNode($factory->getConfigKey())->canBeUnset());
  62. }
  63. }
  64. private function addMetadataSection(NodeBuilder $builder)
  65. {
  66. $builder
  67. ->arrayNode('metadata')
  68. ->addDefaultsIfNotSet()
  69. ->fixXmlConfig('directory', 'directories')
  70. ->children()
  71. ->scalarNode('cache')->defaultValue('file')->end()
  72. ->booleanNode('debug')->defaultValue($this->debug)->end()
  73. ->arrayNode('file_cache')
  74. ->addDefaultsIfNotSet()
  75. ->children()
  76. ->scalarNode('dir')->defaultValue('%kernel.cache_dir%/serializer')->end()
  77. ->end()
  78. ->end()
  79. ->booleanNode('auto_detection')->defaultTrue()->end()
  80. ->arrayNode('directories')
  81. ->prototype('array')
  82. ->children()
  83. ->scalarNode('path')->isRequired()->end()
  84. ->scalarNode('namespace_prefix')->defaultValue('')->end()
  85. ->end()
  86. ->end()
  87. ->end()
  88. ->end()
  89. ->end()
  90. ;
  91. }
  92. }