CustomHandlersPass.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace JMS\SerializerBundle\DependencyInjection\Compiler;
  3. use JMS\SerializerBundle\Serializer\Handler\HandlerRegistry;
  4. use JMS\SerializerBundle\Serializer\GraphNavigator;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  7. class CustomHandlersPass implements CompilerPassInterface
  8. {
  9. public function process(ContainerBuilder $container)
  10. {
  11. $handlers = array();
  12. foreach ($container->findTaggedServiceIds('jms_serializer.handler') as $id => $tags) {
  13. foreach ($tags as $attrs) {
  14. if ( ! isset($attrs['type'], $attrs['format'])) {
  15. throw new \RuntimeException(sprintf('Each tag named "jms_serializer.custom_handler" of service "%s" must have at least two attributes: "type", and "format".', $id));
  16. }
  17. $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
  18. if (isset($attrs['direction'])) {
  19. if ( ! defined($directionConstant = 'JMS\SerializerBundle\Serializer\GraphNavigator::DIRECTION_'.strtoupper($attrs['direction']))) {
  20. throw new \RuntimeException(sprintf('The direction "%s" of tag "jms_serializer.custom_handler" of service "%s" does not exist.', $attrs['direction'], $id));
  21. }
  22. $directions = array(constant($directionConstant));
  23. }
  24. foreach ($directions as $direction) {
  25. $method = isset($attrs['method']) ? $attrs['method'] : HandlerRegistry::getDefaultMethod($direction, $attrs['type'], $attrs['format']);
  26. $handlers[$direction][$attrs['type']][$attrs['format']] = array($id, $method);
  27. }
  28. }
  29. }
  30. foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) {
  31. $class = $container->getDefinition($id)->getClass();
  32. if ( ! is_subclass_of($class, 'JMS\SerializerBundle\Serializer\Handler\SubscribingHandlerInterface')) {
  33. throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id));
  34. }
  35. foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) {
  36. if ( ! isset($methodData['format'], $methodData['type'])) {
  37. throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.', $id));
  38. }
  39. $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
  40. if (isset($methodData['direction'])) {
  41. $directions = array($methodData['direction']);
  42. }
  43. foreach ($directions as $direction) {
  44. $method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
  45. $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
  46. }
  47. }
  48. }
  49. $container->getDefinition('jms_serializer.handler_registry')
  50. ->addArgument($handlers);
  51. }
  52. }