Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: add middleware to FrankenPhpSymfony
  • Loading branch information
Scarbous committed Oct 22, 2025
commit 8964184c454afd494572e091c965b4a1cad3c758
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

Check warning on line 1 in src/frankenphp-symfony/src/Exception/InvalidMiddlewareException.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: global_namespace_import

Check warning on line 1 in src/frankenphp-symfony/src/Exception/InvalidMiddlewareException.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: visibility_required

namespace Runtime\FrankenPhpSymfony\Exception;

use Throwable;

class InvalidMiddlewareException extends \Exception
{
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

Check warning on line 1 in src/frankenphp-symfony/src/Middleware/MiddlewareInterface.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: visibility_required

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Middleware;

interface MiddlewareInterface
{
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
2 changes: 2 additions & 0 deletions src/frankenphp-symfony/src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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'] ?? '');

Check failure on line 26 in src/frankenphp-symfony/src/Runtime.php

View workflow job for this annotation

GitHub Actions / PHPStan

Offset 'frankenphp…' on array{frankenphp_loop_max: int} on left side of ?? does not exist.

Check failure on line 26 in src/frankenphp-symfony/src/Runtime.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArrayOffset

src/frankenphp-symfony/src/Runtime.php:26:56: InvalidArrayOffset: Cannot access value on variable $options using offset value of 'frankenphp_middlewares', expecting 'frankenphp_loop_max' (see https://psalm.dev/115)
$options['frankenphp_middlewares'] = array_filter(explode("\n", $options['frankenphp_middlewares']));

parent::__construct($options);
}
Expand Down
68 changes: 57 additions & 11 deletions src/frankenphp-symfony/tests/RunnerTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

Check warning on line 1 in src/frankenphp-symfony/tests/RunnerTest.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: yoda_style

Check warning on line 1 in src/frankenphp-symfony/tests/RunnerTest.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: trailing_comma_in_multiline

Check warning on line 1 in src/frankenphp-symfony/tests/RunnerTest.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: concat_space

Check warning on line 1 in src/frankenphp-symfony/tests/RunnerTest.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: class_attributes_separation

Check warning on line 1 in src/frankenphp-symfony/tests/RunnerTest.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: visibility_required

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Tests;

require_once __DIR__.'/function-mock.php';
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,65 @@
*/
class RunnerTest extends TestCase
{
public function testRun(): void

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 {
if ($expectException !== null) {
$this->expectException($expectException);
}

$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 ($expectException === null) {
$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');
}

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

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

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

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

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

Check warning on line 1 in src/frankenphp-symfony/tests/Support/InvalidMiddleware.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_blank_lines_after_class_opening

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

Check warning on line 1 in src/frankenphp-symfony/tests/Support/TestMiddleware.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: visibility_required

declare(strict_types=1);

namespace Runtime\FrankenPhpSymfony\Tests\Support;

use Runtime\FrankenPhpSymfony\Middleware\MiddlewareInterface;

class TestMiddleware implements MiddlewareInterface
{
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