Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
frankenphp {
worker {
file /app/public/index.php
watch
}
}
}
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM dunglas/frankenphp

RUN install-php-extensions \
pdo_mysql \
gd \
intl \
zip \
opcache
48 changes: 44 additions & 4 deletions packages/router/src/Commands/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@
namespace Tempest\Router\Commands;

use Tempest\Console\ConsoleCommand;
use Tempest\Console\HasConsole;
use Tempest\Intl\Number;
use Tempest\Support\Str;
use function Tempest\Support\path;

final readonly class ServeCommand
{
use HasConsole;

#[ConsoleCommand(
name: 'serve',
description: 'Starts a PHP development server',
)]
public function __invoke(string $host = '127.0.0.1', int $port = 8000, string $publicDir = 'public/'): void
{
$routerFile = __DIR__ . '/router.php';

public function __invoke(
string $host = 'localhost',
int $port = 8000,
int $httpsPort = 4433,
string $publicDir = './public/',
bool $worker = false,
): void {
if (Str\contains($host, ':')) {
[$host, $overriddenPort] = explode(':', $host, limit: 2);

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

if ($worker) {
$this->worker($host, $port, $httpsPort, $publicDir);
} else {
$this->serve($host, $port, $publicDir);
}
}

private function worker(string $host, int $port, int $httpsPort, string $publicDir): void
{
$command = sprintf(<<<'SH'
docker run \
-e FRANKENPHP_CONFIG="worker %s" \
-v $PWD:/app \
-p %d:80 -p %d:443 -p %d:443/udp \
tempest
SH,
path($publicDir, 'index.php')->toString(),
$port,
$httpsPort,
$httpsPort
);

$this->info('Listening on http://' . $host . ':' . $port . ', https://' . $host . ':' . $httpsPort);

$this->info($command);

passthru($command);
}

private function serve(string $host, int $port, string $publicDir): void
{
$routerFile = __DIR__ . '/router.php';

passthru("php -S {$host}:{$port} -t {$publicDir} {$routerFile}");
}
}
66 changes: 66 additions & 0 deletions packages/router/src/WorkerApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Tempest\Router;

use Tempest\Container\Container;
use Tempest\Container\Singleton;
use Tempest\Core\Application;
use Tempest\Core\DeferredTasks;
use Tempest\Core\Kernel;
use Tempest\Core\Kernel\FinishDeferredTasks;
use Tempest\Core\Tempest;
use Tempest\Http\RequestFactory;
use Tempest\Http\Session\Session;
use Tempest\Log\Channels\AppendLogChannel;
use Tempest\Log\LogConfig;

use function Tempest\env;
use function Tempest\Support\path;

#[Singleton]
final readonly class WorkerApplication implements Application
{
public function __construct(
private Container $container,
) {}

/** @param \Tempest\Discovery\DiscoveryLocation[] $discoveryLocations */
public static function boot(
string $root,
array $discoveryLocations = [],
): self {
$container = Tempest::boot($root, $discoveryLocations);

$application = $container->get(WorkerApplication::class);

// Application-specific setup
$logConfig = $container->get(LogConfig::class);

if ($logConfig->debugLogPath === null && $logConfig->serverLogPath === null && $logConfig->channels === []) {
$logConfig->debugLogPath = path($container->get(Kernel::class)->root, '/log/debug.log')->toString();
$logConfig->serverLogPath = env('SERVER_LOG');
$logConfig->channels[] = new AppendLogChannel(path($root, '/log/tempest.log')->toString());
}

return $application;
}

public function run(): void
{
$router = $this->container->get(Router::class);

$psrRequest = $this->container->get(RequestFactory::class)->make();

$responseSender = $this->container->get(ResponseSender::class);

$responseSender->send(
$router->dispatch($psrRequest),
);

$this->container->get(Session::class)->cleanup();

$this->container->invoke(FinishDeferredTasks::class);
}
}
33 changes: 28 additions & 5 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
<?php

declare(strict_types=1);

use Tempest\Discovery\DiscoveryLocation;
use Tempest\Router\HttpApplication;
use Tempest\Router\WorkerApplication;

require_once __DIR__ . '/../vendor/autoload.php';

HttpApplication::boot(__DIR__ . '/../', discoveryLocations: [
$discoveryLocations = [
new DiscoveryLocation('Tests\\Tempest\\Fixtures\\', __DIR__ . '/../tests/Fixtures/'),
])->run();
];

if (function_exists('frankenphp_handle_request')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't be enough, as this method exists even when FrankenPHP is running in non-worker mode, and the call below to this method will throw an exception.

This would be a proper check:

Suggested change
if (function_exists('frankenphp_handle_request')) {
$isWorkerMode = $_SERVER['FRANKENPHP_WORKER'] ?? false;
if ($isWorkerMode && function_exists('frankenphp_handle_request')) {

ignore_user_abort(true);

$application = WorkerApplication::boot(__DIR__ . '/../', discoveryLocations: $discoveryLocations);

$handler = static function () use ($application) {
$application->run();
};

$maxRequests = (int)($_SERVER['MAX_REQUESTS'] ?? 0);

for ($nbRequests = 0; ! $maxRequests || $nbRequests < $maxRequests; ++$nbRequests) {
$keepRunning = frankenphp_handle_request($handler);

gc_collect_cycles();

if (! $keepRunning) break;
}
} else {
HttpApplication::boot(__DIR__ . '/../', discoveryLocations: $discoveryLocations)->run();

exit();

}

exit();
Loading