custom_handlers.rst 3.2 KB

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