public function RegisterSerializationClassesPass::process

Adds services to the Serializer.

Parameters

\Symfony\Component\DependencyInjection\ContainerBuilder $container: The container to process.

Overrides CompilerPassInterface::process

File

drupal/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterSerializationClassesPass.php, line 25
Contains Drupal\serialization\RegisterSerializationClassesPass.

Class

RegisterSerializationClassesPass
Adds services tagged 'normalizer' and 'encoder' to the Serializer.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) {
  $definition = $container
    ->getDefinition('serializer');

  // Retrieve registered Normalizers and Encoders from the container.
  foreach ($container
    ->findTaggedServiceIds('normalizer') as $id => $attributes) {
    $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
    $normalizers[$priority][] = new Reference($id);
  }
  foreach ($container
    ->findTaggedServiceIds('encoder') as $id => $attributes) {
    $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
    $encoders[$priority][] = new Reference($id);
  }

  // Add the registered Normalizers and Encoders to the Serializer.
  if (!empty($normalizers)) {
    $definition
      ->replaceArgument(0, $this
      ->sort($normalizers));
  }
  if (!empty($encoders)) {
    $definition
      ->replaceArgument(1, $this
      ->sort($encoders));
  }
}