-
Notifications
You must be signed in to change notification settings - Fork 351
/
EntityAliasesConfigurationCompilerPass.php
75 lines (65 loc) · 2.43 KB
/
EntityAliasesConfigurationCompilerPass.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Oro\Bundle\ApiBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class EntityAliasesConfigurationCompilerPass implements CompilerPassInterface
{
const LOADER_SERVICE = 'oro_api.entity_alias_loader';
const ALIAS_PROVIDER_TAG_NAME = 'oro_entity.alias_provider';
const CLASS_PROVIDER_TAG_NAME = 'oro_entity.class_provider';
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(self::LOADER_SERVICE)) {
return;
}
// find providers
$classProviders = $this->getClassProviders($container);
$aliasProviders = $this->getAliasProviders($container);
// register
$resolverDef = $container->getDefinition(self::LOADER_SERVICE);
foreach ($classProviders as $classProvider) {
$resolverDef->addMethodCall('addEntityClassProvider', [$classProvider]);
}
foreach ($aliasProviders as $aliasProvider) {
$resolverDef->addMethodCall('addEntityAliasProvider', [$aliasProvider]);
}
}
/**
* @param ContainerBuilder $container
*
* @return string[]
*/
protected function getClassProviders(ContainerBuilder $container)
{
$classProviders = [];
$taggedServices = $container->findTaggedServiceIds(self::CLASS_PROVIDER_TAG_NAME);
foreach ($taggedServices as $id => $attributes) {
$classProviders[] = new Reference($id);
}
return $classProviders;
}
/**
* @param ContainerBuilder $container
*
* @return string[]
*/
protected function getAliasProviders(ContainerBuilder $container)
{
$aliasProviders = [];
$taggedServices = $container->findTaggedServiceIds(self::ALIAS_PROVIDER_TAG_NAME);
foreach ($taggedServices as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$aliasProviders[$priority][] = new Reference($id);
}
if (!empty($aliasProviders)) {
// sort by priority and flatten
krsort($aliasProviders);
$aliasProviders = call_user_func_array('array_merge', $aliasProviders);
}
return $aliasProviders;
}
}