Skip to content

Commit

Permalink
feat: add a runtime for FrankenPHP with Symfony (#124)
Browse files Browse the repository at this point in the history
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
3 people authored Oct 15, 2022
0 parents commit 81ade34
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: [nyholm]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## 0.1.0

First version
21 changes: 21 additions & 0 deletions LICENSE
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.
40 changes: 40 additions & 0 deletions README.md
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']);
};
```
38 changes: 38 additions & 0 deletions composer.json
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
}
}
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
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>
47 changes: 47 additions & 0 deletions src/Runner.php
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;
}
}
26 changes: 26 additions & 0 deletions src/Runtime.php
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);
}
}
43 changes: 43 additions & 0 deletions tests/RunnerTest.php
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());
}
}
28 changes: 28 additions & 0 deletions tests/RuntimeTest.php
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));
}
}
10 changes: 10 additions & 0 deletions tests/function-mock.php
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;
}
}

0 comments on commit 81ade34

Please sign in to comment.