1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebSocketsWAMPClientExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:WebSocketsWAMPClient! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 11.05.18 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\WebSocketsWAMPClient\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
|
22
|
|
|
use Kdyby\Console; |
23
|
|
|
|
24
|
|
|
use React; |
25
|
|
|
|
26
|
|
|
use Psr\Log; |
27
|
|
|
|
28
|
|
|
use IPub\WebSocketsWAMPClient\Client; |
29
|
|
|
use IPub\WebSocketsWAMPClient\Commands; |
30
|
|
|
use IPub\WebSocketsWAMPClient\Logger; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Web sockets WAMP client extension container |
34
|
|
|
* |
35
|
|
|
* @package iPublikuj:WebSocketsWAMPClient! |
36
|
|
|
* @subpackage DI |
37
|
|
|
* |
38
|
|
|
* @author Adam Kadlec <[email protected]> |
39
|
|
|
* |
40
|
|
|
* @method DI\ContainerBuilder getContainerBuilder() |
41
|
|
|
* @method array getConfig(array $default) |
42
|
|
|
* @method string prefix($id) |
43
|
|
|
*/ |
44
|
1 |
|
final class WebSocketsWAMPClientExtension extends DI\CompilerExtension |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $defaults = [ |
50
|
|
|
'server' => [ |
51
|
|
|
'host' => '127.0.0.1', |
52
|
|
|
'port' => 8080, |
53
|
|
|
'path' => '/', |
54
|
|
|
'origin' => NULL, |
55
|
|
|
'dns' => [ |
56
|
|
|
'enable' => TRUE, |
57
|
|
|
'address' => '8.8.8.8', |
58
|
|
|
], |
59
|
|
|
'secured' => [ |
60
|
|
|
'enable' => FALSE, |
61
|
|
|
'sslSettings' => [], |
62
|
|
|
], |
63
|
|
|
], |
64
|
|
|
'loop' => NULL, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function loadConfiguration() : void |
71
|
|
|
{ |
72
|
1 |
|
parent::loadConfiguration(); |
73
|
|
|
|
74
|
|
|
/** @var DI\ContainerBuilder $builder */ |
75
|
1 |
|
$builder = $this->getContainerBuilder(); |
76
|
|
|
/** @var array $configuration */ |
77
|
1 |
|
$configuration = $this->getConfig($this->defaults); |
78
|
|
|
|
79
|
1 |
|
if ($configuration['loop'] === NULL) { |
80
|
1 |
|
$loop = $builder->addDefinition($this->prefix('client.loop')) |
81
|
1 |
|
->setType(React\EventLoop\LoopInterface::class) |
82
|
1 |
|
->setFactory('React\EventLoop\Factory::create') |
83
|
1 |
|
->setAutowired(FALSE); |
84
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
$loop = $builder->getDefinition(ltrim($configuration['loop'], '@')); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
if ($builder->findByType(Log\LoggerInterface::class) === []) { |
90
|
1 |
|
$builder->addDefinition($this->prefix('server.logger')) |
91
|
1 |
|
->setType(Logger\Console::class); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$builder->addDefinition($this->prefix('client.configuration')) |
95
|
1 |
|
->setType(Client\Configuration::class) |
96
|
1 |
|
->setArguments([ |
97
|
1 |
|
'host' => (string) $configuration['server']['host'], |
98
|
1 |
|
'port' => (int) $configuration['server']['port'], |
99
|
1 |
|
'path' => (string) $configuration['server']['path'], |
100
|
1 |
|
'origin' => $configuration['server']['origin'], |
101
|
|
|
]); |
102
|
|
|
|
103
|
1 |
|
$builder->addDefinition($this->prefix('client.client')) |
104
|
1 |
|
->setType(Client\Client::class) |
105
|
1 |
|
->setArguments([ |
106
|
1 |
|
'loop' => $loop, |
107
|
|
|
]); |
108
|
|
|
|
109
|
|
|
// Define all console commands |
110
|
|
|
$commands = [ |
111
|
1 |
|
'client' => Commands\ClientCommand::class, |
112
|
|
|
]; |
113
|
|
|
|
114
|
1 |
|
foreach ($commands as $name => $cmd) { |
115
|
1 |
|
$builder->addDefinition($this->prefix('commands' . lcfirst($name))) |
116
|
1 |
|
->setType($cmd) |
117
|
1 |
|
->addTag(Console\DI\ConsoleExtension::TAG_COMMAND); |
118
|
|
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param Nette\Configurator $config |
123
|
|
|
* @param string $extensionName |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'wampClient') : void |
128
|
|
|
{ |
129
|
1 |
|
$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) { |
130
|
1 |
|
$compiler->addExtension($extensionName, new WebSocketsWAMPClientExtension); |
131
|
1 |
|
}; |
132
|
1 |
|
} |
133
|
|
|
} |
134
|
|
|
|