1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Console\Command; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Completion\CompletionInput; |
9
|
|
|
use Symfony\Component\Console\Completion\CompletionSuggestions; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
16
|
|
|
|
17
|
|
|
use function explode; |
18
|
|
|
use function fclose; |
19
|
|
|
use function file_exists; |
20
|
|
|
use function fsockopen; |
21
|
|
|
use function is_dir; |
22
|
|
|
use function passthru; |
23
|
|
|
|
24
|
|
|
final class Serve extends Command |
25
|
|
|
{ |
26
|
|
|
public const EXIT_CODE_NO_DOCUMENT_ROOT = 2; |
27
|
|
|
public const EXIT_CODE_NO_ROUTING_FILE = 3; |
28
|
|
|
public const EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS = 5; |
29
|
|
|
|
30
|
|
|
private string $defaultAddress; |
31
|
|
|
private string $defaultPort; |
32
|
|
|
private string $defaultDocroot; |
33
|
|
|
private string $defaultRouter; |
34
|
|
|
private int $defaultWorkers; |
35
|
|
|
|
36
|
|
|
protected static $defaultName = 'serve'; |
37
|
|
|
protected static $defaultDescription = 'Runs PHP built-in web server'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @psalm-param array{ |
41
|
|
|
* address?:non-empty-string, |
42
|
|
|
* port?:non-empty-string, |
43
|
|
|
* docroot?:string, |
44
|
|
|
* router?:string, |
45
|
|
|
* workers?:int|string |
46
|
|
|
* } $options |
47
|
|
|
*/ |
48
|
49 |
|
public function __construct(private ?string $appRootPath = null, ?array $options = []) |
49
|
|
|
{ |
50
|
49 |
|
$this->defaultAddress = $options['address'] ?? '127.0.0.1'; |
51
|
49 |
|
$this->defaultPort = $options['port'] ?? '8080'; |
52
|
49 |
|
$this->defaultDocroot = $options['docroot'] ?? 'public'; |
53
|
49 |
|
$this->defaultRouter = $options['router'] ?? 'public/index.php'; |
54
|
49 |
|
$this->defaultWorkers = (int) ($options['workers'] ?? 2); |
55
|
|
|
|
56
|
49 |
|
parent::__construct(); |
57
|
|
|
} |
58
|
|
|
|
59
|
49 |
|
public function configure(): void |
60
|
|
|
{ |
61
|
49 |
|
$this |
62
|
49 |
|
->setHelp( |
63
|
49 |
|
'In order to access server from remote machines use 0.0.0.0:8000. That is especially useful when running server in a virtual machine.' |
64
|
49 |
|
) |
65
|
49 |
|
->addArgument('address', InputArgument::OPTIONAL, 'Host to serve at', $this->defaultAddress) |
66
|
49 |
|
->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to serve at', $this->defaultPort) |
67
|
49 |
|
->addOption( |
68
|
49 |
|
'docroot', |
69
|
49 |
|
't', |
70
|
49 |
|
InputOption::VALUE_OPTIONAL, |
71
|
49 |
|
'Document root to serve from', |
72
|
49 |
|
$this->defaultDocroot |
73
|
49 |
|
) |
74
|
49 |
|
->addOption('router', 'r', InputOption::VALUE_OPTIONAL, 'Path to router script', $this->defaultRouter) |
75
|
49 |
|
->addOption( |
76
|
49 |
|
'workers', |
77
|
49 |
|
'w', |
78
|
49 |
|
InputOption::VALUE_OPTIONAL, |
79
|
49 |
|
'Workers number the server will start with', |
80
|
49 |
|
$this->defaultWorkers |
81
|
49 |
|
) |
82
|
49 |
|
->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.') |
83
|
49 |
|
->addOption('xdebug', 'x', InputOption::VALUE_OPTIONAL, 'Enables XDEBUG session.', false); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
87
|
|
|
{ |
88
|
1 |
|
if ($input->mustSuggestArgumentValuesFor('address')) { |
89
|
1 |
|
$suggestions->suggestValues(['localhost', '127.0.0.1', '0.0.0.0']); |
90
|
1 |
|
return; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
95
|
|
|
{ |
96
|
6 |
|
$io = new SymfonyStyle($input, $output); |
97
|
6 |
|
$io->title('Yii3 Development Server'); |
98
|
6 |
|
$io->writeln('https://yiiframework.com' . "\n"); |
99
|
|
|
|
100
|
|
|
/** @var string $address */ |
101
|
6 |
|
$address = $input->getArgument('address'); |
102
|
|
|
|
103
|
|
|
/** @var string $router */ |
104
|
6 |
|
$router = $input->getOption('router'); |
105
|
6 |
|
$workers = (int) $input->getOption('workers'); |
106
|
|
|
|
107
|
|
|
/** @var string $port */ |
108
|
6 |
|
$port = $input->getOption('port'); |
109
|
|
|
|
110
|
|
|
/** @var string $docroot */ |
111
|
6 |
|
$docroot = $input->getOption('docroot'); |
112
|
|
|
|
113
|
6 |
|
if ($router === $this->defaultRouter && !file_exists($this->defaultRouter)) { |
114
|
4 |
|
$io->warning( |
115
|
4 |
|
'Default router "' . $this->defaultRouter . '" does not exist. Serving without router. URLs with dots may fail.' |
116
|
4 |
|
); |
117
|
4 |
|
$router = null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** @var string $env */ |
121
|
6 |
|
$env = $input->getOption('env'); |
122
|
|
|
|
123
|
6 |
|
$documentRoot = $this->getRootPath() . DIRECTORY_SEPARATOR . $docroot; |
124
|
|
|
|
125
|
6 |
|
if (!str_contains($address, ':')) { |
126
|
5 |
|
$address .= ':' . $port; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
if (!is_dir($documentRoot)) { |
130
|
1 |
|
$io->error("Document root \"$documentRoot\" does not exist."); |
131
|
1 |
|
return self::EXIT_CODE_NO_DOCUMENT_ROOT; |
132
|
|
|
} |
133
|
|
|
|
134
|
5 |
|
if ($this->isAddressTaken($address)) { |
135
|
1 |
|
$io->error("http://$address is taken by another process."); |
136
|
1 |
|
return self::EXIT_CODE_ADDRESS_TAKEN_BY_ANOTHER_PROCESS; |
137
|
|
|
} |
138
|
|
|
|
139
|
4 |
|
if ($router !== null && !file_exists($router)) { |
140
|
1 |
|
$io->error("Routing file \"$router\" does not exist."); |
141
|
1 |
|
return self::EXIT_CODE_NO_ROUTING_FILE; |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
$command = []; |
145
|
|
|
|
146
|
3 |
|
$isLinux = DIRECTORY_SEPARATOR !== '\\'; |
147
|
|
|
|
148
|
3 |
|
if ($isLinux) { |
149
|
3 |
|
$command[] = 'PHP_CLI_SERVER_WORKERS=' . $workers; |
150
|
|
|
} |
151
|
|
|
|
152
|
3 |
|
$xDebugInstalled = extension_loaded('xdebug'); |
153
|
3 |
|
$xDebugEnabled = $isLinux && $xDebugInstalled && $input->hasOption('xdebug') && $input->getOption('xdebug'); |
154
|
|
|
|
155
|
3 |
|
if ($xDebugEnabled) { |
156
|
|
|
$command[] = 'XDEBUG_MODE=debug XDEBUG_TRIGGER=yes'; |
157
|
|
|
} |
158
|
3 |
|
$outputTable = []; |
159
|
3 |
|
$outputTable[] = ['PHP', PHP_VERSION]; |
160
|
3 |
|
$outputTable[] = [ |
161
|
3 |
|
'xDebug', |
162
|
3 |
|
$xDebugInstalled ? sprintf( |
163
|
3 |
|
'%s, %s', |
164
|
3 |
|
phpversion('xdebug'), |
165
|
3 |
|
$xDebugEnabled ? '<info>enabled</>' : '<error>disabled</>', |
166
|
3 |
|
) : '<error>Not installed</>', |
167
|
3 |
|
]; |
168
|
3 |
|
$outputTable[] = ['Workers', $isLinux ? $workers : 'Not supported']; |
169
|
3 |
|
$outputTable[] = ['Address', $address]; |
170
|
3 |
|
$outputTable[] = ['Document root', $documentRoot]; |
171
|
3 |
|
$outputTable[] = ($router ? ['Routing file', $router] : []); |
172
|
|
|
|
173
|
3 |
|
$io->table(['Configuration'], $outputTable); |
174
|
|
|
|
175
|
3 |
|
$command[] = '"' . PHP_BINARY . '"' . " -S $address -t \"$documentRoot\" $router"; |
176
|
3 |
|
$command = implode(' ', $command); |
177
|
|
|
|
178
|
3 |
|
$output->writeln([ |
179
|
3 |
|
'Executing: ', |
180
|
3 |
|
sprintf('<info>%s</>', $command), |
181
|
3 |
|
], OutputInterface::VERBOSITY_VERBOSE); |
182
|
|
|
|
183
|
3 |
|
$io->success('Quit the server with CTRL-C or COMMAND-C.'); |
184
|
|
|
|
185
|
3 |
|
if ($env === 'test') { |
186
|
3 |
|
return ExitCode::OK; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
passthru($command, $result); |
190
|
|
|
|
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $address The server address. |
196
|
|
|
* |
197
|
|
|
* @return bool If address is already in use. |
198
|
|
|
*/ |
199
|
5 |
|
private function isAddressTaken(string $address): bool |
200
|
|
|
{ |
201
|
5 |
|
[$hostname, $port] = explode(':', $address); |
202
|
5 |
|
$fp = @fsockopen($hostname, (int) $port, $errno, $errstr, 3); |
203
|
|
|
|
204
|
5 |
|
if ($fp === false) { |
205
|
4 |
|
return false; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
fclose($fp); |
209
|
1 |
|
return true; |
210
|
|
|
} |
211
|
|
|
|
212
|
6 |
|
private function getRootPath(): string |
213
|
|
|
{ |
214
|
6 |
|
if ($this->appRootPath !== null) { |
215
|
|
|
return rtrim($this->appRootPath, DIRECTORY_SEPARATOR); |
216
|
|
|
} |
217
|
|
|
|
218
|
6 |
|
return getcwd(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|