1<?php
2
3namespace JMS\SerializerBundle;
4
5use JMS\DiExtraBundle\DependencyInjection\Compiler\LazyServiceMapPass;
6use JMS\SerializerBundle\DependencyInjection\Compiler\CustomHandlersPass;
7use JMS\SerializerBundle\DependencyInjection\Compiler\DoctrinePass;
8use JMS\SerializerBundle\DependencyInjection\Compiler\FormErrorHandlerTranslationDomainPass;
9use JMS\SerializerBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
10use JMS\SerializerBundle\DependencyInjection\Compiler\ServiceMapPass;
11use JMS\SerializerBundle\DependencyInjection\Compiler\TwigExtensionPass;
12use Symfony\Component\DependencyInjection\Compiler\PassConfig;
13use Symfony\Component\DependencyInjection\ContainerBuilder;
14use Symfony\Component\DependencyInjection\Definition;
15use Symfony\Component\HttpKernel\Bundle\Bundle;
16
17class JMSSerializerBundle extends Bundle
18{
19    public function build(ContainerBuilder $builder)
20    {
21        $builder->addCompilerPass($this->getServiceMapPass('jms_serializer.serialization_visitor', 'format',
22            function (ContainerBuilder $container, Definition $def) {
23                $container->getDefinition('jms_serializer.serializer')->replaceArgument(3, $def);
24            }
25        ));
26        $builder->addCompilerPass($this->getServiceMapPass('jms_serializer.deserialization_visitor', 'format',
27            function (ContainerBuilder $container, Definition $def) {
28                $container->getDefinition('jms_serializer.serializer')->replaceArgument(4, $def);
29            }
30        ));
31
32        $builder->addCompilerPass(new FormErrorHandlerTranslationDomainPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
33        $builder->addCompilerPass(new TwigExtensionPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
34        $builder->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_REMOVING);
35        $builder->addCompilerPass(new CustomHandlersPass(), PassConfig::TYPE_BEFORE_REMOVING);
36        $builder->addCompilerPass(new DoctrinePass(), PassConfig::TYPE_BEFORE_REMOVING);
37    }
38
39    private function getServiceMapPass($tagName, $keyAttributeName, $callable)
40    {
41        if (class_exists('JMS\DiExtraBundle\DependencyInjection\Compiler\LazyServiceMapPass')) {
42            return new LazyServiceMapPass($tagName, $keyAttributeName, $callable);
43        }
44
45        return new ServiceMapPass($tagName, $keyAttributeName, $callable);
46    }
47}
48