3 namespace Drupal\serialization;
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;
11 * Adds services tagged 'normalizer' and 'encoder' to the Serializer.
13 class RegisterSerializationClassesCompilerPass implements CompilerPassInterface {
16 * Adds services to the Serializer.
18 * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
19 * The container to process.
21 public function process(ContainerBuilder $container) {
22 $definition = $container->getDefinition('serializer');
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);
29 // If there is a BC key present, pass this to determine if the normalizer
31 if (isset($attributes[0]['bc']) && $this->normalizerBcSettingIsEnabled($attributes[0]['bc'], $attributes[0]['bc_config_name'])) {
35 $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
36 $normalizers[$priority][] = new Reference($id);
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);
42 $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
43 $encoders[$priority][] = new Reference($id);
46 // Add the registered Normalizers and Encoders to the Serializer.
47 if (!empty($normalizers)) {
48 $definition->replaceArgument(0, $this->sort($normalizers));
50 if (!empty($encoders)) {
51 $definition->replaceArgument(1, $this->sort($encoders));
54 // Find all serialization formats known.
56 $format_providers = [];
57 foreach ($container->findTaggedServiceIds('encoder') as $service_id => $attributes) {
58 $format = $attributes[0]['format'];
61 if ($provider_tag = $container->getDefinition($service_id)->getTag('_provider')) {
62 $format_providers[$format] = $provider_tag[0]['provider'];
65 $container->setParameter('serializer.formats', $formats);
66 $container->setParameter('serializer.format_providers', $format_providers);
70 * Returns whether a normalizer BC setting is disabled or not.
76 protected function normalizerBcSettingIsEnabled($key, $config_name) {
77 $settings = BootstrapConfigStorageFactory::get()->read($config_name);
78 return !empty($settings[$key]);
84 * Order services from highest priority number to lowest (reverse sorting).
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.
92 * A flattened array of Reference objects from $services, ordered from high
95 protected function sort($services) {
100 foreach ($services as $a) {
101 $sorted = array_merge($sorted, $a);