RegisterSerializerCompilerPass.php in Replication 8.2
File
src/RegisterSerializerCompilerPass.php
View source
<?php
namespace Drupal\replication;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterSerializerCompilerPass implements CompilerPassInterface {
const SERIALIZER_SERVICE_NAME = 'replication.serializer';
public function process(ContainerBuilder $container) {
$definition = $container
->getDefinition(static::SERIALIZER_SERVICE_NAME);
$formats = [];
$format_providers = [];
foreach ($container
->findTaggedServiceIds('replication_normalizer') as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$normalizers[$priority][] = new Reference($id);
$normalizers_id[$priority][] = $id;
}
foreach ($container
->findTaggedServiceIds('replication_encoder') as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$encoders[$priority][] = new Reference($id);
$format = $attributes[0]['format'];
$formats[] = $format;
if ($provider_tag = $container
->getDefinition($id)
->getTag('_provider')) {
$format_providers[$format] = $provider_tag[0]['provider'];
}
}
if (!empty($normalizers)) {
$definition
->replaceArgument(0, static::flattenAndSort($normalizers));
}
if (!empty($encoders)) {
$definition
->replaceArgument(1, static::flattenAndSort($encoders));
}
$container
->setParameter(sprintf('%s.formats', static::SERIALIZER_SERVICE_NAME), $formats);
$container
->setParameter(sprintf('%s.format_providers', static::SERIALIZER_SERVICE_NAME), $format_providers);
}
protected static function flattenAndSort($services) {
$sorted = [];
krsort($services);
foreach ($services as $a) {
$sorted = array_merge($sorted, $a);
}
return $sorted;
}
}