Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\Serializer\DependencyInjection; Chris@14: Chris@17: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@14: use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; Chris@14: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@14: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@14: Chris@14: /** Chris@14: * Adds all services with the tags "serializer.encoder" and "serializer.normalizer" as Chris@14: * encoders and normalizers to the "serializer" service. Chris@14: * Chris@14: * @author Javier Lopez Chris@14: * @author Robin Chalas Chris@14: */ Chris@14: class SerializerPass implements CompilerPassInterface Chris@14: { Chris@14: use PriorityTaggedServiceTrait; Chris@14: Chris@14: private $serializerService; Chris@14: private $normalizerTag; Chris@14: private $encoderTag; Chris@14: Chris@14: public function __construct($serializerService = 'serializer', $normalizerTag = 'serializer.normalizer', $encoderTag = 'serializer.encoder') Chris@14: { Chris@14: $this->serializerService = $serializerService; Chris@14: $this->normalizerTag = $normalizerTag; Chris@14: $this->encoderTag = $encoderTag; Chris@14: } Chris@14: Chris@14: public function process(ContainerBuilder $container) Chris@14: { Chris@14: if (!$container->hasDefinition($this->serializerService)) { Chris@14: return; Chris@14: } Chris@14: Chris@14: if (!$normalizers = $this->findAndSortTaggedServices($this->normalizerTag, $container)) { Chris@14: throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->normalizerTag, $this->serializerService)); Chris@14: } Chris@14: Chris@14: $serializerDefinition = $container->getDefinition($this->serializerService); Chris@14: $serializerDefinition->replaceArgument(0, $normalizers); Chris@14: Chris@14: if (!$encoders = $this->findAndSortTaggedServices($this->encoderTag, $container)) { Chris@14: throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->encoderTag, $this->serializerService)); Chris@14: } Chris@14: Chris@14: $serializerDefinition->replaceArgument(1, $encoders); Chris@14: } Chris@14: }