-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a runtime for FrankenPHP with Symfony (#124)
Add a runtime for the worker mode of [FrankenPHP](https://frankenphp.dev). To be released this afternoon at ForumPHP! ![frankenphp](https://user-images.githubusercontent.com/57224/195830342-61650267-68a6-4f05-9c34-f8f099a6c9d4.png) Co-authored-by: Alexander Schranz <[email protected]> Co-authored-by: Nyholm <[email protected]>
- Loading branch information
Showing
12 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# These are supported funding model platforms | ||
|
||
github: [nyholm] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Change Log | ||
|
||
## 0.1.0 | ||
|
||
First version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "[email protected]" | ||
} | ||
], | ||
"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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} while ($ret); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |