class RegisterSerializationClassesCompilerPass in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php \Drupal\serialization\RegisterSerializationClassesCompilerPass
Adds services tagged 'normalizer' and 'encoder' to the Serializer.
Hierarchy
- class \Drupal\serialization\RegisterSerializationClassesCompilerPass implements CompilerPassInterface
Expanded class hierarchy of RegisterSerializationClassesCompilerPass
File
- core/modules/ serialization/ src/ RegisterSerializationClassesCompilerPass.php, line 17 
- Contains \Drupal\serialization\RegisterSerializationClassesCompilerPass.
Namespace
Drupal\serializationView source
class RegisterSerializationClassesCompilerPass implements CompilerPassInterface {
  /**
   * Adds services to the Serializer.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
   *   The container to process.
   */
  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));
    }
    // Find all serialization formats known.
    $formats = array();
    foreach ($container
      ->findTaggedServiceIds('encoder') as $attributes) {
      $formats[] = $attributes[0]['format'];
    }
    $container
      ->setParameter('serializer.formats', $formats);
  }
  /**
   * Sorts by priority.
   *
   * Order services from highest priority number to lowest (reverse sorting).
   *
   * @param array $services
   *   A nested array keyed on priority number. For each priority number, the
   *   value is an array of Symfony\Component\DependencyInjection\Reference
   *   objects, each a reference to a normalizer or encoder service.
   *
   * @return array
   *   A flattened array of Reference objects from $services, ordered from high
   *   to low priority.
   */
  protected function sort($services) {
    $sorted = array();
    krsort($services);
    // Flatten the array.
    foreach ($services as $a) {
      $sorted = array_merge($sorted, $a);
    }
    return $sorted;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| RegisterSerializationClassesCompilerPass:: | public | function | Adds services to the Serializer. Overrides CompilerPassInterface:: | |
| RegisterSerializationClassesCompilerPass:: | protected | function | Sorts by priority. | 
