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 // If there is a BC key present, pass this to determine if the normalizer
28 if (isset($attributes[0]['bc']) && $this->normalizerBcSettingIsEnabled($attributes[0]['bc'], $attributes[0]['bc_config_name'])) {
32 $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
33 $normalizers[$priority][] = new Reference($id);
35 foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
36 $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
37 $encoders[$priority][] = new Reference($id);
40 // Add the registered Normalizers and Encoders to the Serializer.
41 if (!empty($normalizers)) {
42 $definition->replaceArgument(0, $this->sort($normalizers));
44 if (!empty($encoders)) {
45 $definition->replaceArgument(1, $this->sort($encoders));
48 // Find all serialization formats known.
50 $format_providers = [];
51 foreach ($container->findTaggedServiceIds('encoder') as $service_id => $attributes) {
52 $format = $attributes[0]['format'];
55 if ($provider_tag = $container->getDefinition($service_id)->getTag('_provider')) {
56 $format_providers[$format] = $provider_tag[0]['provider'];
59 $container->setParameter('serializer.formats', $formats);
60 $container->setParameter('serializer.format_providers', $format_providers);
64 * Returns whether a normalizer BC setting is disabled or not.
70 protected function normalizerBcSettingIsEnabled($key, $config_name) {
71 $settings = BootstrapConfigStorageFactory::get()->read($config_name);
72 return !empty($settings[$key]);
78 * Order services from highest priority number to lowest (reverse sorting).
80 * @param array $services
81 * A nested array keyed on priority number. For each priority number, the
82 * value is an array of Symfony\Component\DependencyInjection\Reference
83 * objects, each a reference to a normalizer or encoder service.
86 * A flattened array of Reference objects from $services, ordered from high
89 protected function sort($services) {
94 foreach ($services as $a) {
95 $sorted = array_merge($sorted, $a);