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