1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Spike library |
4
|
|
|
* @author Tao <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Spike\Client; |
7
|
|
|
|
8
|
|
|
use Slince\Event\Event; |
9
|
|
|
use Spike\Application as BaseApplication; |
10
|
|
|
use Slince\Event\SubscriberInterface; |
11
|
|
|
use Spike\Client\Command\InitCommand; |
12
|
|
|
use Spike\Client\Command\SpikeCommand; |
13
|
|
|
use Spike\Client\Command\ShowProxyHostsCommand; |
14
|
|
|
use Spike\Client\Subscriber\LoggerSubscriber; |
15
|
|
|
use Spike\Logger\Logger; |
16
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
class Application extends BaseApplication implements SubscriberInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const NAME = 'spike-client'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const VERSION = '1.0.0.dev'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The client instance |
35
|
|
|
* @var Client |
36
|
|
|
*/ |
37
|
|
|
protected $client; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The server address |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $serverAddress; |
44
|
|
|
|
45
|
|
|
public function __construct(Configuration $configuration) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($configuration,static::NAME, static::VERSION); |
48
|
|
|
$this->client = new Client( |
49
|
|
|
$configuration->getServerAddress(), |
50
|
|
|
$configuration->getTunnels(), |
51
|
|
|
$configuration->get('auth') ?: [], |
52
|
|
|
$this->loop, |
53
|
|
|
$this->dispatcher |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets the client for the application |
59
|
|
|
* @param Client $client |
60
|
|
|
*/ |
61
|
|
|
public function setKernel(Client $client) |
62
|
|
|
{ |
63
|
|
|
$this->client = $client; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the client instance |
68
|
|
|
* @return Client |
69
|
|
|
*/ |
70
|
|
|
public function getKernel() |
71
|
|
|
{ |
72
|
|
|
return $this->client; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* @codeCoverageIgnore |
78
|
|
|
*/ |
79
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
80
|
|
|
{ |
81
|
|
|
$this->input = $input; |
82
|
|
|
$this->output = $output; |
83
|
|
|
//Logger |
84
|
|
|
$this->logger = new Logger( |
85
|
|
|
$this->getConfiguration()->getLogLevel(), |
86
|
|
|
$this->getConfiguration()->getLogFile(), |
87
|
|
|
$this->output |
88
|
|
|
); |
89
|
|
|
$this->client->setLogger($this->logger); |
90
|
|
|
$commandName = $input->getFirstArgument(); |
91
|
|
|
if ($commandName) { |
92
|
|
|
$exitCode = parent::doRun($input, $output); |
93
|
|
|
} else { |
94
|
|
|
$exitCode = $this->doRunClient(); |
95
|
|
|
} |
96
|
|
|
return $exitCode; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @codeCoverageIgnore |
101
|
|
|
*/ |
102
|
|
|
protected function doRunClient() |
103
|
|
|
{ |
104
|
|
|
if (true === $this->input->hasParameterOption(array('--help', '-h'), true)) { |
105
|
|
|
$command = $this->get('spike'); |
106
|
|
|
$exitCode = $this->doRunCommand($command, $this->input, $this->output); |
107
|
|
|
} else { |
108
|
|
|
$exitCode = $this->runClient(); |
109
|
|
|
} |
110
|
|
|
return $exitCode; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Start the client |
115
|
|
|
* @codeCoverageIgnore |
116
|
|
|
*/ |
117
|
|
|
protected function runClient() |
118
|
|
|
{ |
119
|
|
|
foreach ($this->getSubscribers() as $subscriber) { |
120
|
|
|
$this->dispatcher->addSubscriber($subscriber); |
121
|
|
|
} |
122
|
|
|
$this->client->run(); |
123
|
|
|
return 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function getEvents() |
130
|
|
|
{ |
131
|
|
|
return [ |
132
|
|
|
EventStore::CONNECT_TO_SERVER => 'onConnectToServer', |
133
|
|
|
EventStore::CANNOT_CONNECT_TO_SERVER => 'onCannotConnectToServer', |
134
|
|
|
EventStore::AUTH_ERROR => 'onAuthError', |
135
|
|
|
EventStore::REGISTER_TUNNEL_ERROR => 'onRegisterTunnelError', |
136
|
|
|
EventStore::DISCONNECT_FROM_SERVER => 'onDisconnectFromServer' |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @codeCoverageIgnore |
142
|
|
|
*/ |
143
|
|
|
public function onConnectToServer(Event $event) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @codeCoverageIgnore |
149
|
|
|
*/ |
150
|
|
|
public function onAuthError(Event $event) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$this->client->close(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @codeCoverageIgnore |
157
|
|
|
*/ |
158
|
|
|
public function onRegisterTunnelError(Event $event) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
$this->client->close(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @codeCoverageIgnore |
165
|
|
|
*/ |
166
|
|
|
public function onCannotConnectToServer(Event $event) |
|
|
|
|
167
|
|
|
{ |
168
|
|
|
$this->client->close(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @codeCoverageIgnore |
173
|
|
|
*/ |
174
|
|
|
public function onDisconnectFromServer(Event $event) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$this->client->run(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
|
|
public function getDefaultCommands() |
183
|
|
|
{ |
184
|
|
|
return array_merge(parent::getDefaultCommands(), [ |
|
|
|
|
185
|
|
|
new SpikeCommand($this), |
186
|
|
|
new ShowProxyHostsCommand($this), |
187
|
|
|
new InitCommand($this), |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets all subscribers |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
|
|
public function getSubscribers() |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
|
|
new LoggerSubscriber($this), |
199
|
|
|
$this, |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
protected function getDefaultInputDefinition() |
207
|
|
|
{ |
208
|
|
|
$definition = new InputDefinition([ |
209
|
|
|
new InputOption('config', null, InputOption::VALUE_OPTIONAL, |
210
|
|
|
'The configuration file, support json,ini,xml and yaml format') |
211
|
|
|
]); |
212
|
|
|
$defaultDefinition = parent::getDefaultInputDefinition(); |
213
|
|
|
$definition->addArguments($defaultDefinition->getArguments()); |
214
|
|
|
$definition->addOptions($defaultDefinition->getOptions()); |
215
|
|
|
return $definition; |
216
|
|
|
} |
217
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.