CustomHandlersPass.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. $ref = new \ReflectionClass($class);
  33. if ( ! $ref->implementsInterface('JMS\SerializerBundle\Serializer\Handler\SubscribingHandlerInterface')) {
  34. throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.', $id));
  35. }
  36. foreach (call_user_func(array($class, 'getSubscribingMethods')) as $methodData) {
  37. if ( ! isset($methodData['format'], $methodData['type'])) {
  38. throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.', $id));
  39. }
  40. $directions = array(GraphNavigator::DIRECTION_DESERIALIZATION, GraphNavigator::DIRECTION_SERIALIZATION);
  41. if (isset($methodData['direction'])) {
  42. $directions = array($methodData['direction']);
  43. }
  44. foreach ($directions as $direction) {
  45. $method = isset($methodData['method']) ? $methodData['method'] : HandlerRegistry::getDefaultMethod($direction, $methodData['type'], $methodData['format']);
  46. $handlers[$direction][$methodData['type']][$methodData['format']] = array($id, $method);
  47. }
  48. }
  49. }
  50. $container->getDefinition('jms_serializer.handler_registry')
  51. ->addArgument($handlers);
  52. }
  53. }