|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Bridge\Symfony\DependencyInjection; |
|
12
|
|
|
|
|
13
|
|
|
use Strider2038\JsonRpcClient\ClientFactoryInterface; |
|
14
|
|
|
use Strider2038\JsonRpcClient\ClientInterface; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @author Igor Lazarev <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class JsonRpcClientExtension extends Extension implements PrependExtensionInterface |
|
27
|
|
|
{ |
|
28
|
|
|
public function prepend(ContainerBuilder $container): void |
|
29
|
|
|
{ |
|
30
|
|
|
$frameworkConfiguration = $container->getExtensionConfig('framework'); |
|
31
|
|
|
|
|
32
|
|
|
if (empty($frameworkConfiguration)) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
if (!isset($frameworkConfiguration['serializer']['enabled'])) { |
|
37
|
|
|
$container->prependExtensionConfig('framework', ['serializer' => ['enabled' => true]]); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
42
|
|
|
{ |
|
43
|
|
|
$this->loadServices($container); |
|
44
|
|
|
$configuration = $this->loadConfiguration($configs, $container); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($configuration as $clientId => $clientConfig) { |
|
47
|
|
|
$definition = new Definition(ClientInterface::class); |
|
48
|
|
|
$definition->setPublic(true); |
|
49
|
|
|
$definition->setFactory([ |
|
50
|
|
|
new Reference(ClientFactoryInterface::class), |
|
51
|
|
|
'createClient', |
|
52
|
|
|
]); |
|
53
|
|
|
$definition->setArgument('$url', $clientConfig['url']); |
|
54
|
|
|
$definition->setArgument('$options', $clientConfig['options'] ?? []); |
|
55
|
|
|
|
|
56
|
|
|
$container->setDefinition('json_rpc_client.'.$clientId, $definition); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (array_key_exists('default', $configuration)) { |
|
60
|
|
|
$container->setAlias(ClientInterface::class, 'json_rpc_client.default'); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function loadServices(ContainerBuilder $container): void |
|
65
|
|
|
{ |
|
66
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
67
|
|
|
$loader->load('services.xml'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function loadConfiguration(array $configs, ContainerBuilder $container): array |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
$configuration = new Configuration(); |
|
73
|
|
|
|
|
74
|
|
|
return $this->processConfiguration($configuration, $configs); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.