Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Exception;

class InvalidMiddlewareException extends \Exception
{
public function __construct(public string $className, int $code = 0, ?\Throwable $previous = null)
{
parent::__construct(
"The middleware class '$className' is invalid.",
$code,
$previous
);
}
}
10 changes: 10 additions & 0 deletions src/frankenphp-symfony/src/Middleware/MiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Middleware;

interface MiddlewareInterface
{
public function wrap(callable $handler, array $server): void;
}
15 changes: 15 additions & 0 deletions src/frankenphp-symfony/src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Runtime\FrankenPhpSymfony;

use Runtime\FrankenPhpSymfony\Exception\InvalidMiddlewareException;
use Runtime\FrankenPhpSymfony\Middleware\MiddlewareInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
Expand All @@ -19,9 +21,13 @@ class Runner implements RunnerInterface
public function __construct(
private HttpKernelInterface $kernel,
private int $loopMax,
private array $middlewares = []
) {
}

/**
* @throws InvalidMiddlewareException
*/
public function run(): int
{
// Prevent worker script termination when a client connection is interrupted
Expand All @@ -47,6 +53,15 @@ public function run(): int
$sfResponse->send();
};

foreach ($this->middlewares as $middlewareClass) {
if (!is_a($middlewareClass, MiddlewareInterface::class, true)) {
throw new InvalidMiddlewareException($middlewareClass, 1761117929733);
}

$middleware = new $middlewareClass();
$handler = fn () => $middleware->wrap($handler, $server);
}

$loops = 0;
do {
$ret = \frankenphp_handle_request($handler);
Expand Down
3 changes: 3 additions & 0 deletions src/frankenphp-symfony/src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class Runtime extends SymfonyRuntime
/**
* @param array{
* frankenphp_loop_max?: int,
* frankenphp_middlewares?: string
* } $options
*/
public function __construct(array $options = [])
{
$options['frankenphp_loop_max'] = (int) ($options['frankenphp_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? 500);
$options['frankenphp_middlewares'] = (string) ($options['frankenphp_middlewares'] ?? $_SERVER['FRANKENPHP_MIDDLEWARES'] ?? $_ENV['FRANKENPHP_MIDDLEWARES'] ?? '');
$options['frankenphp_middlewares'] = array_filter(explode("\n", $options['frankenphp_middlewares']));

parent::__construct($options);
}
Expand Down
63 changes: 53 additions & 10 deletions src/frankenphp-symfony/tests/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
require_once __DIR__.'/function-mock.php';

use PHPUnit\Framework\TestCase;
use Runtime\FrankenPhpSymfony\Exception\InvalidMiddlewareException;
use Runtime\FrankenPhpSymfony\Runner;
use Runtime\FrankenPhpSymfony\Tests\Support\InvalidMiddleware;
use Runtime\FrankenPhpSymfony\Tests\Support\TestMiddleware;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -22,22 +25,62 @@ interface TestAppInterface extends HttpKernelInterface, TerminableInterface
*/
class RunnerTest extends TestCase
{
public function testRun(): void
public static function runData(): iterable
{
yield 'basic' => [];

yield 'middleware' => [
'middleware' => TestMiddleware::class,
];

yield 'Invalid middleware' => [
'middleware' => InvalidMiddleware::class,
'expectException' => InvalidMiddlewareException::class,
];
}

/**
* @dataProvider runData
*/
public function testRun(
?string $middleware = null,
?string $expectException = null,
): void {
$application = $this->createMock(TestAppInterface::class);
$application
->expects($this->once())
->method('handle')
->willReturnCallback(function (Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true): Response {
$this->assertSame('bar', $request->server->get('FOO'));

return new Response();
});
$application->expects($this->once())->method('terminate');
if (null === $expectException) {
$application
->expects($this->once())
->method('handle')
->willReturnCallback(
function (
Request $request,
int $type = HttpKernelInterface::MAIN_REQUEST,
bool $catch = true
): Response {
$this->assertSame('bar', $request->server->get('FOO'));

return new Response();
}
);
$application->expects($this->once())->method('terminate');
} else {
$this->expectException($expectException);
}

$_SERVER['FOO'] = 'bar';

$runner = new Runner($application, 500);
$runner = new Runner($application, 500, array_filter([$middleware]));

$assertMiddlewareInvoked = null === $expectException && $middleware && method_exists($middleware, 'isInvoked');
if ($assertMiddlewareInvoked) {
$this->assertFalse($middleware::isInvoked());
}

$this->assertSame(0, $runner->run());

if ($assertMiddlewareInvoked) {
$this->assertTrue($middleware::isInvoked());
}
}
}
9 changes: 9 additions & 0 deletions src/frankenphp-symfony/tests/Support/InvalidMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Tests\Support;

class InvalidMiddleware
{
}
28 changes: 28 additions & 0 deletions src/frankenphp-symfony/tests/Support/TestMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Tests\Support;

use Runtime\FrankenPhpSymfony\Middleware\MiddlewareInterface;

class TestMiddleware implements MiddlewareInterface
{
public function __construct()
{
self::$invoked = false;
}

private static bool $invoked = false;

public function wrap(callable $handler, array $server): void
{
self::$invoked = true;
$handler();
}

public static function isInvoked(): bool
{
return self::$invoked;
}
}
Loading