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
|
|
|
null, |
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
|
|
|
*/ |
78
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
79
|
|
|
{ |
80
|
|
|
$this->input = $input; |
81
|
|
|
$this->output = $output; |
82
|
|
|
//Logger |
83
|
|
|
$this->logger = new Logger( |
84
|
|
|
$this->getConfiguration()->getLogLevel(), |
85
|
|
|
$this->getConfiguration()->getLogFile(), |
86
|
|
|
$this->output |
87
|
|
|
); |
88
|
|
|
$this->client->setLogger($this->logger); |
89
|
|
|
$commandName = $input->getFirstArgument(); |
90
|
|
|
if ($commandName) { |
91
|
|
|
$exitCode = parent::doRun($input, $output); |
92
|
|
|
} else { |
93
|
|
|
$exitCode = $this->doRunClient(); |
94
|
|
|
} |
95
|
|
|
return $exitCode; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function doRunClient() |
99
|
|
|
{ |
100
|
|
|
if (true === $this->input->hasParameterOption(array('--help', '-h'), true)) { |
101
|
|
|
$command = $this->get('spike'); |
102
|
|
|
$exitCode = $this->doRunCommand($command, $this->input, $this->output); |
103
|
|
|
} else { |
104
|
|
|
$exitCode = $this->runClient(); |
105
|
|
|
} |
106
|
|
|
return $exitCode; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getEvents() |
113
|
|
|
{ |
114
|
|
|
return [ |
115
|
|
|
EventStore::AUTH_ERROR => 'onAuthError', |
116
|
|
|
EventStore::REGISTER_TUNNEL_ERROR => 'onRegisterTunnelError', |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function onAuthError(Event $event) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$this->output->writeln('Auth error, please checks your config file'); |
123
|
|
|
$this->client->close(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function onRegisterTunnelError(Event $event) |
127
|
|
|
{ |
128
|
|
|
$this->output->writeln(sprintf('Registers the tunnel "%s" error, message: %s', |
129
|
|
|
$event->getArgument('tunnel'), |
130
|
|
|
$event->getArgument('errorMessage') |
131
|
|
|
)); |
132
|
|
|
$this->client->close(); |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* Start the client |
136
|
|
|
*/ |
137
|
|
|
protected function runClient() |
138
|
|
|
{ |
139
|
|
|
foreach ($this->getSubscribers() as $subscriber) { |
140
|
|
|
$this->dispatcher->addSubscriber($subscriber); |
141
|
|
|
} |
142
|
|
|
$this->client->run(); |
143
|
|
|
return 0; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
|
|
public function getDefaultCommands() |
150
|
|
|
{ |
151
|
|
|
return array_merge(parent::getDefaultCommands(), [ |
|
|
|
|
152
|
|
|
new SpikeCommand($this), |
153
|
|
|
new ShowProxyHostsCommand($this), |
154
|
|
|
new InitCommand($this), |
155
|
|
|
]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Gets all subscribers |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function getSubscribers() |
163
|
|
|
{ |
164
|
|
|
return [ |
165
|
|
|
$this, |
166
|
|
|
new LoggerSubscriber($this) |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
|
|
protected function getDefaultInputDefinition() |
174
|
|
|
{ |
175
|
|
|
$definition = new InputDefinition([ |
176
|
|
|
new InputOption('config', null, InputOption::VALUE_OPTIONAL, |
177
|
|
|
'The configuration file, support json,ini,xml and yaml format') |
178
|
|
|
]); |
179
|
|
|
$defaultDefinition = parent::getDefaultInputDefinition(); |
180
|
|
|
$definition->addArguments($defaultDefinition->getArguments()); |
181
|
|
|
$definition->addOptions($defaultDefinition->getOptions()); |
182
|
|
|
return $definition; |
183
|
|
|
} |
184
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.