Skip to content

Commit edd0d85

Browse files
committed
wip
1 parent b2e0c75 commit edd0d85

File tree

3 files changed

+122
-4
lines changed

3 files changed

+122
-4
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM dunglas/frankenphp
2+
3+
COPY . /app/public
4+
5+
RUN install-php-extensions \
6+
pdo_mysql \
7+
gd \
8+
intl \
9+
zip \
10+
opcache
11+
12+
ENV FRANKENPHP_CONFIG="worker ./public/index.php"

packages/router/src/Commands/ServeCommand.php

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@
55
namespace Tempest\Router\Commands;
66

77
use Tempest\Console\ConsoleCommand;
8+
use Tempest\Console\HasConsole;
89
use Tempest\Intl\Number;
910
use Tempest\Support\Str;
11+
use function Tempest\Support\path;
1012

1113
final readonly class ServeCommand
1214
{
15+
use HasConsole;
16+
1317
#[ConsoleCommand(
1418
name: 'serve',
1519
description: 'Starts a PHP development server',
1620
)]
17-
public function __invoke(string $host = '127.0.0.1', int $port = 8000, string $publicDir = 'public/'): void
18-
{
19-
$routerFile = __DIR__ . '/router.php';
20-
21+
public function __invoke(
22+
string $host = 'localhost',
23+
int $port = 8000,
24+
int $httpsPort = 4433,
25+
string $publicDir = './public/',
26+
bool $worker = false,
27+
): void {
2128
if (Str\contains($host, ':')) {
2229
[$host, $overriddenPort] = explode(':', $host, limit: 2);
2330

@@ -26,6 +33,39 @@ public function __invoke(string $host = '127.0.0.1', int $port = 8000, string $p
2633
$port = Number\parse($overriddenPort, default: $port);
2734
}
2835

36+
if ($worker) {
37+
$this->worker($host, $port, $httpsPort, $publicDir);
38+
} else {
39+
$this->serve($host, $port, $publicDir);
40+
}
41+
}
42+
43+
private function worker(string $host, int $port, int $httpsPort, string $publicDir): void
44+
{
45+
$this->success('Listening on http://' . $host . ':' . $port . ', https://' . $host . ':' . $httpsPort);
46+
47+
$command = sprintf(<<<'SH'
48+
docker run \
49+
-e FRANKENPHP_CONFIG="worker %s" \
50+
-v $PWD:/app \
51+
-p %d:80 -p %d:443 -p %d:443/udp \
52+
dunglas/frankenphp
53+
SH,
54+
path($publicDir, 'index.php')->toString(),
55+
$port,
56+
$httpsPort,
57+
$httpsPort
58+
);
59+
60+
$this->info($command);
61+
62+
passthru($command);
63+
}
64+
65+
private function serve(string $host, int $port, string $publicDir): void
66+
{
67+
$routerFile = __DIR__ . '/router.php';
68+
2969
passthru("php -S {$host}:{$port} -t {$publicDir} {$routerFile}");
3070
}
3171
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Router;
6+
7+
use Tempest\Container\Container;
8+
use Tempest\Container\Singleton;
9+
use Tempest\Core\Application;
10+
use Tempest\Core\DeferredTasks;
11+
use Tempest\Core\Kernel;
12+
use Tempest\Core\Kernel\FinishDeferredTasks;
13+
use Tempest\Core\Tempest;
14+
use Tempest\Http\RequestFactory;
15+
use Tempest\Http\Session\Session;
16+
use Tempest\Log\Channels\AppendLogChannel;
17+
use Tempest\Log\LogConfig;
18+
19+
use function Tempest\env;
20+
use function Tempest\Support\path;
21+
22+
#[Singleton]
23+
final readonly class WorkerApplication implements Application
24+
{
25+
public function __construct(
26+
private Container $container,
27+
) {}
28+
29+
/** @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
30+
public static function boot(
31+
string $root,
32+
array $discoveryLocations = [],
33+
): self {
34+
$container = Tempest::boot($root, $discoveryLocations);
35+
36+
$application = $container->get(WorkerApplication::class);
37+
38+
// Application-specific setup
39+
$logConfig = $container->get(LogConfig::class);
40+
41+
if ($logConfig->debugLogPath === null && $logConfig->serverLogPath === null && $logConfig->channels === []) {
42+
$logConfig->debugLogPath = path($container->get(Kernel::class)->root, '/log/debug.log')->toString();
43+
$logConfig->serverLogPath = env('SERVER_LOG');
44+
$logConfig->channels[] = new AppendLogChannel(path($root, '/log/tempest.log')->toString());
45+
}
46+
47+
return $application;
48+
}
49+
50+
public function run(): void
51+
{
52+
$router = $this->container->get(Router::class);
53+
54+
$psrRequest = $this->container->get(RequestFactory::class)->make();
55+
56+
$responseSender = $this->container->get(ResponseSender::class);
57+
58+
$responseSender->send(
59+
$router->dispatch($psrRequest),
60+
);
61+
62+
$this->container->get(Session::class)->cleanup();
63+
64+
$this->container->invoke(FinishDeferredTasks::class);
65+
}
66+
}

0 commit comments

Comments
 (0)