Completed
Push — master ( c62646...583a7d )
by Eric
02:58
created

RegisterClassMetadataLoaderPass::process()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer bundle package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\SerializerBundle\DependencyInjection\Compiler;
13
14
use Ivory\Serializer\Mapping\Loader\ChainClassMetadataLoader;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
use Symfony\Component\DependencyInjection\Definition;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class RegisterClassMetadataLoaderPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        $loaders = array_keys($container->findTaggedServiceIds($tag = 'ivory.serializer.loader'));
31
32
        if (empty($loaders)) {
33
            throw new \RuntimeException(sprintf(
34
                'You must define at least one class metadata loader by enabling the reflection loader in your '.
35
                'configuration or by registering a loader in the container with the tag "%s".',
36
                $tag
37
            ));
38
        }
39
40
        $loader = 'ivory.serializer.mapping.loader';
41
42
        if (count($loaders) > 1) {
43
            $container->setDefinition($loader, new Definition(ChainClassMetadataLoader::class, [
44
                array_map(function ($service) {
45
                    return new Reference($service);
46
                }, $loaders),
47
                new Reference('ivory.serializer.type.parser'),
48
            ]));
49
        } else {
50
            $container->setAlias($loader, array_shift($loaders));
51
        }
52
    }
53
}
54