custom_handlers.rst 3.0 KB

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