function RegisterSerializationClassesCompilerPass::process

Same name in this branch
  1. 11.x core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php \Drupal\serialization\RegisterSerializationClassesCompilerPass::process()
Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/DependencyInjection/Compiler/RegisterSerializationClassesCompilerPass.php \Drupal\jsonapi\DependencyInjection\Compiler\RegisterSerializationClassesCompilerPass::process()
  2. 9 core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php \Drupal\serialization\RegisterSerializationClassesCompilerPass::process()
  3. 8.9.x core/modules/jsonapi/src/DependencyInjection/Compiler/RegisterSerializationClassesCompilerPass.php \Drupal\jsonapi\DependencyInjection\Compiler\RegisterSerializationClassesCompilerPass::process()
  4. 8.9.x core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php \Drupal\serialization\RegisterSerializationClassesCompilerPass::process()
  5. 10 core/modules/jsonapi/src/DependencyInjection/Compiler/RegisterSerializationClassesCompilerPass.php \Drupal\jsonapi\DependencyInjection\Compiler\RegisterSerializationClassesCompilerPass::process()
  6. 10 core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php \Drupal\serialization\RegisterSerializationClassesCompilerPass::process()

Adds services to the JSON:API Serializer.

This code is copied from the class parent with two modifications. The service id has been changed and the service tag has been updated.

ID: 'serializer' -> 'jsonapi.serializer' Tag: 'normalizer' -> 'jsonapi_normalizer'

Parameters

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

Overrides RegisterSerializationClassesCompilerPass::process

File

core/modules/jsonapi/src/DependencyInjection/Compiler/RegisterSerializationClassesCompilerPass.php, line 65

Class

RegisterSerializationClassesCompilerPass
Adds services tagged JSON:API-only normalizers to the Serializer.

Namespace

Drupal\jsonapi\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) : void {
    $definition = $container->getDefinition(static::OVERRIDDEN_SERVICE_ID);
    // Retrieve registered Normalizers and Encoders from the container.
    foreach ($container->findTaggedServiceIds(static::OVERRIDDEN_SERVICE_NORMALIZER_TAG) as $id => $attributes) {
        // Normalizers are not an API: mark private.
        $container->getDefinition($id)
            ->setPublic(FALSE);
        $priority = $attributes[0]['priority'] ?? 0;
        $normalizers[$priority][] = new Reference($id);
    }
    foreach ($container->findTaggedServiceIds(static::OVERRIDDEN_SERVICE_ENCODER_TAG) as $id => $attributes) {
        // Encoders are not an API: mark private.
        $container->getDefinition($id)
            ->setPublic(FALSE);
        $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));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.