Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.{yml, yaml, sh, conf, neon*}]
[*.yaml]
indent_size = 2

[*.yml]
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
15 changes: 7 additions & 8 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
* text=auto

/.github export-ignore
/tests export-ignore
/.* export-ignore
/phpunit.xml* export-ignore
/phpstan.* export-ignore
/psalm.* export-ignore
/infection.* export-ignore
/codecov.* export-ignore
/.* export-ignore
/tests export-ignore
/phpunit.xml* export-ignore
/psalm.* export-ignore
/psalm-baseline.xml export-ignore
/infection.* export-ignore
/rector.php export-ignore
12 changes: 12 additions & 0 deletions .github/workflows/cs-fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
on:
push:
branches:
- '*'

name: Fix Code Style

jobs:
cs-fix:
permissions:
contents: write
uses: spiral/gh-actions/.github/workflows/cs-fix.yml@master
29 changes: 9 additions & 20 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
# Composer lock file
composer.lock

# IDEs
/.idea
/.vscode

# Vendors
/vendor
**/vendor

# Temp dirs & trash
/tests/server*
clover*
cover*
.DS_Store
*.cache

.phpunit.cache/
.phpunit.result.cache
/.*
!/.github/
!/.php-cs-fixer.dist.php
!/.editorconfig
!/.gitattributes
/runtime/
/vendor/
/composer.lock
*.log
12 changes: 12 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

require_once 'vendor/autoload.php';

return \Spiral\CodeStyle\Builder::create()
->include(__DIR__ . '/src')
->include(__DIR__ . '/tests')
->include(__FILE__)
->allowRisky(false)
->build();
21 changes: 15 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^10.0",
"phpunit/phpunit": "^10.5",
"spiral/code-style": "^2.3",
"spiral/dumper": "^3.3",
"symfony/process": "^6.2 || ^7.0",
"vimeo/psalm": "^5.9"
"vimeo/psalm": "^6.13"
},
"autoload": {
"psr-4": {
Expand All @@ -70,9 +71,6 @@
"url": "https://github.com/sponsors/roadrunner-server"
}
],
"scripts": {
"analyze": "psalm"
},
"suggest": {
"spiral/roadrunner-cli": "Provides RoadRunner installation and management CLI tools",
"ext-protobuf": "Provides Protocol Buffers support. Without it, performance will be lower."
Expand All @@ -81,5 +79,16 @@
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"scripts": {
"cs:fix": "php-cs-fixer fix -v",
"psalm": "psalm",
"psalm:baseline": "psalm --set-baseline=psalm-baseline.xml",
"psalm:ci": "psalm --output-format=github --shepherd --show-info=false --stats --threads=4",
"test": "phpunit --color=always --testdox",
"test:cc": [
"@putenv XDEBUG_MODE=coverage",
"phpunit --coverage-clover=runtime/phpunit/logs/clover.xml --color=always"
]
}
}
18 changes: 16 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile="runtime/phpunit/result.cache"
backupGlobals="false" colors="true"
processIsolation="false"
executionOrder="random"
stopOnFailure="false"
stopOnError="false"
stderr="true"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="RR Worker Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="runtime/coverage"/>
<text outputFile="runtime/coverage.txt"/>
<clover outputFile="runtime/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="runtime/report.junit.xml"/>
</logging>
<source>
<include>
<directory>src</directory>
</include>
</coverage>
<exclude>
<directory>tests</directory>
</exclude>
</source>
</phpunit>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
resolveFromConfigFile="true"
findUnusedBaselineEntry="false"
findUnusedCode="false"
ensureOverrideAttribute="false"
>
<issueHandlers>
<RedundantConditionGivenDocblockType errorLevel="suppress" />
Expand Down
16 changes: 5 additions & 11 deletions src/GlobalState.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

namespace Spiral\RoadRunner\Http;

use function time;
use function microtime;
use function strtoupper;
use function str_replace;
use function implode;

