Completed
Pull Request — master (#578)
by Kévin
04:08
created

DataProviderPass::registerDataProviders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
/**
19
 * Registers data providers.
20
 *
21
 * @internal
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 */
25
final class DataProviderPass implements CompilerPassInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(ContainerBuilder $container)
31
    {
32
        $this->registerDataProviders($container, 'collection');
33
        $this->registerDataProviders($container, 'item');
34
    }
35
36
    /**
37
     * The priority sorting algorithm has been backported from Symfony 3.2.
38
     *
39
     * @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php
40
     *
41
     * @param ContainerBuilder $container
42
     * @param string           $type
43
     */
44
    private function registerDataProviders(ContainerBuilder $container, string $type) {
45
        $services = $container->findTaggedServiceIds('api_platform.'.$type.'_data_provider');
46
47
        $queue = new \SplPriorityQueue();
48
49
        foreach ($services as $serviceId => $tags) {
50
            foreach ($tags as $attributes) {
51
                $priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
52
                $queue->insert(new Reference($serviceId), $priority);
53
            }
54
        }
55
56
        $container->getDefinition('api_platform.'.$type.'_data_provider')->addArgument(iterator_to_array($queue, false));
57
    }
58
}
59