summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/serialization/src/RegisterSerializationClassesCompilerPass.php
blob: 86e3b5541fc60696b6c8d677836ac7847d38669c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

namespace Drupal\serialization;

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
 * Adds services tagged 'normalizer' and 'encoder' to the Serializer.
 */
class RegisterSerializationClassesCompilerPass implements CompilerPassInterface {

  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container): void {
    $definition = $container->getDefinition('serializer');

    // Retrieve registered Normalizers and Encoders from the container.
    foreach ($container->findTaggedServiceIds('normalizer') as $id => $attributes) {
      // The 'serializer' service is the public API: mark normalizers private.
      $container->getDefinition($id)->setPublic(FALSE);

      $priority = $attributes[0]['priority'] ?? 0;
      $normalizers[$priority][] = new Reference($id);
    }
    foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
      // The 'serializer' service is the public API: mark encoders 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));
    }

    // Find all serialization formats known.
    $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);
  }

  /**
   * 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) {
    krsort($services);
    return array_merge(...$services);
  }

}