-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDevUpCommand.php
More file actions
193 lines (172 loc) · 7.34 KB
/
DevUpCommand.php
File metadata and controls
193 lines (172 loc) · 7.34 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Marko\DevServer\Command;
use Marko\Config\ConfigRepositoryInterface;
use Marko\Config\Exceptions\ConfigNotFoundException;
use Marko\Core\Attributes\Command;
use Marko\Core\Command\CommandInterface;
use Marko\Core\Command\Input;
use Marko\Core\Command\Output;
use Marko\Core\Path\ProjectPaths;
use Marko\DevServer\Detection\DockerDetector;
use Marko\DevServer\Detection\FrontendDetector;
use Marko\DevServer\Detection\PubSubDetector;
use Marko\DevServer\Exceptions\DevServerException;
use Marko\DevServer\Process\PidFile;
use Marko\DevServer\Process\ProcessEntry;
use Marko\DevServer\Process\ProcessManager;
/** @noinspection PhpUnused */
#[Command(name: 'dev:up', description: 'Start the development environment', aliases: ['up'])]
readonly class DevUpCommand implements CommandInterface
{
public function __construct(
private ConfigRepositoryInterface $config,
private DockerDetector $dockerDetector,
private FrontendDetector $frontendDetector,
private PubSubDetector $pubsubDetector,
private PidFile $pidFile,
private ProcessManager $processManager,
private ProjectPaths $paths,
) {}
/**
* @throws ConfigNotFoundException|DevServerException
*/
public function execute(
Input $input,
Output $output,
): int {
$port = (int) ($input->getOption('port') ?? $input->getOption('p') ?? $this->config->getInt('dev.port'));
$foreground = $input->hasOption('foreground') || $input->hasOption('f');
$host = $input->getOption('host') ?? $input->getOption('h') ?? $this->config->getString('dev.host');
$detach = !$foreground && ($input->hasOption('detach') || $input->hasOption('d') || $this->config->getBool(
'dev.detach',
));
$dockerConfig = $this->config->get('dev.docker');
$frontendConfig = $this->config->get('dev.frontend');
$pubsubConfig = $this->config->get('dev.pubsub');
// Guard: check if services are already running
$existingEntries = $this->pidFile->read();
foreach ($existingEntries as $entry) {
if ($this->pidFile->isRunning($entry->pid)) {
throw new DevServerException(
message: 'Development environment is already running.',
context: "Process '{$entry->name}' (PID {$entry->pid}) is still active",
suggestion: "Stop the existing environment first with 'marko down', then run 'marko up' again.",
);
}
}
$indexPath = $this->paths->base . '/public/index.php';
if (!file_exists($indexPath)) {
throw new DevServerException(
message: 'Cannot start PHP server: public/index.php not found.',
context: "While starting PHP development server (expected at $indexPath)",
suggestion: "Create public/index.php with:\n\n" .
"<?php\n\n" .
"declare(strict_types=1);\n\n" .
"require __DIR__ . '/../vendor/autoload.php';\n\n" .
"use Marko\\Core\\Application;\n\n" .
"\$app = Application::boot(dirname(__DIR__));\n" .
"\$app->handleRequest();\n",
);
}
$output->writeLine('Starting development environment...');
$entries = [];
$startProcess = $detach
? $this->processManager->startDetached(...)
: $this->processManager->start(...);
// Docker
if ($dockerConfig !== false) {
$dockerCommand = is_string($dockerConfig)
? $dockerConfig
: $this->dockerDetector->detect()['upCommand'] ?? null;
if ($dockerCommand !== null) {
$output->writeLine(" Starting Docker: $dockerCommand");
$pid = $startProcess('docker', $dockerCommand);
$entries[] = new ProcessEntry(
name: 'docker',
pid: $pid,
command: $dockerCommand,
port: 0,
startedAt: date('c'),
);
}
}
// Frontend
if ($frontendConfig !== false) {
$frontendCommand = is_string($frontendConfig)
? $frontendConfig
: $this->frontendDetector->detect();
if ($frontendCommand !== null) {
$output->writeLine(" Starting frontend: $frontendCommand");
$pid = $startProcess('frontend', $frontendCommand);
$entries[] = new ProcessEntry(
name: 'frontend',
pid: $pid,
command: $frontendCommand,
port: 0,
startedAt: date('c'),
);
}
}
// Pub/Sub listener
if ($pubsubConfig !== false) {
$pubsubCommand = is_string($pubsubConfig)
? $pubsubConfig
: $this->pubsubDetector->detect();
if ($pubsubCommand !== null) {
$output->writeLine(" Starting pub/sub listener: $pubsubCommand");
$pid = $startProcess('pubsub', $pubsubCommand);
$entries[] = new ProcessEntry(
name: 'pubsub',
pid: $pid,
command: $pubsubCommand,
port: 0,
startedAt: date('c'),
);
}
}
// Custom processes
/** @var array<string, string> $processes */
$processes = $this->config->get('dev.processes');
foreach ($processes as $name => $processCommand) {
$output->writeLine(" Starting $name: $processCommand");
$pid = $startProcess($name, $processCommand);
$entries[] = new ProcessEntry(
name: $name,
pid: $pid,
command: $processCommand,
port: 0,
startedAt: date('c'),
);
}
// PHP server (always) — multiple workers needed for SSE
$phpCommand = "env PHP_CLI_SERVER_WORKERS=4 php -S {$host}:{$port} -t public/";
$output->writeLine(" Starting PHP server: php -S {$host}:{$port}");
$pid = $startProcess('php', $phpCommand);
// In foreground mode, verify PHP server is alive — if it died, port is likely in use.
// In detached mode, startDetached() already checks for immediate failure.
if (!$detach) {
usleep(100000); // 100ms — give the server time to attempt binding
if (!$this->processManager->isRunning('php')) {
throw DevServerException::portInUse($port);
}
}
$entries[] = new ProcessEntry(
name: 'php',
pid: $pid,
command: $phpCommand,
port: $port,
startedAt: date('c'),
);
if ($detach) {
$this->pidFile->write($entries);
$output->writeLine('Development environment started in background.');
$output->writeLine("Run 'marko dev:status' to check status.");
$output->writeLine("Run 'marko dev:down' to stop.");
} else {
$output->writeLine('Development environment running. Press Ctrl+C to stop.');
$this->processManager->runForeground();
}
return 0;
}
}