|
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
|
|
|
{ |
|
46
|
|
|
$services = $container->findTaggedServiceIds('api_platform.'.$type.'_data_provider'); |
|
47
|
|
|
|
|
48
|
|
|
$queue = new \SplPriorityQueue(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($services as $serviceId => $tags) { |
|
51
|
|
|
foreach ($tags as $attributes) { |
|
52
|
|
|
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0; |
|
53
|
|
|
$queue->insert(new Reference($serviceId), $priority); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$container->getDefinition('api_platform.'.$type.'_data_provider')->addArgument(iterator_to_array($queue, false)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|