-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerCommand.php
More file actions
155 lines (130 loc) · 4.86 KB
/
ServerCommand.php
File metadata and controls
155 lines (130 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Stasis\Console\Command;
use Stasis\Console\CommandFactoryInterface;
use Stasis\Generator\Distribution\DistributionInterface;
use Stasis\Generator\Distribution\LocalDistributionInterface;
use Stasis\Kernel;
use Stasis\Server\Server;
use Stasis\Server\ServerFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressIndicator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @internal
*/
class ServerCommand extends Command implements CommandFactoryInterface
{
private const string NAME = 'server';
private const string DESCRIPTION = 'Start the development server';
private const string OPTION_HOST = 'host';
private const string OPTION_PORT = 'port';
private ?Server $server = null;
#[\Override]
public static function name(): string
{
return self::NAME;
}
#[\Override]
public static function description(): string
{
return self::DESCRIPTION;
}
#[\Override]
public static function create(Kernel $kernel): self
{
$distribution = $kernel->distribution();
$serverFactory = new ServerFactory();
return new self($distribution, $serverFactory);
}
public function __construct(
private readonly DistributionInterface $distribution,
private readonly ServerFactory $serverFactory,
) {
parent::__construct(self::NAME);
}
/** @return array<int> */
#[\Override]
public function getSubscribedSignals(): array
{
return [2, 15]; // SIGINT, SIGTERM
}
#[\Override]
public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
{
$this->server?->stop();
return false;
}
#[\Override]
protected function configure(): void
{
$this
->setDescription(self::DESCRIPTION)
->addOption(self::OPTION_HOST, null, InputOption::VALUE_REQUIRED, 'Host to run the server on', 'localhost')
->addOption(self::OPTION_PORT, null, InputOption::VALUE_REQUIRED, 'Port to run the server on', 8000)
->setHelp('Stasis development server is utilizing PHP built-in web server. Does NOT meant to be used in production environment.')
;
}
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->distribution instanceof LocalDistributionInterface) {
$output->writeln('Configured distribution does not support local server.');
return Command::FAILURE;
}
$path = $this->distribution->path();
$host = $input->getOption(self::OPTION_HOST);
$port = (int) $input->getOption(self::OPTION_PORT);
$this->printStartMessage($output, $path, $host, $port);
$server = $this->serverFactory->create($path, $host, $port);
$this->server = $server;
$server->start();
$indicator = $this->createIndicator($output);
$indicator->start('Server is running');
while (true) {
$stdoutContent = $server->getStdOutContents();
$output->write($stdoutContent, false, OutputInterface::OUTPUT_RAW | OutputInterface::VERBOSITY_VERBOSE);
$stderrContents = $server->getStdErrContents();
$output->write($stderrContents, false, OutputInterface::OUTPUT_RAW | OutputInterface::VERBOSITY_VERBOSE);
if (!$server->isRunning()) {
break;
}
usleep(200_000); // 0.2 sec
$indicator->advance();
}
$indicator->finish('Server stopped');
return $server->stop();
}
private function printStartMessage(OutputInterface $output, string $path, string $host, int $port): void
{
$url = sprintf('http://%s:%d', $host, $port);
$output->writeln(' Stasis Development Server');
$output->writeln('');
$output->writeln(sprintf(' Address: <href=%s><fg=cyan;options=underscore>%s</></>', $url, $url));
$output->writeln(sprintf(' Distribution: <fg=yellow>%s</>', $path));
$output->writeln('');
}
private function createIndicator(OutputInterface $output): ProgressIndicator
{
$progressOutput = $output->isVerbose() ? new NullOutput() : $output;
return new ProgressIndicator(
$progressOutput,
'normal',
200,
[
'<fg=green>⠏</>',
'<fg=green>⠛</>',
'<fg=green>⠹</>',
'<fg=green>⢸</>',
'<fg=green>⣰</>',
'<fg=green>⣤</>',
'<fg=green>⣆</>',
'<fg=green>⡇</>',
],
'<fg=red>⠿</>',
);
}
}