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
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
scanDirectories:
- vendor/swoole/ide-helper
level: 5
level: 7
paths:
- src
- tests
6 changes: 3 additions & 3 deletions src/Http/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

abstract class Adapter
{
abstract public function onStart(callable $callback);
abstract public function onRequest(callable $callback);
abstract public function start();
abstract public function onStart(callable $callback): void;
abstract public function onRequest(callable $callback): void;
abstract public function start(): void;
abstract public function getContainer(): Container;
}
16 changes: 11 additions & 5 deletions src/Http/Adapter/FPM/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function setURI(string $uri): static
* Method for querying upload files data. If $key is not found empty array will be returned.
*
* @param string $key
* @return array
* @return array<string, mixed>
*/
public function getFiles(string $key): array
{
Expand Down Expand Up @@ -275,7 +275,13 @@ public function getHeader(string $key, string $default = ''): string
{
$headers = $this->generateHeaders();

return (isset($headers[$key])) ? $headers[$key] : $default;
if (!isset($headers[$key])) {
return $default;
}

$value = $headers[$key];

return \is_array($value) ? \implode(', ', $value) : $value;
}

/**
Expand Down Expand Up @@ -316,7 +322,7 @@ public function removeHeader(string $key): static
*
* Generate PHP input stream and parse it as an array in order to handle different content type of requests
*
* @return array
* @return array<string, mixed>
*/
protected function generateInput(): array
{
Expand All @@ -331,7 +337,7 @@ protected function generateInput(): array
$length = (empty($length)) ? \strlen($contentType) : $length;
$contentType = \substr($contentType, 0, $length);

$this->rawPayload = \file_get_contents('php://input');
$this->rawPayload = \file_get_contents('php://input') ?: '';

switch ($contentType) {
case 'application/json':
Expand Down Expand Up @@ -361,7 +367,7 @@ protected function generateInput(): array
*
* Parse request headers as an array for easy querying using the getHeader method
*
* @return array
* @return array<string, string|array<int, string>>
*/
protected function generateHeaders(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/FPM/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function sendHeader(string $key, mixed $value): void
*
* @param string $name
* @param string $value
* @param array $options
* @param array<string, mixed> $options
* @return void
*/
protected function sendCookie(string $name, string $value, array $options): void
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Adapter/FPM/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Server extends Adapter
{
public function __construct(private Container $container) {}

public function onRequest(callable $callback)
public function onRequest(callable $callback): void
{
$request = new Request();
$response = new Response();
Expand All @@ -20,7 +20,7 @@ public function onRequest(callable $callback)
\call_user_func($callback, $request, $response);
}

public function onStart(callable $callback)
public function onStart(callable $callback): void
{
\call_user_func($callback, $this);
}
Expand All @@ -30,7 +30,7 @@ public function getContainer(): Container
return $this->container;
}

public function start()
public function start(): void
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/Swoole/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(SwooleRequest $request)
*/
public function getRawPayload(): string
{
return $this->swoole->rawContent();
return $this->swoole->rawContent() ?: '';
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Http/Adapter/Swoole/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ class Server extends Adapter
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
protected Container $container;

/**
* @param array<string, mixed> $settings
*/
public function __construct(string $host, ?string $port = null, array $settings = [], int $mode = SWOOLE_PROCESS, ?Container $container = null)
{
$this->server = new SwooleServer($host, (int) $port, $mode);
$this->server->set($settings);
$this->container = $container ?? new Container();
}

public function onRequest(callable $callback)
public function onRequest(callable $callback): void
{
$this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
$requestContainer = new Container($this->container);
Expand All @@ -49,7 +52,7 @@ public function getServer(): SwooleServer
return $this->server;
}

public function onStart(callable $callback)
public function onStart(callable $callback): void
{
$this->server->on('start', function () use ($callback) {
go(function () use ($callback) {
Expand All @@ -58,8 +61,8 @@ public function onStart(callable $callback)
});
}

public function start()
public function start(): void
{
return $this->server->start();
$this->server->start();
}
}
9 changes: 6 additions & 3 deletions src/Http/Adapter/SwooleCoroutine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Server extends Adapter
/** @var callable|null */
protected $onStartCallback = null;

/**
* @param array<string, mixed> $settings
*/
public function __construct(
string $host,
?string $port = null,
Expand All @@ -30,7 +33,7 @@ public function __construct(
$this->container = $container ?? new Container();
}

public function onRequest(callable $callback)
public function onRequest(callable $callback): void
{
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
$requestContainer = new Container($this->container);
Expand All @@ -57,12 +60,12 @@ public function getServer(): SwooleServer
return $this->server;
}

public function onStart(callable $callback)
public function onStart(callable $callback): void
{
$this->onStartCallback = $callback;
}

public function start()
public function start(): void
{
if ($this->onStartCallback) {
\call_user_func($this->onStartCallback, $this);
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function load(string $directory, ?string $root = null): void

$handle = opendir(strval($directory));

if ($handle === false) {
throw new Exception("Failed to open directory: {$directory}");
}

while ($path = readdir($handle)) {
$extension = pathinfo($path, PATHINFO_EXTENSION);

Expand Down
16 changes: 8 additions & 8 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public function getResources(array $list): array
/**
* Set a resource on the given scope.
*
* @param string[] $injections
* @param list<string> $injections
*/
public function setResource(string $name, callable $callback, array $injections = []): void
{
Expand All @@ -474,7 +474,7 @@ public function setResource(string $name, callable $callback, array $injections
/**
* Set a request-scoped resource on the current request's container.
*
* @param string[] $injections
* @param list<string> $injections
*/
protected function setRequestResource(string $name, callable $callback, array $injections = []): void
{
Expand Down Expand Up @@ -516,7 +516,7 @@ public static function isStage(): bool
*
* Get all application routes
*
* @return array
* @return array<string, Route[]>
*/
public static function getRoutes(): array
{
Expand Down Expand Up @@ -628,7 +628,7 @@ public static function onRequest(): Hook
return $hook;
}

public function start()
public function start(): void
{

$this->server->onRequest(
Expand Down Expand Up @@ -779,9 +779,9 @@ public function execute(Route $route, Request $request, Response $response): sta
* Get Arguments
*
* @param Hook $hook
* @param array $values
* @param array $requestParams
* @return array
* @param array<string, mixed> $values
* @param array<string, mixed> $requestParams
* @return array<int, mixed>
*
* @throws Exception
*/
Expand Down Expand Up @@ -1002,7 +1002,7 @@ private function runInternal(Request $request, Response $response): static
* Creates an validator instance and validate given value with given rules.
*
* @param string $key
* @param array $param
* @param array<string, mixed> $param
* @param mixed $value
* @return void
*
Expand Down
29 changes: 16 additions & 13 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,27 @@ abstract class Request
/**
* Container for php://input parsed stream as an associative array
*
* @var array|null
* @var array<string, mixed>|null
*/
protected $payload = null;

/**
* Container for parsed query string params
*
* @var array|null
* @var array<string, mixed>|null
*/
protected $queryString = null;

/**
* Container for parsed headers
*
* @var array|null
* @var array<string, string|array<int, string>>|null
*/
protected $headers = null;

/**
* @var array<int, string>
*/
protected array $trustedIpHeaders = [];

/**
Expand All @@ -69,7 +72,7 @@ public function getParam(string $key, mixed $default = null): mixed
*
* Get all params of current method
*
* @return array
* @return array<string, mixed>
*/
public function getParams(): array
{
Expand Down Expand Up @@ -145,7 +148,7 @@ abstract public function setServer(string $key, string $value): static;
* Set which headers to trust for determining client IP address.
* Headers are checked in order; the first one found with a valid IP is used.
*
* @param array $headers
* @param array<int, string> $headers
* @return static
*/
public function setTrustedIpHeaders(array $headers): static
Expand Down Expand Up @@ -244,7 +247,7 @@ abstract public function setURI(string $uri): static;
* Method for querying upload files data. If $key is not found empty array will be returned.
*
* @param string $key
* @return array
* @return array<string, mixed>
*/
abstract public function getFiles(string $key): array;

Expand Down Expand Up @@ -361,7 +364,7 @@ public function getSize(): int
$headerStrings[] = $key . ': ' . $value;
}
}
return \mb_strlen(\implode("\n", $headerStrings), '8bit') + \mb_strlen(\file_get_contents('php://input'), '8bit');
return \mb_strlen(\implode("\n", $headerStrings), '8bit') + \mb_strlen(\file_get_contents('php://input') ?: '', '8bit');
}

/**
Expand Down Expand Up @@ -486,7 +489,7 @@ public function getRangeUnit(): ?string
/**
* Set query string parameters
*
* @param array $params
* @param array<string, mixed> $params
* @return static
*/
public function setQueryString(array $params): static
Expand All @@ -499,7 +502,7 @@ public function setQueryString(array $params): static
/**
* Set payload parameters
*
* @param array $params
* @param array<string, mixed> $params
* @return static
*/
public function setPayload(array $params): static
Expand All @@ -514,7 +517,7 @@ public function setPayload(array $params): static
*
* Parse request headers as an array for easy querying using the getHeader method
*
* @return array
* @return array<string, string|array<int, string>>
*/
protected function generateHeaders(): array
{
Expand Down Expand Up @@ -548,7 +551,7 @@ protected function generateHeaders(): array
*
* Generate PHP input stream and parse it as an array in order to handle different content type of requests
*
* @return array
* @return array<string, mixed>
*/
abstract protected function generateInput(): array;

Expand All @@ -557,7 +560,7 @@ abstract protected function generateInput(): array;
*
* Parse content-range request header for easy access
*
* @return array|null
* @return array{unit: string, size: int, start: int, end: int}|null
*/
protected function parseContentRange(): ?array
{
Expand Down Expand Up @@ -611,7 +614,7 @@ protected function parseContentRange(): ?array
*
* Parse range request header for easy access
*
* @return array|null
* @return array<string, mixed>|null
*/
protected function parseRange(): ?array
{
Expand Down
Loading
Loading