Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / serialization / src / RegisterSerializationClassesCompilerPass.php
1 <?php
2
3 namespace Drupal\serialization;
4
5 use Drupal\Core\Config\BootstrapConfigStorageFactory;
6 use Symfony\Component\DependencyInjection\Reference;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
10 /**
11  * Adds services tagged 'normalizer' and 'encoder' to the Serializer.
12  */
13 class RegisterSerializationClassesCompilerPass implements CompilerPassInterface {
14
15   /**
16    * Adds services to the Serializer.
17    *
18    * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
19    *   The container to process.
20    */
21   public function process(ContainerBuilder $container) {
22     $definition = $container->getDefinition('serializer');
23
24     // Retrieve registered Normalizers and Encoders from the container.
25     foreach ($container->findTaggedServiceIds('normalizer') as $id => $attributes) {
26       // The 'serializer' service is the public API: mark normalizers private.
27       $container->getDefinition($id)->setPublic(FALSE);
28
29       // If there is a BC key present, pass this to determine if the normalizer
30       // should be skipped.
31       if (isset($attributes[0]['bc']) && $this->normalizerBcSettingIsEnabled($attributes[0]['bc'], $attributes[0]['bc_config_name'])) {
32         continue;
33       }
34
35       $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
36       $normalizers[$priority][] = new Reference($id);
37     }
38     foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
39       // The 'serializer' service is the public API: mark encoders private.
40       $container->getDefinition($id)->setPublic(FALSE);
41
42       $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
43       $encoders[$priority][] = new Reference($id);
44     }
45
46     // Add the registered Normalizers and Encoders to the Serializer.
47     if (!empty($normalizers)) {
48       $definition->replaceArgument(0, $this->sort($normalizers));
49     }
50     if (!empty($encoders)) {
51       $definition->replaceArgument(1, $this->sort($encoders));
52     }
53
54     // Find all serialization formats known.
55     $formats = [];
56     $format_providers = [];
57     foreach ($container->findTaggedServiceIds('encoder') as $service_id => $attributes) {
58       $format = $attributes[0]['format'];
59       $formats[] = $format;
60
61       if ($provider_tag = $container->getDefinition($service_id)->getTag('_provider')) {
62         $format_providers[$format] = $provider_tag[0]['provider'];
63       }
64     }
65     $container->setParameter('serializer.formats', $formats);
66     $container->setParameter('serializer.format_providers', $format_providers);
67   }
68
69   /**
70    * Returns whether a normalizer BC setting is disabled or not.
71    *
72    * @param string $key
73    *
74    * @return bool
75    */
76   protected function normalizerBcSettingIsEnabled($key, $config_name) {
77     $settings = BootstrapConfigStorageFactory::get()->read($config_name);
78     return !empty($settings[$key]);
79   }
80
81   /**
82    * Sorts by priority.
83    *
84    * Order services from highest priority number to lowest (reverse sorting).
85    *
86    * @param array $services
87    *   A nested array keyed on priority number. For each priority number, the
88    *   value is an array of Symfony\Component\DependencyInjection\Reference
89    *   objects, each a reference to a normalizer or encoder service.
90    *
91    * @return array
92    *   A flattened array of Reference objects from $services, ordered from high
93    *   to low priority.
94    */
95   protected function sort($services) {
96     $sorted = [];
97     krsort($services);
98
99     // Flatten the array.
100     foreach ($services as $a) {
101       $sorted = array_merge($sorted, $a);
102     }
103
104     return $sorted;
105   }
106
107 }