Skip to content

Commit b1f1cb5

Browse files
authored
Adding pure Laminas PSR runtime (#23)
* Adding pure Laminas PSR runtime * normalize * CS fix
0 parents  commit b1f1cb5

17 files changed

+401
-0
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
github: [nyholm]

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Tobias Nyholm
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# PSR-7 and PSR-15 Runtime
2+
3+
A runtime with `laminas/diactoros` and `laminas/laminas-httphandlerrunner`.
4+
5+
## Installation
6+
7+
```
8+
composer require runtime/psr-laminas
9+
```
10+
11+
## Usage
12+
13+
This runtime is discovered automatically. You can force your application to use
14+
this runtime by defining the environment variable `APP_RUNTIME`.
15+
16+
```
17+
APP_RUNTIME=Runtime\PsrLaminas\Runtime
18+
```
19+
20+
### PSR-7
21+
22+
```php
23+
// public/index.php
24+
25+
use Psr\Http\Server\RequestHandlerInterface;
26+
27+
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
28+
29+
return function (ServerRequestInterface $request) {
30+
$response = new \Laminas\Diactoros\Response();
31+
$response->getBody()->write('PSR-7');
32+
33+
return $response;
34+
};
35+
```
36+
37+
### PSR-15
38+
39+
```php
40+
// public/index.php
41+
42+
use Psr\Http\Server\RequestHandlerInterface;
43+
use Psr\Http\Message\ServerRequestInterface;
44+
use Psr\Http\Message\ResponseInterface;
45+
46+
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
47+
48+
class Application implements RequestHandlerInterface {
49+
// ...
50+
public function handle(ServerRequestInterface $request): ResponseInterface
51+
{
52+
$response = new \Laminas\Diactoros\Response();
53+
$response->getBody()->write('PSR-15');
54+
55+
return $response;
56+
}
57+
}
58+
59+
return function (array $context) {
60+
return new Application($context['APP_ENV'] ?? 'dev');
61+
};
62+
```

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "runtime/psr-laminas",
3+
"type": "library",
4+
"description": "PSR runtime with laminas/diactoros and laminas/laminas-httphandlerrunner",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Tobias Nyholm",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"laminas/laminas-diactoros": "^2.5",
14+
"laminas/laminas-httphandlerrunner": "^1.2",
15+
"symfony/runtime": "5.x-dev"
16+
},
17+
"require-dev": {
18+
"symfony/phpunit-bridge": "^5.2"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Runtime\\PsrLaminas\\": "src/",
23+
"Symfony\\Runtime\\Psr\\Http\\": "runtime/"
24+
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"Runtime\\PsrLaminas\\Tests\\": "tests/"
29+
}
30+
},
31+
"minimum-stability": "dev",
32+
"prefer-stable": true
33+
}

phpunit.xml.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
bootstrap="vendor/autoload.php"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1"/>
12+
</php>
13+
<testsuites>
14+
<testsuite name="Test Suite">
15+
<directory>./tests</directory>
16+
<directory suffix=".phpt">./tests/phpt</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Runtime\Psr\Http\Message;
4+
5+
use Runtime\PsrLaminas\Runtime;
6+
7+
class ResponseInterfaceRuntime extends Runtime
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Runtime\Psr\Http\Message;
4+
5+
use Runtime\PsrLaminas\Runtime;
6+
7+
class ServerRequestInterfaceRuntime extends Runtime
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Runtime\Psr\Http\Server;
4+
5+
use Runtime\PsrLaminas\Runtime;
6+
7+
class RequestHandlerInterfaceRuntime extends Runtime
8+
{
9+
}

src/Emitter.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Runtime\PsrLaminas;
4+
5+
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
use Psr\Http\Server\RequestHandlerInterface;
9+
use Symfony\Component\Runtime\RunnerInterface;
10+
11+
/**
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class Emitter implements RunnerInterface
15+
{
16+
private $requestHandler;
17+
private $response;
18+
private $request;
19+
private $emitter;
20+
21+
private function __construct(array $options)
22+
{
23+
$class = $options['emitter'] ?? SapiEmitter::class;
24+
25+
if (!class_exists($class)) {
26+
throw new \LogicException(sprintf('The class "%s" cannot be found.', $class));
27+
}
28+
29+
$this->emitter = new $class();
30+
}
31+
32+
/**
33+
* @param array{emitter?: ?string} $options
34+
*/
35+
public static function createForResponse(ResponseInterface $response, array $options): self
36+
{
37+
$emitter = new self($options);
38+
$emitter->response = $response;
39+
40+
return $emitter;
41+
}
42+
43+
/**
44+
* @param array{emitter?: ?string} $options
45+
*/
46+
public static function createForRequestHandler(RequestHandlerInterface $handler, ServerRequestInterface $request, array $options): self
47+
{
48+
$emitter = new self($options);
49+
$emitter->requestHandler = $handler;
50+
$emitter->request = $request;
51+
52+
return $emitter;
53+
}
54+
55+
public function run(): int
56+
{
57+
if (null === $this->response) {
58+
$this->response = $this->requestHandler->handle($this->request);
59+
}
60+
61+
$this->emitter->emit($this->response);
62+
63+
return 0;
64+
}
65+
}

0 commit comments

Comments
 (0)