extending.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. Extending The Serializer
  2. ========================
  3. This document details the different extension points, and how you can utilize
  4. them to change the default behavior of the serializer.
  5. Custom De-/Serialization Handlers
  6. ---------------------------------
  7. This allows you to change the way of how a specifc type is being de-/serialized.
  8. Any handler must implement either the ``SerializationHandlerInterface``, or
  9. ``DeserializationHandlerInterface``, or both. This bundle already comes with
  10. some handlers which you find in the Serializer/Handler folder, and which you
  11. can use as a starting point.
  12. Custom handlers are normal services, and thus can have dependencies on any
  13. other service available in the dependency injection container.
  14. After you have written your handler, you can write a service definition. Such
  15. as the following::
  16. <service id="acme_foo.serializer.my_handler"
  17. class="Acme\FooBundle\Serializer\MyHandler"
  18. public="false"
  19. />
  20. What is left to do is to publish our new handler to this bundle. So it gets
  21. picked up, and wired correctly. In order to do this, this bundle uses a
  22. configuration system similar to that of the SecurityBundle. Each handler needs
  23. a corresponding definition factory::
  24. <?php
  25. namespace Acme\FooBundle\DependencyInjection\Factory;
  26. use JMS\SerializerBundle\DependencyInjection\HandlerFactoryInterface;
  27. class MyHandlerFactory implements HandlerFactoryInterface
  28. {
  29. public function getConfigKey()
  30. {
  31. return 'acme_foo_my';
  32. }
  33. public function getType(array $config)
  34. {
  35. return self::TYPE_SERIALIZATION | self::TYPE_DESERIALIZATION;
  36. }
  37. public function addConfiguration(ArrayNodeDefinition $builder)
  38. {
  39. $builder
  40. ->children()
  41. ->scalarNode('foo')->end()
  42. ->scalarNode('bar')->end()
  43. ->end()
  44. ;
  45. }
  46. public function getHandlerId(ContainerBuilder $container, array $config)
  47. {
  48. return 'acme_foo.serializer.my_handler';
  49. }
  50. }
  51. This factory is responsible for setting up the configuration for your handler
  52. in the ``addConfiguration`` method, and then process that configuration in the
  53. ``getHandlerId`` method.
  54. The last thing left to do, is to add this factory to this bundle. This is
  55. done by adding a ``configureSerializerExtension`` to your bundle class::
  56. <?php
  57. namespace Acme\FooBundle;
  58. use Acme\FooBundle\DependencyInjection\Factory\FooFactory;
  59. use JMS\SerializerBundle\DependencyInjection\JMSSerializerExtension;
  60. use Symfony\Component\HttpKernel\Bundle\Bundle;
  61. class AcmeFooBundle extends Bundle
  62. {
  63. public function configureSerializerExtension(JMSSerializerExtension $ext)
  64. {
  65. $ext->addHandlerFactory(new FooFactory());
  66. }
  67. }