|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File part of the Redmine User Provider bundle |
|
4
|
|
|
* |
|
5
|
|
|
* @category SymfonyBundle |
|
6
|
|
|
* @package GMaissa.RedmineUserProviderBundle |
|
7
|
|
|
* @author Guillaume Maïssa <[email protected]> |
|
8
|
|
|
* @copyright 2017 Guillaume Maïssa |
|
9
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace GMaissa\RedmineUserProviderBundle\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
|
|
|
* User provider compiler pass |
|
20
|
|
|
* |
|
21
|
|
|
* Inject User repository in user provider if declared |
|
22
|
|
|
*/ |
|
23
|
|
|
class UserProviderCompilerPass implements CompilerPassInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
5 |
|
public function process(ContainerBuilder $container) |
|
29
|
|
|
{ |
|
30
|
|
|
$defaultDependencies = [ |
|
31
|
|
|
[ |
|
32
|
|
|
'serviceId' => 'gm_redmine_user_provider.api.client', |
|
33
|
|
|
'defaultServiceId' => 'gm_redmine_user_provider.api.client.default' |
|
34
|
5 |
|
], |
|
35
|
|
|
[ |
|
36
|
|
|
'serviceId' => 'gm_redmine_user_provider.factory.user', |
|
37
|
|
|
'defaultServiceId' => 'gm_redmine_user_provider.factory.user.default' |
|
38
|
|
|
] |
|
39
|
|
|
]; |
|
40
|
5 |
|
foreach ($defaultDependencies as $dependency) { |
|
41
|
5 |
|
if (!$container->has($dependency['serviceId'])) { |
|
42
|
5 |
|
$container->setAlias($dependency['serviceId'], $dependency['defaultServiceId']); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
5 |
|
$this->processRepository($container); |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Look for configured user repository, to inject it into user provider service |
|
51
|
|
|
* |
|
52
|
|
|
* @param ContainerBuilder $container |
|
53
|
|
|
*/ |
|
54
|
5 |
|
private function processRepository(ContainerBuilder $container): void |
|
55
|
|
|
{ |
|
56
|
5 |
|
$repositories = []; |
|
57
|
5 |
|
foreach (array_keys($container->findTaggedServiceIds('gm_redmine_user_provider.user_repository')) as $id) { |
|
58
|
4 |
|
$repositories[] = $id; |
|
59
|
|
|
} |
|
60
|
5 |
|
if (count($repositories) > 1) { |
|
61
|
1 |
|
throw new \InvalidArgumentException( |
|
62
|
|
|
sprintf( |
|
63
|
1 |
|
'You cannot have multiple services tagged as "%s"', |
|
64
|
1 |
|
'gm_redmine_user_provider.user_repository' |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
4 |
|
if (count($repositories) == 1) { |
|
69
|
3 |
|
$repositoryId = $repositories[0]; |
|
70
|
3 |
|
$this->checkRepositoryValidity($container, $repositoryId); |
|
71
|
|
|
|
|
72
|
2 |
|
$definition = $container->getDefinition('gm_redmine_user_provider.provider'); |
|
73
|
2 |
|
$definition->addMethodCall('setUserRepository', array(new Reference($repositoryId))); |
|
74
|
|
|
} |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Control if tagged repository is valid |
|
79
|
|
|
* |
|
80
|
|
|
* @param ContainerBuilder $container |
|
81
|
|
|
* @param string $repositoryId |
|
82
|
|
|
*/ |
|
83
|
3 |
|
private function checkRepositoryValidity(ContainerBuilder $container, string $repositoryId): void |
|
84
|
|
|
{ |
|
85
|
3 |
|
$repositoryDefinition = $container->getDefinition($repositoryId); |
|
86
|
3 |
|
$className = $container->getParameterBag()->resolveValue($repositoryDefinition->getClass()); |
|
87
|
3 |
|
$reflection = new \ReflectionClass($className); |
|
88
|
3 |
|
$repositoryInterface = 'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface'; |
|
89
|
3 |
|
if (!$reflection->implementsInterface($repositoryInterface)) { |
|
90
|
1 |
|
throw new \InvalidArgumentException( |
|
91
|
|
|
sprintf( |
|
92
|
1 |
|
'The user repository %s should implement interface "%s"', |
|
93
|
|
|
$repositoryId, |
|
94
|
1 |
|
'GMaissa\RedmineUserProviderBundle\Repository\UserRepositoryInterface' |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
2 |
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|