final class GlobalState
{
/**
Expand All @@ -35,22 +29,22 @@ public static function enrichServerVars(Request $request): array
$server = self::$cachedServer;

$server['REQUEST_URI'] = $request->uri;
$server['REQUEST_TIME'] = time();
$server['REQUEST_TIME_FLOAT'] = microtime(true);
$server['REQUEST_TIME'] = \time();
$server['REQUEST_TIME_FLOAT'] = \microtime(true);
$server['REMOTE_ADDR'] = $request->getRemoteAddr();
$server['REQUEST_METHOD'] = $request->method;
$server['HTTP_USER_AGENT'] = '';

foreach ($request->headers as $key => $value) {
$key = strtoupper(str_replace('-', '_', $key));
$key = \strtoupper(\str_replace('-', '_', $key));

if ($key == 'CONTENT_TYPE' || $key == 'CONTENT_LENGTH') {
$server[$key] = implode(', ', $value);
$server[$key] = \implode(', ', $value);

continue;
}

$server['HTTP_' . $key] = implode(', ', $value);
$server['HTTP_' . $key] = \implode(', ', $value);
}

return $server;
Expand Down
29 changes: 15 additions & 14 deletions src/HttpWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@
* }
*
* @see Request
*
* @api
*/
class HttpWorker implements HttpWorkerInterface
{
private static ?int $codec = null;

public function __construct(
private readonly WorkerInterface $worker,
) {
}
) {}

public function getWorker(): WorkerInterface
{
Expand Down Expand Up @@ -83,13 +84,13 @@ public function waitRequest(): ?Request
* @param array<array-key, array<array-key, string>> $headers
* @throws \JsonException
*/
public function respond(int $status, string|Generator $body = '', array $headers = [], bool $endOfStream = true): void
public function respond(int $status, string|\Generator $body = '', array $headers = [], bool $endOfStream = true): void
{
if ($status < 200 && $status >= 100 && $body !== '') {
throw new \InvalidArgumentException('Unable to send a body with informational status code.');
}

if ($body instanceof Generator) {
if ($body instanceof \Generator) {
$this->respondStream($status, $body, $headers, $endOfStream);
return;
}
Expand All @@ -101,7 +102,7 @@ public function respond(int $status, string|Generator $body = '', array $headers
/**
* @param array<array-key, array<array-key, string>> $headers
*/
private function respondStream(int $status, Generator $body, array $headers = [], bool $endOfStream = true): void
private function respondStream(int $status, \Generator $body, array $headers = [], bool $endOfStream = true): void
{
$worker = $this->worker instanceof StreamWorkerInterface
? $this->worker->withStreamMode()
Expand All @@ -110,20 +111,20 @@ private function respondStream(int $status, Generator $body, array $headers = []
do {
if (!$body->valid()) {
// End of generator
$content = (string)$body->getReturn();
$content = (string) $body->getReturn();
if ($endOfStream === false && $content === '') {
// We don't need to send an empty frame if the stream is not ended
return;
}
/** @psalm-suppress TooManyArguments */
$worker->respond(
$this->createRespondPayload($status, $content, $headers, $endOfStream),
static::$codec
static::$codec,
);
break;
}

$content = (string)$body->current();
$content = (string) $body->current();
if ($worker->getPayload(StreamStop::class) !== null) {
$body->throw(new StreamStoppedException());

Expand Down Expand Up @@ -160,12 +161,12 @@ private function arrayToRequest(string $body, array $context): Request
protocol: $context['protocol'],
method: $context['method'],
uri: $context['uri'],
headers: $this->filterHeaders((array)($context['headers'] ?? [])),
cookies: (array)($context['cookies'] ?? []),
uploads: (array)($context['uploads'] ?? []),
headers: $this->filterHeaders((array) ($context['headers'] ?? [])),
cookies: (array) ($context['cookies'] ?? []),
uploads: (array) ($context['uploads'] ?? []),
attributes: [
Request::PARSED_BODY_ATTRIBUTE_NAME => $context['parsed'],
] + (array)($context['attributes'] ?? []),
] + (array) ($context['attributes'] ?? []),
query: $query,
body: $body,
parsed: $context['parsed'],
Expand Down Expand Up @@ -253,7 +254,7 @@ private function arrayToHeaderValue(array $headers = []): array
*/
foreach ($headers as $key => $value) {
/** @psalm-suppress DocblockTypeContradiction */
$value = \array_filter(\is_array($value) ? $value : [$value], static fn (mixed $v): bool => \is_string($v));
$value = \array_filter(\is_array($value) ? $value : [$value], static fn(mixed $v): bool => \is_string($v));
if ($value !== []) {
$result[$key] = new HeaderValue(['value' => $value]);
}
Expand All @@ -270,7 +271,7 @@ private function createRespondPayload(int $status, string $body, array $headers
$head = static::$codec === Frame::CODEC_PROTO
? (new Response(['status' => $status, 'headers' => $this->arrayToHeaderValue($headers)]))
->serializeToString()
: \json_encode(['status' => $status, 'headers' => $headers ?: (object)[]], \JSON_THROW_ON_ERROR);
: \json_encode(['status' => $status, 'headers' => $headers ?: (object) []], \JSON_THROW_ON_ERROR);

return new Payload(body: $body, header: $head, eos: $eos);
}
Expand Down
5 changes: 2 additions & 3 deletions src/HttpWorkerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Generator;
use Spiral\RoadRunner\WorkerAwareInterface;
use Stringable;

/**
* @psalm-import-type HeadersList from Request
Expand All @@ -22,13 +21,13 @@ public function waitRequest(): ?Request;
* Send response to the application server.
*
* @param int $status Http status code
* @param Generator<mixed, scalar|Stringable, mixed, Stringable|scalar|null>|string $body Body of response.
* @param \Generator<mixed, scalar|\Stringable, mixed, \Stringable|scalar|null>|string $body Body of response.
* If the body is a generator, then each yielded value will be sent as a separated stream chunk.
* Returned value will be sent as a last stream package.
* Note: Stream response is supported by RoadRunner since version 2023.3
* @param HeadersList|array<array-key, array<array-key, string>> $headers $headers An associative array of the
* message's headers. Each key MUST be a header name, and each value MUST be an array of strings for
* that header.
*/
public function respond(int $status, string|Generator $body, array $headers = []): void;
public function respond(int $status, string|\Generator $body, array $headers = []): void;
}
Loading
Loading