-
-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add a runtime for FrankenPHP with Symfony #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# These are supported funding model platforms | ||
|
||
github: [nyholm] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Change Log | ||
|
||
## 0.1.0 | ||
|
||
First version |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 Tobias Nyholm | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# FrankenPHP Runtime for Symfony | ||
|
||
A runtime for [FrankenPHP](https://frankenphp.dev/). | ||
|
||
If you are new to the Symfony Runtime component, read more in the [main readme](https://github.com/php-runtime/runtime). | ||
|
||
## Installation | ||
|
||
``` | ||
composer require runtime/frankenphp-symfony | ||
``` | ||
|
||
## Usage | ||
|
||
Define the environment variable `APP_RUNTIME` for your application. | ||
|
||
``` | ||
// .env | ||
APP_RUNTIME=Runtime\FrankenPhpSymfony\Runtime | ||
``` | ||
|
||
``` | ||
// .rr.yaml | ||
server: | ||
... | ||
env: | ||
APP_RUNTIME: Runtime\FrankenPhpSymfony\Runtime | ||
``` | ||
|
||
```php | ||
// public/index.php | ||
|
||
use App\Kernel; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | ||
|
||
return function (array $context) { | ||
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | ||
}; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "runtime/frankenphp-symfony", | ||
"description": "FrankenPHP runtime for Symfony", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "Kévin Dunglas", | ||
"email": "kevin@dunglas.dev" | ||
} | ||
], | ||
"require": { | ||
"php": ">=8.2.0", | ||
"symfony/dependency-injection": "^5.4 || ^6.0", | ||
"symfony/http-kernel": "^5.4 || ^6.0", | ||
"symfony/runtime": "^5.4 || ^6.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.5" | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"autoload": { | ||
"psr-4": { | ||
"Runtime\\FrankenPhpSymfony\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Runtime\\FrankenPhpSymfony\\Tests\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"symfony/runtime": true | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1"/> | ||
</php> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Runtime\FrankenPhpSymfony; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
use Symfony\Component\Runtime\RunnerInterface; | ||
|
||
/** | ||
* A runner for FrankenPHP. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
class Runner implements RunnerInterface | ||
{ | ||
private HttpKernelInterface $kernel; | ||
|
||
public function __construct(HttpKernelInterface $kernel) | ||
{ | ||
$this->kernel = $kernel; | ||
} | ||
|
||
public function run(): int | ||
{ | ||
$server = array_filter($_SERVER, static fn (string $key) => !str_starts_with($key, 'HTTP_'), ARRAY_FILTER_USE_KEY); | ||
do { | ||
$ret = \frankenphp_handle_request(function () use ($server, &$sfRequest, &$sfResponse): void { | ||
// Merge the environment variables coming from DotEnv with the ones tight to the current request | ||
$_SERVER += $server; | ||
|
||
$sfRequest = Request::createFromGlobals(); | ||
$sfResponse = $this->kernel->handle($sfRequest); | ||
|
||
$sfResponse->send(); | ||
}); | ||
|
||
if ($this->kernel instanceof TerminableInterface && $sfRequest && $sfResponse) { | ||
$this->kernel->terminate($sfRequest, $sfResponse); | ||
} | ||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this part not be also inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frankenphp only cares about putting out the response. A "cleanup" or background process could be running outside IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It does. |
||
} while ($ret); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the review but maybe the callable could define its return type $server = array_filter($_SERVER, static fn (string $key): bool => !str_starts_with($key, 'HTTP_'), ARRAY_FILTER_USE_KEY); I'm not sure if the current lib tend to define return types on callable 🙁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually dont add return types on callables. It gives zero to little benefit. But it is just a matter of taste. As long as CI is not complaining, Im fine with adding return types. |
||
|
||
return 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Runtime\FrankenPhpSymfony; | ||
|
||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\Runtime\RunnerInterface; | ||
use Symfony\Component\Runtime\SymfonyRuntime; | ||
|
||
/** | ||
* A runtime for FrankenPHP. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
class Runtime extends SymfonyRuntime | ||
{ | ||
public function getRunner(?object $application): RunnerInterface | ||
{ | ||
if ($application instanceof HttpKernelInterface && ($_SERVER['FRANKENPHP_WORKER'] ?? false)) { | ||
return new Runner($application); | ||
} | ||
|
||
return parent::getRunner($application); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Runtime\FrankenPhpSymfony\Tests; | ||
|
||
require_once __DIR__.'/function-mock.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Runtime\FrankenPhpSymfony\Runner; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\TerminableInterface; | ||
|
||
interface TestAppInterface extends HttpKernelInterface, TerminableInterface | ||
{ | ||
} | ||
|
||
/** | ||
* @author Kévin Dunglas <kevin@dunglas.fr> | ||
*/ | ||
class RunnerTest extends TestCase | ||
{ | ||
public function testRun(): 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'); | ||
|
||
$_SERVER['FOO'] = 'bar'; | ||
|
||
$runner = new Runner($application); | ||
$this->assertSame(0, $runner->run()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Runtime\FrankenPhpSymfony\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Runtime\FrankenPhpSymfony\Runner; | ||
use Runtime\FrankenPhpSymfony\Runtime; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
/** | ||
* @author Kévin Dunglas <kevin@dunglas.dev> | ||
*/ | ||
final class RuntimeTest extends TestCase | ||
{ | ||
public function testGetRunner(): void | ||
{ | ||
$application = $this->createStub(HttpKernelInterface::class); | ||
|
||
$runtime = new Runtime(); | ||
$this->assertNotInstanceOf(Runner::class, $runtime->getRunner(null)); | ||
$this->assertNotInstanceOf(Runner::class, $runtime->getRunner($application)); | ||
|
||
$_SERVER['FRANKENPHP_WORKER'] = 1; | ||
$this->assertInstanceOf(Runner::class, $runtime->getRunner($application)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
if (!function_exists('frankenphp_handle_request')) { | ||
function frankenphp_handle_request(callable $callable): bool | ||
{ | ||
$callable(); | ||
|
||
return false; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.