class RegisterSerializationClassesCompilerPass

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

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

Services tagged with 'jsonapi_normalizer' will be added to the JSON:API serializer. No extensions can provide such services.

JSON:API does respect generic (non-JSON:API) DataType-level normalizers.

@internal JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.

Hierarchy

Expanded class hierarchy of RegisterSerializationClassesCompilerPass

See also

https://www.drupal.org/project/drupal/issues/3032787

jsonapi.api.php

1 file declares its use of RegisterSerializationClassesCompilerPass
JsonapiServiceProvider.php in core/modules/jsonapi/src/JsonapiServiceProvider.php

File

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

Namespace

Drupal\jsonapi\DependencyInjection\Compiler
View source
class RegisterSerializationClassesCompilerPass extends DrupalRegisterSerializationClassesCompilerPass {
    
    /**
     * The service ID.
     *
     * @const string
     */
    const OVERRIDDEN_SERVICE_ID = 'jsonapi.serializer';
    
    /**
     * The service tag that only JSON:API normalizers should use.
     *
     * @const string
     */
    const OVERRIDDEN_SERVICE_NORMALIZER_TAG = 'jsonapi_normalizer';
    
    /**
     * The service tag that only JSON:API encoders should use.
     *
     * @const string
     */
    const OVERRIDDEN_SERVICE_ENCODER_TAG = 'jsonapi_encoder';
    
    /**
     * The ID for the JSON:API format.
     *
     * @const string
     */
    const FORMAT = 'api_json';
    
    /**
     * 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'
     *
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
     *   The container to process.
     */
    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));
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
RegisterSerializationClassesCompilerPass::FORMAT constant The ID for the JSON:API format.
RegisterSerializationClassesCompilerPass::OVERRIDDEN_SERVICE_ENCODER_TAG constant The service tag that only JSON:API encoders should use.
RegisterSerializationClassesCompilerPass::OVERRIDDEN_SERVICE_ID constant The service ID.
RegisterSerializationClassesCompilerPass::OVERRIDDEN_SERVICE_NORMALIZER_TAG constant The service tag that only JSON:API normalizers should use.
RegisterSerializationClassesCompilerPass::process public function Adds services to the JSON:API Serializer. Overrides RegisterSerializationClassesCompilerPass::process
RegisterSerializationClassesCompilerPass::sort protected function Sorts by priority.

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