1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: batanov.pavel |
5
|
|
|
* Date: 11.02.2016 |
6
|
|
|
* Time: 16:25 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Bankiru\Api\Rpc\DependencyInjection; |
10
|
|
|
|
11
|
|
|
use Bankiru\Api\Rpc\Listener\ExceptionListener; |
12
|
|
|
use Bankiru\Api\Rpc\Listener\RouterListener; |
13
|
|
|
use Bankiru\Api\Rpc\Routing\MethodCollection; |
14
|
|
|
use Bankiru\Api\Rpc\Routing\Router; |
15
|
|
|
use Bankiru\Api\Rpc\RpcEvents; |
16
|
|
|
use Symfony\Component\Config\FileLocator; |
17
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
23
|
|
|
|
24
|
|
|
final class RpcExtension extends Extension implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** {@inheritdoc} */ |
28
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
29
|
|
|
{ |
30
|
1 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
31
|
|
|
|
32
|
1 |
|
if (array_key_exists('NelmioApiDocBundle', $container->getParameter('kernel.bundles'))) { |
33
|
|
|
$loader->load('nelmio.yml'); |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
$configuration = new Configuration(); |
37
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
$loader->load('rpc.yml'); |
41
|
|
|
|
42
|
1 |
|
$this->configureRouter($config['router'], $container); |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $router |
47
|
|
|
* @param ContainerBuilder $container |
48
|
|
|
*/ |
49
|
1 |
|
private function configureRouter(array $router, ContainerBuilder $container) |
50
|
|
|
{ |
51
|
1 |
|
$endpoints = $router['endpoints']; |
52
|
|
|
|
53
|
1 |
|
$endpointLoader = $container->getDefinition('rpc.router.loader'); |
54
|
|
|
|
55
|
1 |
|
$routerCollection = $container->getDefinition('rpc.router.collection'); |
56
|
|
|
|
57
|
1 |
|
foreach ($endpoints as $name => $config) { |
58
|
|
|
|
59
|
1 |
|
$collection = new Definition(MethodCollection::class); |
60
|
1 |
|
$endpointRouter = new Definition( |
61
|
1 |
|
Router::class, |
62
|
|
|
[ |
63
|
1 |
|
$container->getDefinition('rpc.router.resolver'), |
64
|
1 |
|
$config['resources'], |
65
|
1 |
|
$collection, |
66
|
|
|
] |
67
|
1 |
|
); |
68
|
|
|
|
69
|
1 |
|
$endpointRouter->addTag('rpc_router'); |
70
|
1 |
|
$endpointLoader->addMethodCall('addEndpoint', [$name, $config]); |
71
|
1 |
|
$this->configureRouteListener($container, $name, $endpointRouter); |
72
|
|
|
|
73
|
|
|
//@todo review (@scaytrase) |
74
|
1 |
|
$container->setDefinition(sprintf('rpc.endpoint_router.%s', $name), $endpointRouter); |
75
|
|
|
|
76
|
1 |
|
$routerCollection->addMethodCall('addRouter', [$name, $endpointRouter]); |
77
|
1 |
|
} |
78
|
1 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param ContainerBuilder $container |
82
|
|
|
* @param string $name |
83
|
|
|
* @param Definition $endpointRouter |
84
|
|
|
*/ |
85
|
1 |
|
private function configureRouteListener(ContainerBuilder $container, $name, Definition $endpointRouter) |
86
|
|
|
{ |
87
|
1 |
|
$listener = new Definition(RouterListener::class, [$name, $endpointRouter]); |
88
|
1 |
|
$listener->addTag('kernel.event_listener', ['event' => 'rpc.request', 'method' => 'onRequest']); |
89
|
1 |
|
$container->setDefinition('rpc.router_listener.' . $name, $listener); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
1 |
|
public function getAlias() |
93
|
|
|
{ |
94
|
1 |
|
return 'rpc'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** {@inheritdoc} */ |
98
|
1 |
|
public function process(ContainerBuilder $container) |
99
|
|
|
{ |
100
|
1 |
|
if ($container->has('logger')) { |
101
|
|
|
$container |
102
|
1 |
|
->register('rpc.exception_listener', ExceptionListener::class) |
103
|
1 |
|
->setArguments([new Reference('logger')]) |
104
|
1 |
|
->addTag( |
105
|
1 |
|
'kernel.event_listener', |
106
|
|
|
[ |
107
|
1 |
|
'event' => RpcEvents::EXCEPTION, |
108
|
1 |
|
'method' => 'onException', |
109
|
|
|
] |
110
|
1 |
|
); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
1 |
|
$loader = $container->getDefinition('rpc.router.resolver'); |
114
|
|
|
|
115
|
1 |
|
$taggedServices = $container->findTaggedServiceIds('rpc.route_loader'); |
116
|
|
|
|
117
|
1 |
|
foreach ($taggedServices as $id => $tags) { |
118
|
1 |
|
$loader->addMethodCall('addLoader', [new Reference($id)]); |
119
|
1 |
|
} |
120
|
1 |
|
} |
121
|
|
|
} |
122
|
|
|
|