Skip to content

Commit aabcbf8

Browse files
Stanislau KomarStanislau Komar
authored andcommitted
v1.6 release
1 parent 102114a commit aabcbf8

9 files changed

+337
-0
lines changed

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.github export-ignore
2+
/tests export-ignore
3+
/phpunit.xml.dist export-ignore
4+
/.gitattributes export-ignore
5+
/.gitignore export-ignore
6+
/.php-cs-fixer.dist.php export-ignore
7+
/psalm.xml export-ignore
8+
9+
*.php diff=php

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vendor
2+
composer.lock
3+
.phpunit.result.cache
4+
.php-cs-fixer.cache
5+
test-coverage-report
6+
phpunit.xml
7+
.php-cs-fixer.php
8+
phpstan.neon

.php-cs-fixer.dist.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
if (!file_exists(__DIR__.'/src')) {
4+
exit(0);
5+
}
6+
7+
$finder = (new PhpCsFixer\Finder())
8+
->in(__DIR__.'/src')
9+
->in(__DIR__.'/tests')
10+
;
11+
12+
return (new PhpCsFixer\Config())
13+
->setRules(array(
14+
'@Symfony' => true,
15+
'@Symfony:risky' => true,
16+
'protected_to_private' => false,
17+
'semicolon_after_instruction' => false,
18+
'header_comment' => [
19+
'header' => <<<EOF
20+
This file is part of the Micro framework package.
21+
22+
(c) Stanislau Komar <[email protected]>
23+
24+
For the full copyright and license information, please view the LICENSE
25+
file that was distributed with this source code.
26+
EOF
27+
]
28+
))
29+
->setRiskyAllowed(true)
30+
->setFinder($finder);

composer.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "micro/plugin-http-roadrunner",
3+
"description": "Micro Framework: HTTP Roadrunner adapter",
4+
"license": "MIT",
5+
"type": "micro-plugin",
6+
"authors": [
7+
{
8+
"name": "Stanislau Komar",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {
13+
"micro/kernel-app": "^1.6",
14+
"micro/kernel-boot-plugin-depended": "^1.6",
15+
"micro/plugin-event-emitter": "^1.6",
16+
"micro/plugin-http-core": "^1.6",
17+
"nyholm/psr7": "^1.8",
18+
"spiral/roadrunner": "^2.0",
19+
"symfony/psr-http-message-bridge": "^2.3"
20+
},
21+
"require-dev": {
22+
"ergebnis/composer-normalize": "^2.29",
23+
"friendsofphp/php-cs-fixer": "^3.13",
24+
"phpstan/phpstan": "^1.9",
25+
"phpunit/php-code-coverage": "^9.2",
26+
"phpunit/phpunit": "^9.5",
27+
"vimeo/psalm": "^5.2"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Micro\\Plugin\\Http\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"Micro\\Plugin\\Http\\Test\\Unit\\": "tests/Unit"
37+
}
38+
},
39+
"config": {
40+
"allow-plugins": {
41+
"ergebnis/composer-normalize": true
42+
},
43+
"sort-packages": true
44+
},
45+
"scripts": {
46+
"coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-text",
47+
"php-cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --using-cache=no",
48+
"php-cs-try": "PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --verbose --dry-run --using-cache=no",
49+
"phpstan": "./vendor/bin/phpstan analyze --no-progress",
50+
"phpunit": "./vendor/bin/phpunit",
51+
"psalm": "./vendor/bin/psalm --no-progress --show-info=true",
52+
"statics": [
53+
"@phpstan",
54+
"@psalm",
55+
"@php-cs-try"
56+
],
57+
"test": [
58+
"@statics",
59+
"composer validate --strict",
60+
"composer normalize",
61+
"@coverage"
62+
]
63+
}
64+
}

phpunit.xml.dist

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- https://phpunit.readthedocs.io/en/9.5/configuration.html#the-phpunit-element -->
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
5+
backupGlobals="false"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" force="true"/>
13+
</php>
14+
<testsuites>
15+
<testsuite name="Unit tests">
16+
<directory>tests/Unit</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist>
21+
<directory suffix=".php">src/</directory>
22+
<exclude>
23+
</exclude>
24+
</whitelist>
25+
</filter>
26+
<coverage>
27+
<include>
28+
<directory suffix=".php">src</directory>
29+
</include>
30+
<report>
31+
<html outputDirectory="test-coverage-report/" />
32+
</report>
33+
</coverage>
34+
</phpunit>

