JMSSerializerExtension.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\FileLocator;
  19. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. use Symfony\Component\Config\Definition\Processor;
  22. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  25. class JMSSerializerExtension extends Extension
  26. {
  27. public function load(array $configs, ContainerBuilder $container)
  28. {
  29. $processor = new Processor();
  30. $config = $processor->process($this->getConfigTree(), $configs);
  31. $loader = new XmlFileLoader($container, new FileLocator(array(__DIR__.'/../Resources/config/')));
  32. $loader->load('services.xml');
  33. // set encoders
  34. $encoders = array();
  35. if ($config['encoders']['xml']) {
  36. $encoders['xml'] = new Reference('jms_serializer_extra.xml_encoder');
  37. }
  38. if ($config['encoders']['json']) {
  39. $encoders['json'] = new Reference('jms_serializer_extra.json_encoder');
  40. }
  41. if (!$encoders) {
  42. throw new \RuntimeException('No encoders have been configured.');
  43. }
  44. $container
  45. ->getDefinition('jms_serializer_extra.serializer_factory')
  46. ->addArgument($encoders)
  47. ;
  48. // naming strategy
  49. $container
  50. ->getDefinition('jms_serializer_extra.camel_case_naming_strategy')
  51. ->addArgument($config['naming_strategy']['separator'])
  52. ->addArgument($config['naming_strategy']['lower_case'])
  53. ;
  54. }
  55. private function getConfigTree()
  56. {
  57. $tb = new TreeBuilder();
  58. $tb->root('jms_serializer_extra', 'array')
  59. ->fixXmlConfig('encoder')
  60. ->children()
  61. ->arrayNode('naming_strategy')
  62. ->addDefaultsIfNotSet()
  63. ->children()
  64. ->scalarNode('separator')->defaultValue('_')->end()
  65. ->booleanNode('lower_case')->defaultTrue()->end()
  66. ->end()
  67. ->end()
  68. ->arrayNode('encoders')
  69. ->addDefaultsIfNotSet()
  70. ->children()
  71. ->booleanNode('xml')->defaultTrue()->end()
  72. ->booleanNode('json')->defaultTrue()->end()
  73. ->end()
  74. ->end()
  75. ->end()
  76. ->end();
  77. return $tb->buildTree();
  78. }
  79. }