1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TransformerPass |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\DependencyInjection\Compiler; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class TransformerPass |
15
|
|
|
* |
16
|
|
|
* @package Graviton\ProxyBundle\Definition\Loader |
17
|
|
|
* |
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
19
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
20
|
|
|
* @link http://swisscom.ch |
21
|
|
|
*/ |
22
|
|
|
class TransformerPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
* |
27
|
|
|
* @param ContainerBuilder $container Symfony Service container |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
2 |
|
public function process(ContainerBuilder $container) |
32
|
|
|
{ |
33
|
|
|
// always first check if the primary service is defined |
34
|
2 |
|
if (!$container->has('graviton.proxy.service.transformationhandler')) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
$definition = $container->findDefinition( |
39
|
2 |
|
'graviton.proxy.service.transformationhandler' |
40
|
|
|
); |
41
|
2 |
|
$this->registerTaggedTransformation( |
42
|
|
|
$container, |
43
|
|
|
$definition, |
44
|
2 |
|
'graviton.proxy.transformer.request', |
45
|
2 |
|
'addRequestTransformation' |
46
|
|
|
); |
47
|
2 |
|
$this->registerTaggedTransformation( |
48
|
|
|
$container, |
49
|
|
|
$definition, |
50
|
2 |
|
'graviton.proxy.transformer.response', |
51
|
2 |
|
'addResponseTransformation' |
52
|
|
|
); |
53
|
2 |
|
$this->registerTaggedTransformation( |
54
|
|
|
$container, |
55
|
|
|
$definition, |
56
|
2 |
|
'graviton.proxy.transformer.schema', |
57
|
2 |
|
'addSchemaTransformation' |
58
|
|
|
); |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds the found services to the TransformationHandler |
63
|
|
|
* |
64
|
|
|
* @param ContainerBuilder $container Symfony Service Container |
65
|
|
|
* @param Definition $definition Service the services shall be add to. |
66
|
|
|
* @param string $tag Tag identifying the service to be added |
67
|
|
|
* @param string $callable Name of the method to call to add the tagged service. |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
2 |
|
private function registerTaggedTransformation(ContainerBuilder $container, Definition $definition, $tag, $callable) |
72
|
|
|
{ |
73
|
2 |
|
$taggedServices = $container->findTaggedServiceIds($tag); |
74
|
|
|
|
75
|
2 |
View Code Duplication |
foreach ($taggedServices as $id => $tags) { |
|
|
|
|
76
|
|
|
foreach ($tags as $attributes) { |
77
|
|
|
$definition->addMethodCall( |
78
|
|
|
$callable, |
79
|
|
|
array( |
80
|
|
|
$attributes["alias"], |
81
|
|
|
$attributes["endpoint"], |
82
|
|
|
new Reference($id) |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
|
|
} |
89
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.