vendor/jms/serializer-bundle/DependencyInjection/Compiler/CustomHandlersPass.php line 73

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace JMS\SerializerBundle\DependencyInjection\Compiler;
  4. use JMS\Serializer\GraphNavigatorInterface;
  5. use JMS\Serializer\Handler\HandlerRegistry;
  6. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  7. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  8. use Symfony\Component\DependencyInjection\ContainerBuilder;
  9. use Symfony\Component\DependencyInjection\Reference;
  10. /**
  11.  * @internal
  12.  */
  13. final class CustomHandlersPass implements CompilerPassInterface
  14. {
  15.     public function process(ContainerBuilder $container)
  16.     {
  17.         $handlersByDirection $this->findHandlers($container);
  18.         $handlerRegistryDef $container->findDefinition('jms_serializer.handler_registry');
  19.         $handlerServices = [];
  20.         foreach ($handlersByDirection as $direction => $handlersByType) {
  21.             foreach ($handlersByType as $type => $handlersByFormat) {
  22.                 foreach ($handlersByFormat as $format => $handlerCallable) {
  23.                     $id = (string) $handlerCallable[0];
  24.                     $handlerServices[$id] = new ServiceClosureArgument($handlerCallable[0]);
  25.                     $handlerCallable[0] = $id;
  26.                     $handlerRegistryDef->addMethodCall('registerHandler', [$direction$type$format$handlerCallable]);
  27.                 }
  28.             }
  29.         }
  30.         $container->findDefinition('jms_serializer.handler_registry.service_locator')
  31.             ->setArgument(0$handlerServices);
  32.     }
  33.     private function findHandlers(ContainerBuilder $container): array
  34.     {
  35.         $handlers = [];
  36.         foreach ($container->findTaggedServiceIds('jms_serializer.handler') as $id => $tags) {
  37.             foreach ($tags as $attrs) {
  38.                 if (!isset($attrs['type'], $attrs['format'])) {
  39.                     throw new \RuntimeException(sprintf('Each tag named "jms_serializer.handler" of service "%s" must have at least two attributes: "type" and "format".'$id));
  40.                 }
  41.                 $directions = [GraphNavigatorInterface::DIRECTION_DESERIALIZATIONGraphNavigatorInterface::DIRECTION_SERIALIZATION];
  42.                 if (isset($attrs['direction'])) {
  43.                     if (!defined($directionConstant 'JMS\Serializer\GraphNavigatorInterface::DIRECTION_' strtoupper($attrs['direction']))) {
  44.                         throw new \RuntimeException(sprintf('The direction "%s" of tag "jms_serializer.handler" of service "%s" does not exist.'$attrs['direction'], $id));
  45.                     }
  46.                     $directions = [constant($directionConstant)];
  47.                 }
  48.                 foreach ($directions as $direction) {
  49.                     $method $attrs['method'] ?? HandlerRegistry::getDefaultMethod($direction$attrs['type'], $attrs['format']);
  50.                     $priority = isset($attrs['priority']) ? intval($attrs['priority']) : 0;
  51.                     $handlers[] = [$direction$attrs['type'], $attrs['format'], $priority, new Reference($id), $method];
  52.                 }
  53.             }
  54.         }
  55.         foreach ($container->findTaggedServiceIds('jms_serializer.subscribing_handler') as $id => $tags) {
  56.             $def $container->getDefinition($id);
  57.             $class $def->getClass();
  58.             $ref = new \ReflectionClass($class);
  59.             if (!$ref->implementsInterface('JMS\Serializer\Handler\SubscribingHandlerInterface')) {
  60.                 throw new \RuntimeException(sprintf('The service "%s" must implement the SubscribingHandlerInterface.'$id));
  61.             }
  62.             foreach (call_user_func([$class'getSubscribingMethods']) as $methodData) {
  63.                 if (!isset($methodData['format'], $methodData['type'])) {
  64.                     throw new \RuntimeException(sprintf('Each method returned from getSubscribingMethods of service "%s" must have a "type", and "format" attribute.'$id));
  65.                 }
  66.                 $directions = [GraphNavigatorInterface::DIRECTION_DESERIALIZATIONGraphNavigatorInterface::DIRECTION_SERIALIZATION];
  67.                 if (isset($methodData['direction'])) {
  68.                     $directions = [$methodData['direction']];
  69.                 }
  70.                 foreach ($directions as $direction) {
  71.                     $priority = isset($methodData['priority']) ? intval($methodData['priority']) : 0;
  72.                     $method $methodData['method'] ?? HandlerRegistry::getDefaultMethod($direction$methodData['type'], $methodData['format']);
  73.                     $handlers[] = [$direction$methodData['type'], $methodData['format'], $priority, new Reference($id), $method];
  74.                 }
  75.             }
  76.         }
  77.         return $this->sortAndFlattenHandlersList($handlers);
  78.     }
  79.     private function sortAndFlattenHandlersList(array $allHandlers)
  80.     {
  81.         $sorter = static function ($a$b) {
  82.             return $b[3] === $a[3] ? : ($b[3] > $a[3] ? : -1);
  83.         };
  84.         self::stable_uasort($allHandlers$sorter);
  85.         $handlers = [];
  86.         foreach ($allHandlers as $handler) {
  87.             [$direction$type$format$priority$service$method] = $handler;
  88.             $handlers[$direction][$type][$format] = [$service$method];
  89.         }
  90.         return $handlers;
  91.     }
  92.     /**
  93.      * Performs stable sorting. Copied from http://php.net/manual/en/function.uasort.php#121283
  94.      *
  95.      * @param array $array
  96.      * @param callable $value_compare_func
  97.      *
  98.      * @return bool
  99.      */
  100.     private static function stable_uasort(array &$array, callable $value_compare_func)
  101.     {
  102.         $index 0;
  103.         foreach ($array as &$item) {
  104.             $item = [$index++, $item];
  105.         }
  106.         $result uasort($array, static function ($a$b) use ($value_compare_func) {
  107.             $result call_user_func($value_compare_func$a[1], $b[1]);
  108.             return === $result $a[0] - $b[0] : $result;
  109.         });
  110.         foreach ($array as &$item) {
  111.             $item $item[1];
  112.         }
  113.         return $result;
  114.     }
  115. }