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
|
|
|
* Injects query extensions. |
20
|
|
|
* |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @author Samuel ROZE <[email protected]> |
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class DoctrineQueryExtensionPass implements CompilerPassInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function process(ContainerBuilder $container) |
32
|
|
|
{ |
33
|
|
|
if ($container->hasDefinition('api_platform.doctrine.orm.collection_data_provider')) { |
34
|
|
|
$this->handleOrm($container); |
35
|
|
|
} elseif($container->hasDefinition('api_platform.doctrine.mongodb.collection_data_provider')) { |
36
|
|
|
$this->handleMongoDB($container); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Finds services having the given tag and sorts them by their priority attribute. |
42
|
|
|
* |
43
|
|
|
* @param ContainerBuilder $container |
44
|
|
|
* @param string $tag |
45
|
|
|
* |
46
|
|
|
* @return Reference[] |
47
|
|
|
*/ |
48
|
|
|
private function findSortedServices(ContainerBuilder $container, $tag) |
49
|
|
|
{ |
50
|
|
|
$extensions = []; |
51
|
|
|
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tags) { |
52
|
|
|
foreach ($tags as $tag) { |
53
|
|
|
$priority = isset($tag['priority']) ? $tag['priority'] : 0; |
54
|
|
|
$extensions[$priority][] = new Reference($serviceId); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
krsort($extensions); |
58
|
|
|
|
59
|
|
|
// Flatten the array |
60
|
|
|
return empty($extensions) ? [] : call_user_func_array('array_merge', $extensions); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function handleOrm(ContainerBuilder $container) |
64
|
|
|
{ |
65
|
|
|
$collectionDataProviderDefinition = $container->getDefinition('api_platform.doctrine.orm.collection_data_provider'); |
66
|
|
|
$itemDataProviderDefinition = $container->getDefinition('api_platform.doctrine.orm.item_data_provider'); |
67
|
|
|
|
68
|
|
|
$collectionDataProviderDefinition->replaceArgument(1, $this->findSortedServices($container, 'api_platform.doctrine.orm.query_extension.collection')); |
69
|
|
|
$itemDataProviderDefinition->replaceArgument(3, $this->findSortedServices($container, 'api_platform.doctrine.orm.query_extension.item')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function handleMongoDB(ContainerBuilder $container) |
73
|
|
|
{ |
74
|
|
|
$collectionDataProviderDefinition = $container->getDefinition('api_platform.doctrine.mongodb.collection_data_provider'); |
75
|
|
|
$itemDataProviderDefinition = $container->getDefinition('api_platform.doctrine.mongodb.item_data_provider'); |
76
|
|
|
|
77
|
|
|
$collectionDataProviderDefinition->replaceArgument(1, $this->findSortedServices($container, 'api_platform.doctrine.mongodb.query_extension.collection')); |
78
|
|
|
$itemDataProviderDefinition->replaceArgument(3, $this->findSortedServices($container, 'api_platform.doctrine.mongodb.query_extension.item')); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|