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\RouterListener; |
12
|
|
|
use Bankiru\Api\Rpc\Routing\MethodCollection; |
13
|
|
|
use Bankiru\Api\Rpc\Routing\Router; |
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
18
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
19
|
|
|
|
20
|
|
|
final class RpcExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** {@inheritdoc} */ |
24
|
1 |
|
public function load(array $configs, ContainerBuilder $container) |
25
|
|
|
{ |
26
|
1 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
27
|
|
|
|
28
|
1 |
|
if (array_key_exists('NelmioApiDocBundle', $container->getParameter('kernel.bundles'))) { |
29
|
|
|
$loader->load('nelmio.yml'); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
$configuration = new Configuration(); |
33
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
34
|
|
|
|
35
|
|
|
|
36
|
1 |
|
$loader->load('rpc.yml'); |
37
|
|
|
|
38
|
1 |
|
$this->configureRouter($config['router'], $container); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $router |
43
|
|
|
* @param ContainerBuilder $container |
44
|
|
|
*/ |
45
|
1 |
|
private function configureRouter(array $router, ContainerBuilder $container) |
46
|
|
|
{ |
47
|
1 |
|
$endpoints = $router['endpoints']; |
48
|
|
|
|
49
|
1 |
|
$endpointLoader = $container->getDefinition('rpc.router.loader'); |
50
|
|
|
|
51
|
1 |
|
$routerCollection = $container->getDefinition('rpc.router.collection'); |
52
|
|
|
|
53
|
1 |
|
foreach ($endpoints as $name => $config) { |
54
|
|
|
|
55
|
1 |
|
$collection = new Definition(MethodCollection::class); |
56
|
1 |
|
$endpointRouter = new Definition( |
57
|
1 |
|
Router::class, |
58
|
|
|
[ |
59
|
1 |
|
$container->getDefinition('rpc.router.resolver'), |
60
|
1 |
|
$config['resources'], |
61
|
1 |
|
$collection, |
62
|
|
|
] |
63
|
1 |
|
); |
64
|
|
|
|
65
|
1 |
|
$endpointRouter->addTag('rpc_router'); |
66
|
1 |
|
$endpointLoader->addMethodCall('addEndpoint', [$name, $config]); |
67
|
1 |
|
$this->configureRouteListener($container, $name, $endpointRouter); |
68
|
|
|
|
69
|
|
|
//@todo review (@scaytrase) |
70
|
1 |
|
$container->setDefinition(sprintf('rpc.endpoint_router.%s', $name), $endpointRouter); |
71
|
|
|
|
72
|
1 |
|
$routerCollection->addMethodCall('addRouter', [$name, $endpointRouter]); |
73
|
1 |
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ContainerBuilder $container |
78
|
|
|
* @param string $name |
79
|
|
|
* @param Definition $endpointRouter |
80
|
|
|
*/ |
81
|
1 |
|
private function configureRouteListener(ContainerBuilder $container, $name, Definition $endpointRouter) |
82
|
|
|
{ |
83
|
1 |
|
$listener = new Definition(RouterListener::class, [$name, $endpointRouter]); |
84
|
1 |
|
$listener->addTag('kernel.event_listener', ['event' => 'rpc.request', 'method' => 'onRequest']); |
85
|
1 |
|
$container->setDefinition('rpc.router_listener.' . $name, $listener); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
1 |
|
public function getAlias() |
89
|
|
|
{ |
90
|
1 |
|
return 'rpc'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|