RegisterSerializationClassesCompilerPass.php in Drupal 10        
                          
                  
                        
  
  
  
  
File
  core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php
  
    View source  
  <?php
namespace Drupal\serialization;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterSerializationClassesCompilerPass implements CompilerPassInterface {
  
  public function process(ContainerBuilder $container) {
    $definition = $container
      ->getDefinition('serializer');
    
    foreach ($container
      ->findTaggedServiceIds('normalizer') as $id => $attributes) {
      
      $container
        ->getDefinition($id)
        ->setPublic(FALSE);
      $priority = $attributes[0]['priority'] ?? 0;
      $normalizers[$priority][] = new Reference($id);
    }
    foreach ($container
      ->findTaggedServiceIds('encoder') as $id => $attributes) {
      
      $container
        ->getDefinition($id)
        ->setPublic(FALSE);
      $priority = $attributes[0]['priority'] ?? 0;
      $encoders[$priority][] = new Reference($id);
    }
    
    if (!empty($normalizers)) {
      $definition
        ->replaceArgument(0, $this
        ->sort($normalizers));
    }
    if (!empty($encoders)) {
      $definition
        ->replaceArgument(1, $this
        ->sort($encoders));
    }
    
    $formats = [];
    $format_providers = [];
    foreach ($container
      ->findTaggedServiceIds('encoder') as $service_id => $attributes) {
      $format = $attributes[0]['format'];
      $formats[] = $format;
      if ($provider_tag = $container
        ->getDefinition($service_id)
        ->getTag('_provider')) {
        $format_providers[$format] = $provider_tag[0]['provider'];
      }
    }
    $container
      ->setParameter('serializer.formats', $formats);
    $container
      ->setParameter('serializer.format_providers', $format_providers);
  }
  
  protected function sort($services) {
    krsort($services);
    return array_merge([], ...$services);
  }
}