Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster authored and roxblnfk committed Apr 13, 2023
1 parent 28e6fcd commit 529f962
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 108 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: roadrunner-server
4 changes: 2 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
on:
pull_request: null
push:
branches:
- master
- '*.*'
pull_request: null

name: phpunit

Expand All @@ -16,4 +16,4 @@ jobs:
php: >-
['8.1', '8.2']
stability: >-
['prefer-stable', 'prefer-lowest']
['prefer-lowest', 'prefer-stable']
2 changes: 1 addition & 1 deletion .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
on:
pull_request: null
push:
branches:
- master
- '*.*'
pull_request: null

name: static analysis

Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ clover*
cover*
.DS_Store
*.cache

.phpunit.cache/
.phpunit.result.cache
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"license": "MIT",
"authors": [
{
"name": "Anton Titov (Wolfy-J)",
"email": "wolfy.jd@gmail.com"
"name": "Anton Titov (wolfy-j)",
"email": "wolfy-j@spiralscout.com"
},
{
"name": "Valery Piashchynski",
Expand Down Expand Up @@ -45,9 +45,9 @@
"spiral/roadrunner-worker": "^3.0"
},
"require-dev": {
"jetbrains/phpstorm-attributes": "^1.0",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^10.0",
"jetbrains/phpstorm-attributes": "^1.0",
"symfony/process": "^6.2",
"vimeo/psalm": "^5.9"
},
Expand Down
15 changes: 4 additions & 11 deletions src/HttpWorker.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;
Expand Down Expand Up @@ -77,7 +70,7 @@ public function respond(int $status, string|Generator $body, array $headers = []
return;
}

$head = (string)\json_encode([
$head = \json_encode([
'status' => $status,
'headers' => $headers ?: (object)[],
], \JSON_THROW_ON_ERROR);
Expand All @@ -87,7 +80,7 @@ public function respond(int $status, string|Generator $body, array $headers = []

private function respondStream(int $status, Generator $body, array $headers = []): void
{
$head = (string)\json_encode([
$head = \json_encode([
'status' => $status,
'headers' => $headers ?: (object)[],
], \JSON_THROW_ON_ERROR);
Expand Down Expand Up @@ -124,11 +117,11 @@ private function createRequest(string $body, array $context): Request
cookies: (array)($context['cookies'] ?? []),
uploads: (array)($context['uploads'] ?? []),
attributes: [
Request::PARSED_BODY_ATTRIBUTE_NAME => (bool)$context['parsed'],
Request::PARSED_BODY_ATTRIBUTE_NAME => $context['parsed'],
] + (array)($context['attributes'] ?? []),
query: $query,
body: $body,
parsed: (bool)$context['parsed'],
parsed: $context['parsed'],
);
}

Expand Down
9 changes: 0 additions & 9 deletions src/HttpWorkerInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;
Expand All @@ -22,8 +15,6 @@ interface HttpWorkerInterface extends WorkerAwareInterface
{
/**
* Wait for incoming http request.
*
* @return Request|null
*/
public function waitRequest(): ?Request;

Expand Down
67 changes: 6 additions & 61 deletions src/PSR7Worker.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;
Expand Down Expand Up @@ -37,65 +30,31 @@ class PSR7Worker implements PSR7WorkerInterface
*/
public int $chunkSize = 0;

/**
* @var HttpWorker
*/
private HttpWorker $httpWorker;

/**
* @var ServerRequestFactoryInterface
*/
private ServerRequestFactoryInterface $requestFactory;

/**
* @var StreamFactoryInterface
*/
private StreamFactoryInterface $streamFactory;

/**
* @var UploadedFileFactoryInterface
*/
private UploadedFileFactoryInterface $uploadsFactory;
private readonly HttpWorker $httpWorker;

/**
* @var array
*/
private array $originalServer;
private readonly array $originalServer;

/**
* @var string[] Valid values for HTTP protocol version
*/
private static array $allowedVersions = ['1.0', '1.1', '2'];

/**
* @param WorkerInterface $worker
* @param ServerRequestFactoryInterface $requestFactory
* @param StreamFactoryInterface $streamFactory
* @param UploadedFileFactoryInterface $uploadsFactory
*/
public function __construct(
WorkerInterface $worker,
ServerRequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
UploadedFileFactoryInterface $uploadsFactory
private readonly ServerRequestFactoryInterface $requestFactory,
private readonly StreamFactoryInterface $streamFactory,
private readonly UploadedFileFactoryInterface $uploadsFactory,
) {
$this->httpWorker = new HttpWorker($worker);
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
$this->uploadsFactory = $uploadsFactory;
$this->originalServer = $_SERVER;
}

/**
* @return WorkerInterface
*/
public function getWorker(): WorkerInterface
{
return $this->httpWorker->getWorker();
}

/**
* @return ServerRequestInterface|null
* @throws \JsonException
*/
public function waitRequest(): ?ServerRequestInterface
Expand All @@ -113,7 +72,6 @@ public function waitRequest(): ?ServerRequestInterface
/**
* Send response to the application server.
*
* @param ResponseInterface $response
* @throws \JsonException
*/
public function respond(ResponseInterface $response): void
Expand Down Expand Up @@ -158,7 +116,6 @@ private function streamToGenerator(StreamInterface $stream): Generator
* Returns altered copy of _SERVER variable. Sets ip-address,
* request-time and other values.
*
* @param Request $request
* @return non-empty-array<array-key|string, mixed|string>
*/
protected function configureServer(Request $request): array
Expand All @@ -173,7 +130,7 @@ protected function configureServer(Request $request): array

$server['HTTP_USER_AGENT'] = '';
foreach ($request->headers as $key => $value) {
$key = \strtoupper(\str_replace('-', '_', (string)$key));
$key = \strtoupper(\str_replace('-', '_', $key));
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
$server[$key] = \implode(', ', $value);
} else {
Expand All @@ -184,26 +141,17 @@ protected function configureServer(Request $request): array
return $server;
}

/**
* @return int
*/
protected function timeInt(): int
{
return \time();
}

/**
* @return float
*/
protected function timeFloat(): float
{
return \microtime(true);
}

/**
* @param Request $httpRequest
* @param array $server
* @return ServerRequestInterface
* @throws \JsonException
*/
protected function mapRequest(Request $httpRequest, array $server): ServerRequestInterface
Expand Down Expand Up @@ -278,9 +226,6 @@ protected function wrapUploads(array $files): array

/**
* Normalize HTTP protocol version to valid values
*
* @param string $version
* @return string
*/
private static function fetchProtocolVersion(string $version): string
{
Expand Down
7 changes: 0 additions & 7 deletions src/PSR7WorkerInterface.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;
Expand Down
11 changes: 0 additions & 11 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* This file is part of RoadRunner package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Spiral\RoadRunner\Http;
Expand Down Expand Up @@ -59,16 +52,12 @@ public function __construct(
) {
}

/**
* @return string
*/
public function getRemoteAddr(): string
{
return (string)($this->attributes['ipAddress'] ?? $this->remoteAddr);
}

/**
* @return array|null
* @throws \JsonException
*/
public function getParsedBody(): ?array
Expand Down
4 changes: 1 addition & 3 deletions tests/Feature/StreamResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ public function testStopStreamAfterStreamEnd(): void

$this->assertFalse($this->getWorker()->hasPayload(\Spiral\RoadRunner\Message\Command\StreamStop::class));
$this->sendCommand(new StreamStop());


\usleep(200_000);
self::assertSame(\implode("\n", ['Hello', 'World!']), \trim(ServerRunner::getBuffer()));
$this->assertTrue($this->getWorker()->hasPayload(\Spiral\RoadRunner\Message\Command\StreamStop::class));
Expand All @@ -128,7 +126,7 @@ private function getRelay(): SocketRelay

private function getWorker(): Worker
{
return $this->worker ??= new Worker($this->getRelay(), false);
return $this->worker ??= new Worker(relay: $this->getRelay(), interceptSideEffects: false);
}

private function makeHttpWorker(): HttpWorker
Expand Down

0 comments on commit 529f962

Please sign in to comment.