1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
declare(ticks=1); |
5
|
|
|
|
6
|
|
|
namespace Yiisoft\Yii\Debug\Command; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
14
|
|
|
use Yiisoft\Yii\Debug\DebugServer\Connection; |
15
|
|
|
|
16
|
|
|
final class DebugServerBroadcastCommand extends Command |
17
|
|
|
{ |
18
|
|
|
public const COMMAND_NAME = 'dev:broadcast'; |
19
|
|
|
protected static $defaultName = self::COMMAND_NAME; |
20
|
|
|
|
21
|
|
|
protected static $defaultDescription = 'Runs PHP built-in web server'; |
22
|
|
|
|
23
|
|
|
public function configure(): void |
24
|
|
|
{ |
25
|
|
|
$this |
26
|
|
|
->setHelp( |
27
|
|
|
'Broadcasts a message to all connected clients.' |
28
|
|
|
) |
29
|
|
|
->addOption('message', 'm', InputOption::VALUE_OPTIONAL, 'A text to broadcast', 'Test message') |
30
|
|
|
->addOption('env', 'e', InputOption::VALUE_OPTIONAL, 'It is only used for testing.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
34
|
|
|
{ |
35
|
|
|
$io = new SymfonyStyle($input, $output); |
36
|
|
|
$io->title('Yii3 Debug Server'); |
37
|
|
|
$io->writeln('https://yiiframework.com' . "\n"); |
38
|
|
|
|
39
|
|
|
$env = $input->getOption('env'); |
40
|
|
|
if ($env === 'test') { |
41
|
|
|
return ExitCode::OK; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$socket = Connection::create(); |
45
|
|
|
if (\function_exists('pcntl_signal')) { |
46
|
|
|
$io->success('Quit the server with CTRL-C or COMMAND-C.'); |
47
|
|
|
|
48
|
|
|
\pcntl_signal(\SIGINT, static function () use ($socket): void { |
49
|
|
|
$socket->close(); |
50
|
|
|
exit(1); |
|
|
|
|
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$data = $input->getOption('message'); |
55
|
|
|
$socket->broadcast($data); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return ExitCode::OK; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.