|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
declare(ticks=1); |
|
7
|
|
|
|
|
8
|
|
|
namespace Yiisoft\Yii\Debug\Command; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
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\SignalRegistry\SignalRegistry; |
|
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
16
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
|
17
|
|
|
use Yiisoft\Yii\Debug\DebugServer\Connection; |
|
18
|
|
|
|
|
19
|
|
|
final class DebugServerCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
public const COMMAND_NAME = 'dev'; |
|
22
|
|
|
protected static $defaultName = self::COMMAND_NAME; |
|
23
|
|
|
|
|
24
|
|
|
protected static $defaultDescription = 'Runs PHP built-in web server'; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
private SignalRegistry $signalRegistry, |
|
28
|
|
|
private string $address = '0.0.0.0', |
|
29
|
|
|
private int $port = 8890, |
|
30
|
|
|
) { |
|
31
|
|
|
parent::__construct(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function configure(): void |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->setHelp( |
|
38
|
|
|
'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.' |
|
39
|
|
|
) |
|
40
|
|
|
->addOption('address', 'a', InputOption::VALUE_OPTIONAL, 'Host to serve at', $this->address) |
|
41
|
|
|
->addOption('port', 'p', InputOption::VALUE_OPTIONAL, 'Port to serve at', $this->port) |
|
42
|
|
|
->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
46
|
|
|
{ |
|
47
|
|
|
$io = new SymfonyStyle($input, $output); |
|
48
|
|
|
$io->title('Yii3 Debug Server'); |
|
49
|
|
|
$io->writeln('https://yiiframework.com' . "\n"); |
|
50
|
|
|
|
|
51
|
|
|
$env = $input->getOption('env'); |
|
52
|
|
|
if ($env === 'test') { |
|
53
|
|
|
return ExitCode::OK; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$socket = Connection::create(); |
|
57
|
|
|
$socket->bind(); |
|
58
|
|
|
|
|
59
|
|
|
//if (socket_recvfrom($socket, $buf, 64 * 1024, 0, $source) === false) { |
|
60
|
|
|
// echo "recv_from failed"; |
|
61
|
|
|
//} |
|
62
|
|
|
//var_dump($buf); |
|
63
|
|
|
// |
|
64
|
|
|
//return 0; |
|
65
|
|
|
//if (!@socket_bind($socket, $address, $port)) { |
|
66
|
|
|
// $io->error( |
|
67
|
|
|
// sprintf( |
|
68
|
|
|
// 'Address "%s" is already taken by another process.', |
|
69
|
|
|
// $this->address . ':' . $this->port, |
|
70
|
|
|
// ) |
|
71
|
|
|
// ); |
|
72
|
|
|
// $io->info( |
|
73
|
|
|
// sprintf( |
|
74
|
|
|
// 'Would you like to kill the process and rerun?' |
|
75
|
|
|
// ) |
|
76
|
|
|
// ); |
|
77
|
|
|
// $result = $io->ask('Would you like to kill the process and rerun? (Y/n)'); |
|
78
|
|
|
// |
|
79
|
|
|
// if ($result === 'Y') { |
|
80
|
|
|
// // todo: change to finding a process by opened port |
|
81
|
|
|
// $pid = shell_exec( |
|
82
|
|
|
// sprintf( |
|
83
|
|
|
// 'ps | grep "php ./yii dev"', |
|
84
|
|
|
// //$this->port, |
|
85
|
|
|
// ) |
|
86
|
|
|
// ); |
|
87
|
|
|
// $io->info( |
|
88
|
|
|
// sprintf( |
|
89
|
|
|
// 'Killing the process with ID: "%s".', |
|
90
|
|
|
// $pid, |
|
91
|
|
|
// ) |
|
92
|
|
|
// ); |
|
93
|
|
|
// //shell_exec('kill ' . $pid); |
|
94
|
|
|
// } |
|
95
|
|
|
// //return ExitCode::IOERR; |
|
96
|
|
|
//} |
|
97
|
|
|
|
|
98
|
|
|
$io->success( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
'Listening on "%s".', |
|
101
|
|
|
$socket->getUri(), |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
|
|
105
|
|
|
if (\function_exists('pcntl_signal')) { |
|
106
|
|
|
$io->success('Quit the server with CTRL-C or COMMAND-C.'); |
|
107
|
|
|
|
|
108
|
|
|
\pcntl_signal(\SIGINT, static function () use ($socket): void { |
|
109
|
|
|
$socket->close(); |
|
110
|
|
|
exit(1); |
|
|
|
|
|
|
111
|
|
|
}); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
foreach ($socket->read() as $message) { |
|
115
|
|
|
switch ($message[0]) { |
|
116
|
|
|
case Connection::TYPE_ERROR: |
|
117
|
|
|
$io->writeln('Connection closed with error: ' . $message[1]); |
|
118
|
|
|
break 2; |
|
119
|
|
|
default: |
|
120
|
|
|
$data = \json_decode($message[1], null, 512, JSON_THROW_ON_ERROR); |
|
121
|
|
|
if ($data[0] === Connection::MESSAGE_TYPE_VAR_DUMPER) { |
|
122
|
|
|
$io->title('VarDumper'); |
|
123
|
|
|
} elseif ($data[0] === Connection::MESSAGE_TYPE_LOGGER) { |
|
124
|
|
|
$io->write('Logger'); |
|
125
|
|
|
} |
|
126
|
|
|
$io->writeln( |
|
127
|
|
|
sprintf( |
|
128
|
|
|
"\033[1;37m\033[47m%s\033[0m", |
|
129
|
|
|
$data[1] |
|
130
|
|
|
) |
|
131
|
|
|
); |
|
132
|
|
|
$io->newLine(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
return ExitCode::OK; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.