psalm.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="2"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
15+
</projectFiles>
16+
17+
<issueHandlers>
18+
19+
<UnnecessaryVarAnnotation>
20+
<errorLevel type="suppress">
21+
<file name="src/HttpRoadrunnerPlugin.php"/>
22+
</errorLevel>
23+
</UnnecessaryVarAnnotation>
24+
25+
<MissingConstructor>
26+
<errorLevel type="suppress">
27+
<file name="src/HttpRoadrunnerPlugin.php"/>
28+
</errorLevel>
29+
</MissingConstructor>
30+
31+
<ImplementedReturnTypeMismatch>
32+
<errorLevel type="suppress">
33+
<file name="src/HttpRoadrunnerPlugin.php"/>
34+
</errorLevel>
35+
</ImplementedReturnTypeMismatch>
36+
</issueHandlers>
37+
38+
</psalm>

src/HttpRoadrunnerPlugin.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http;
15+
16+
use Micro\Framework\Kernel\Plugin\PluginDependedInterface;
17+
use Micro\Plugin\EventEmitter\EventEmitterPlugin;
18+
19+
final readonly class HttpRoadrunnerPlugin implements PluginDependedInterface
20+
{
21+
public function getDependedPlugins(): iterable
22+
{
23+
return [
24+
EventEmitterPlugin::class,
25+
HttpCorePlugin::class,
26+
];
27+
}
28+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Listener;
15+
16+
use Micro\Component\EventEmitter\EventInterface;
17+
use Micro\Kernel\App\Business\Event\ApplicationReadyEvent;
18+
use Micro\Kernel\App\Business\Event\ApplicationReadyEventInterface;
19+
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
20+
use Nyholm\Psr7\Factory\Psr17Factory;
21+
use Spiral\RoadRunner;
22+
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
23+
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
24+
25+
final readonly class ApplicationRoadrunnerStartedListener
26+
{
27+
public function __construct(
28+
private HttpFacadeInterface $httpFacade
29+
) {
30+
}
31+
32+
/**
33+
* @param ApplicationReadyEvent $event
34+
*
35+
* @psalm-suppress MoreSpecificImplementedParamType
36+
*
37+
* @throws \JsonException
38+
*/
39+
public function on(EventInterface $event): void
40+
{
41+
$sysenv = $event->systemEnvironment();
42+
if ('cli' !== $sysenv || !getenv('RR_MODE')) {
43+
return;
44+
}
45+
46+
$httpFoundationFactory = new HttpFoundationFactory();
47+
$psr17Factory = new Psr17Factory();
48+
$httpMessageFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
49+
50+
$worker = RoadRunner\Worker::create();
51+
$worker = new RoadRunner\Http\PSR7Worker($worker, $psr17Factory, $psr17Factory, $psr17Factory);
52+
while ($request = $worker->waitRequest()) {
53+
try {
54+
$appRequest = $httpFoundationFactory->createRequest($request);
55+
$appResponse = $this->httpFacade->execute($appRequest);
56+
$worker->respond($httpMessageFactory->createResponse($appResponse));
57+
} catch (\Throwable $e) {
58+
$worker->getWorker()->error((string) $e);
59+
}
60+
}
61+
}
62+
63+
public static function supports(EventInterface $event): bool
64+
{
65+
return $event instanceof ApplicationReadyEventInterface;
66+
}
67+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Micro framework package.
5+
*
6+
* (c) Stanislau Komar <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Micro\Plugin\Http\Test\Unit\Listener;
13+
14+
use Micro\Component\EventEmitter\EventInterface;
15+
use Micro\Framework\Kernel\KernelInterface;
16+
use Micro\Kernel\App\AppKernelInterface;
17+
use Micro\Kernel\App\Business\Event\ApplicationReadyEvent;
18+
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
19+
use Micro\Plugin\Http\Listener\ApplicationRoadrunnerStartedListener;
20+
use PHPUnit\Framework\MockObject\MockObject;
21+
use PHPUnit\Framework\TestCase;
22+
23+
final class ApplicationRoadrunnerStartedListenerTest extends TestCase
24+
{
25+
private KernelInterface|MockObject $kernel;
26+
27+
private EventInterface $event;
28+
29+
protected function setUp(): void
30+
{
31+
$this->kernel = $this->createMock(AppKernelInterface::class);
32+
$this->event = new ApplicationReadyEvent(
33+
$this->kernel,
34+
'cli',
35+
);
36+
}
37+
38+
public function testOnWithoutCliAndRrMode(): void
39+
{
40+
$httpFacade = $this->createMock(HttpFacadeInterface::class);
41+
$listener = new ApplicationRoadrunnerStartedListener($httpFacade);
42+
putenv('RR_MODE');
43+
$event = new ApplicationReadyEvent(
44+
$this->kernel,
45+
'web',
46+
);
47+
$httpFacade->expects($this->never())
48+
->method('execute');
49+
$listener->on($event);
50+
}
51+
52+
public function testSupports(): void
53+
{
54+
$httpFacade = $this->createMock(HttpFacadeInterface::class);
55+
$listener = new ApplicationRoadrunnerStartedListener($httpFacade);
56+
$result = $listener->supports($this->event);
57+
$this->assertTrue($result);
58+
}
59+
}

0 commit comments

Comments
 (0)