|
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 doctrine not loaded |
|
34
|
|
|
if (!$container->hasDefinition('api_platform.doctrine.metadata_factory')) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($container->hasDefinition('api_platform.doctrine.orm.collection_data_provider')) { |
|
39
|
|
|
$this->handleOrm($container); |
|
40
|
|
|
} elseif ($container->hasDefinition('api_platform.doctrine.mongodb.collection_data_provider')) { |
|
41
|
|
|
$this->handleMongoDB($container); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Finds services having the given tag and sorts them by their priority attribute. |
|
47
|
|
|
* |
|
48
|
|
|
* @param ContainerBuilder $container |
|
49
|
|
|
* @param string $tag |
|
50
|
|
|
* |
|
51
|
|
|
* @return Reference[] |
|
52
|
|
|
*/ |
|
53
|
|
|
private function findSortedServices(ContainerBuilder $container, $tag) |
|
54
|
|
|
{ |
|
55
|
|
|
$extensions = []; |
|
56
|
|
|
foreach ($container->findTaggedServiceIds($tag) as $serviceId => $tags) { |
|
57
|
|
|
foreach ($tags as $tag) { |
|
58
|
|
|
$priority = isset($tag['priority']) ? $tag['priority'] : 0; |
|
59
|
|
|
$extensions[$priority][] = new Reference($serviceId); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
krsort($extensions); |
|
63
|
|
|
|
|
64
|
|
|
// Flatten the array |
|
65
|
|
|
return empty($extensions) ? [] : call_user_func_array('array_merge', $extensions); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function handleOrm(ContainerBuilder $container) |
|
69
|
|
|
{ |
|
70
|
|
|
$collectionDataProviderDefinition = $container->getDefinition('api_platform.doctrine.orm.collection_data_provider'); |
|
71
|
|
|
$itemDataProviderDefinition = $container->getDefinition('api_platform.doctrine.orm.item_data_provider'); |
|
72
|
|
|
|
|
73
|
|
|
$collectionDataProviderDefinition->replaceArgument(1, $this->findSortedServices($container, 'api_platform.doctrine.orm.query_extension.collection')); |
|
74
|
|
|
$itemDataProviderDefinition->replaceArgument(3, $this->findSortedServices($container, 'api_platform.doctrine.orm.query_extension.item')); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function handleMongoDB(ContainerBuilder $container) |
|
78
|
|
|
{ |
|
79
|
|
|
$collectionDataProviderDefinition = $container->getDefinition('api_platform.doctrine.mongodb.collection_data_provider'); |
|
80
|
|
|
$itemDataProviderDefinition = $container->getDefinition('api_platform.doctrine.mongodb.item_data_provider'); |
|
81
|
|
|
|
|
82
|
|
|
$collectionDataProviderDefinition->replaceArgument(1, $this->findSortedServices($container, 'api_platform.doctrine.mongodb.query_extension.collection')); |
|
83
|
|
|
$itemDataProviderDefinition->replaceArgument(3, $this->findSortedServices($container, 'api_platform.doctrine.mongodb.query_extension.item')); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|