|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Bluz Framework Component |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Bluz PHP Team |
|
6
|
|
|
* @link https://github.com/bluzphp/framework |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Application; |
|
12
|
|
|
|
|
13
|
|
|
use Application\Users\Table; |
|
14
|
|
|
use Bluz\Application\Application; |
|
15
|
|
|
use Bluz\Controller\Controller; |
|
16
|
|
|
use Bluz\Proxy\Auth; |
|
17
|
|
|
use Bluz\Proxy\Logger; |
|
18
|
|
|
use Bluz\Proxy\Request; |
|
19
|
|
|
use Bluz\Proxy\Response; |
|
20
|
|
|
use Bluz\Proxy\Router; |
|
21
|
|
|
use Bluz\Request\RequestFactory; |
|
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Bootstrap for CLI |
|
28
|
|
|
* |
|
29
|
|
|
* @category Application |
|
30
|
|
|
* @package Bootstrap |
|
31
|
|
|
* |
|
32
|
|
|
* @author Anton Shevchuk |
|
33
|
|
|
* @created 17.12.12 15:24 |
|
34
|
|
|
*/ |
|
35
|
|
|
class CliBootstrap extends Application |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Layout flag |
|
39
|
|
|
* |
|
40
|
|
|
* @var boolean |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $layoutFlag = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var InputInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $input; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var OutputInterface |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $output; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param InputInterface $input |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setInput(InputInterface $input): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->input = $input; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return InputInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getInput() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->input; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param OutputInterface $output |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setOutput(OutputInterface $output): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->output = $output; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return OutputInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getOutput() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->output; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* get CLI Request |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
* @throws \InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
|
|
public function initRequest(): void |
|
93
|
|
|
{ |
|
94
|
|
|
$uri = $this->getInput()->getArgument('uri'); |
|
95
|
|
|
|
|
96
|
|
|
$parsedQuery = parse_url($uri, PHP_URL_QUERY); |
|
97
|
|
|
if (false !== $parsedQuery && null !== $parsedQuery) { |
|
98
|
|
|
parse_str($parsedQuery, $query); |
|
99
|
|
|
} else { |
|
100
|
|
|
$query = []; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$request = RequestFactory::fromGlobals(['REQUEST_URI' => $uri, 'REQUEST_METHOD' => 'CLI'], $query); |
|
104
|
|
|
|
|
105
|
|
|
Request::setInstance($request); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Pre process |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function preProcess(): void |
|
114
|
|
|
{ |
|
115
|
|
|
Router::process(); |
|
116
|
|
|
Response::setType('CLI'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* {@inheritdoc} |
|
121
|
|
|
* |
|
122
|
|
|
* @param Controller $controller |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
* @throws \Bluz\Db\Exception\DbException |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function preDispatch($controller): void |
|
128
|
|
|
{ |
|
129
|
|
|
// auth as CLI user |
|
130
|
|
|
if ($cliUser = Table::findRowWhere(['login' => 'system'])) { |
|
131
|
|
|
Auth::setIdentity($cliUser); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
parent::preDispatch($controller); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Render, is send Response |
|
139
|
|
|
* |
|
140
|
|
|
* @return void |
|
141
|
|
|
*/ |
|
142
|
|
|
public function render(): void |
|
143
|
|
|
{ |
|
144
|
|
|
$io = new SymfonyStyle($this->getInput(), $this->getOutput()); |
|
145
|
|
|
$io->title('Bluz CLI'); |
|
146
|
|
|
|
|
147
|
|
|
if ($params = Request::getParams()) { |
|
148
|
|
|
foreach ($params as $key => $value) { |
|
149
|
|
|
$key = \is_int($key) ? "<comment>$key</comment>" : "<info>$key</info>"; |
|
150
|
|
|
$io->writeln("$key: $value"); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$io->writeln(''); |
|
155
|
|
|
$io->writeln('========'); |
|
156
|
|
|
$io->writeln(''); |
|
157
|
|
|
$io->writeln(json_encode(Response::getBody())); |
|
158
|
|
|
$io->writeln(''); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Finish it |
|
163
|
|
|
* |
|
164
|
|
|
* @return void |
|
165
|
|
|
*/ |
|
166
|
|
|
public function end(): void |
|
167
|
|
|
{ |
|
168
|
|
|
if ($errors = Logger::get('error')) { |
|
169
|
|
|
$this->sendErrors($errors); |
|
170
|
|
|
} |
|
171
|
|
|
// return code 1 for invalid behaviour of application |
|
|
|
|
|
|
172
|
|
|
// if ($exception = $this->getException()) { |
|
173
|
|
|
// echo $exception->getMessage(); |
|
174
|
|
|
// exit(1); |
|
175
|
|
|
// } |
|
176
|
|
|
exit; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Send errors to logger |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $errors |
|
183
|
|
|
* |
|
184
|
|
|
* @return void |
|
185
|
|
|
*/ |
|
186
|
|
|
protected function sendErrors($errors): void |
|
187
|
|
|
{ |
|
188
|
|
|
foreach ($errors as $message) { |
|
189
|
|
|
errorLog(new \ErrorException($message, 0, E_USER_ERROR)); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|