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 example/src/server.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';

use Utopia\Http\Http;
use Utopia\Http\Response;
Expand Down
4 changes: 2 additions & 2 deletions pint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"preset": "psr12"
}
"preset": "per"
}
6 changes: 3 additions & 3 deletions src/Http/Adapter/FPM/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getProtocol(): string
*/
public function getPort(): string
{
return (string) \parse_url($this->getProtocol().'://'.$this->getServer('HTTP_HOST', ''), PHP_URL_PORT);
return (string) \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_PORT);
}

/**
Expand All @@ -125,7 +125,7 @@ public function getPort(): string
*/
public function getHostname(): string
{
$hostname = \parse_url($this->getProtocol().'://'.$this->getServer('HTTP_HOST', ''), PHP_URL_HOST);
$hostname = \parse_url($this->getProtocol() . '://' . $this->getServer('HTTP_HOST', ''), PHP_URL_HOST);
return strtolower((string) ($hostname));
}

Expand Down Expand Up @@ -352,7 +352,7 @@ protected function generateInput(): array
self::METHOD_PUT,
self::METHOD_PATCH,
self::METHOD_DELETE => $this->payload,
default => $this->queryString
default => $this->queryString,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Adapter/FPM/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public function sendHeader(string $key, mixed $value): void
{
if (\is_array($value)) {
foreach ($value as $v) {
\header($key.': '.$v, false);
\header($key . ': ' . $v, false);
}
} else {
\header($key.': '.$value);
\header($key . ': ' . $value);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/Http/Adapter/FPM/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

class Server extends Adapter
{
public function __construct(private Container $container)
{
}
public function __construct(private Container $container) {}

public function onRequest(callable $callback)
{
$request = new Request();
$response = new Response();

$this->container->set('fpmRequest', fn () => $request);
$this->container->set('fpmResponse', fn () => $response);
$this->container->set('fpmRequest', fn() => $request);
$this->container->set('fpmResponse', fn() => $response);

\call_user_func($callback, $request, $response);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Adapter/Swoole/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function getProtocol(): string

return match ($protocol) {
'http', 'https', 'ws', 'wss' => $protocol,
default => 'https'
default => 'https',
};
}

Expand All @@ -127,7 +127,7 @@ public function getProtocol(): string
*/
public function getPort(): string
{
return $this->getHeader('x-forwarded-port', (string) \parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT));
return $this->getHeader('x-forwarded-port', (string) \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_PORT));
}

/**
Expand All @@ -139,7 +139,7 @@ public function getPort(): string
*/
public function getHostname(): string
{
$hostname = \parse_url($this->getProtocol().'://'.$this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
$hostname = \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
return strtolower(strval($hostname));
}

Expand Down Expand Up @@ -364,7 +364,7 @@ protected function generateInput(): array
self::METHOD_PUT,
self::METHOD_PATCH,
self::METHOD_DELETE => $this->payload,
default => $this->queryString
default => $this->queryString,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Adapter/Swoole/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function onRequest(callable $callback)
{
$this->server->on('request', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
$requestContainer = new Container($this->container);
$requestContainer->set('swooleRequest', fn () => $request);
$requestContainer->set('swooleResponse', fn () => $response);
$requestContainer->set('swooleRequest', fn() => $request);
$requestContainer->set('swooleResponse', fn() => $response);

Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;

Expand Down
4 changes: 1 addition & 3 deletions src/Http/Adapter/SwooleCoroutine/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Utopia\Http\Adapter\Swoole\Request as SwooleAdapterRequest;

class Request extends SwooleAdapterRequest
{
}
class Request extends SwooleAdapterRequest {}
4 changes: 1 addition & 3 deletions src/Http/Adapter/SwooleCoroutine/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Utopia\Http\Adapter\Swoole\Response as SwooleAdapterResponse;

class Response extends SwooleAdapterResponse
{
}
class Response extends SwooleAdapterResponse {}
6 changes: 3 additions & 3 deletions src/Http/Adapter/SwooleCoroutine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
string $host,
?string $port = null,
array $settings = [],
?Container $container = null
?Container $container = null,
) {
$this->server = new SwooleServer($host, $port, false, true);
$this->server->set($settings);
Expand All @@ -34,8 +34,8 @@ public function onRequest(callable $callback)
{
$this->server->handle('/', function (SwooleRequest $request, SwooleResponse $response) use ($callback) {
$requestContainer = new Container($this->container);
$requestContainer->set('swooleRequest', fn () => $request);
$requestContainer->set('swooleResponse', fn () => $response);
$requestContainer->set('swooleRequest', fn() => $request);
$requestContainer->set('swooleResponse', fn() => $response);

Coroutine::getContext()[self::REQUEST_CONTAINER_CONTEXT_KEY] = $requestContainer;

Expand Down
4 changes: 1 addition & 3 deletions src/Http/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace Utopia\Http;

class Exception extends \Exception
{
}
class Exception extends \Exception {}
6 changes: 3 additions & 3 deletions src/Http/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function load(string $directory, ?string $root = null): void
continue;
}

$dirPath = $directory.'/'.$path;
$dirPath = $directory . '/' . $path;

if (is_dir($dirPath)) {
$this->load($dirPath, strval($root));
Expand Down Expand Up @@ -163,7 +163,7 @@ public function isFileLoaded(string $uri): bool
public function getFileContents(string $uri): mixed
{
if (!array_key_exists($uri, $this->loaded)) {
throw new Exception('File not found or not loaded: '.$uri);
throw new Exception('File not found or not loaded: ' . $uri);
}

return $this->loaded[$uri]['contents'];
Expand All @@ -180,7 +180,7 @@ public function getFileContents(string $uri): mixed
public function getFileMimeType(string $uri): mixed
{
if (!array_key_exists($uri, $this->loaded)) {
throw new Exception('File not found or not loaded: '.$uri);
throw new Exception('File not found or not loaded: ' . $uri);
}

return $this->loaded[$uri]['mimeType'];
Expand Down
22 changes: 11 additions & 11 deletions src/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function setTelemetry(Telemetry $telemetry): void
'http.server.request.duration',
's',
null,
['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]]
['ExplicitBucketBoundaries' => [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10]],
);

// https://opentelemetry.io/docs/specs/semconv/http/http-metrics/#metric-httpserveractive_requests
Expand Down Expand Up @@ -430,7 +430,7 @@ public function getResource(string $name): mixed
{
try {
return $this->server->getContainer()->get($name);
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
// Normalize DI container errors to the Http layer's "resource" terminology.
$message = \str_replace('dependency', 'resource', $e->getMessage());

Expand Down Expand Up @@ -632,7 +632,7 @@ public function start()
{

$this->server->onRequest(
fn (Request $request, Response $response) => $this->run($request, $response)
fn(Request $request, Response $response) => $this->run($request, $response),
);

$this->server->onStart(function ($server) {
Expand All @@ -646,7 +646,7 @@ public function start()
\call_user_func_array($hook->getAction(), $arguments);
}
} catch (\Exception $e) {
$this->setResource('error', fn () => $e);
$this->setResource('error', fn() => $e);

foreach (self::$errors as $error) { // Global error hooks
if (in_array('*', $error->getGroups())) {
Expand Down Expand Up @@ -745,7 +745,7 @@ public function execute(Route $route, Request $request, Response $response): sta
}
}
} catch (\Throwable $e) {
$this->setRequestResource('error', fn () => $e, []);
$this->setRequestResource('error', fn() => $e, []);

foreach ($groups as $group) {
foreach (self::$errors as $error) { // Group error hooks
Expand Down Expand Up @@ -868,16 +868,16 @@ private function runInternal(Request $request, Response $response): static
$response->setCompressionSupported($this->compressionSupported);
}

$this->setRequestResource('request', fn () => $request);
$this->setRequestResource('response', fn () => $response);
$this->setRequestResource('request', fn() => $request);
$this->setRequestResource('response', fn() => $response);

try {
foreach (self::$requestHooks as $hook) {
$arguments = $this->getArguments($hook, [], []);
\call_user_func_array($hook->getAction(), $arguments);
}
} catch (\Exception $e) {
$this->setRequestResource('error', fn () => $e, []);
$this->setRequestResource('error', fn() => $e, []);

foreach (self::$errors as $error) { // Global error hooks
if (\in_array('*', $error->getGroups())) {
Expand Down Expand Up @@ -907,7 +907,7 @@ private function runInternal(Request $request, Response $response): static
$route = $this->match($request);
$groups = ($route instanceof Route) ? $route->getGroups() : [];

$this->setRequestResource('route', fn () => $route, []);
$this->setRequestResource('route', fn() => $route, []);

if (self::REQUEST_METHOD_HEAD == $method) {
$method = self::REQUEST_METHOD_GET;
Expand Down Expand Up @@ -953,7 +953,7 @@ private function runInternal(Request $request, Response $response): static
$path = \is_string($path) ? ($path === '' ? '/' : $path) : '/';
$route->path($path);

$this->setRequestResource('route', fn () => $route, []);
$this->setRequestResource('route', fn() => $route, []);
}

if (null !== $route) {
Expand Down Expand Up @@ -986,7 +986,7 @@ private function runInternal(Request $request, Response $response): static
} else {
foreach (self::$errors as $error) { // Global error hooks
if (\in_array('*', $error->getGroups())) {
$this->setRequestResource('error', fn () => new Exception('Not Found', 404), []);
$this->setRequestResource('error', fn() => new Exception('Not Found', 404), []);
\call_user_func_array($error->getAction(), $this->getArguments($error, [], $request->getParams()));
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public function setCompressionSupported(mixed $compressionSupported): static
*/
public function setContentType(string $type, string $charset = ''): static
{
$this->contentType = $type.((!empty($charset) ? '; charset='.$charset : ''));
$this->contentType = $type . ((!empty($charset) ? '; charset=' . $charset : ''));

return $this;
}
Expand Down Expand Up @@ -619,10 +619,10 @@ public function send(string $body = ''): void

// Compress body only if all conditions are met:
if (
!$hasContentEncoding &&
!empty($this->acceptEncoding) &&
$this->isCompressible($this->contentType) &&
strlen($body) > $this->compressionMinSize
!$hasContentEncoding
&& !empty($this->acceptEncoding)
&& $this->isCompressible($this->contentType)
&& strlen($body) > $this->compressionMinSize
) {
$algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, $this->compressionSupported);

Expand Down Expand Up @@ -924,7 +924,7 @@ public function jsonp(string $callback, array $data): void
{
$this
->setContentType(self::CONTENT_TYPE_JAVASCRIPT, self::CHARSET_UTF8)
->send('parent.'.$callback.'('.\json_encode($data).');');
->send('parent.' . $callback . '(' . \json_encode($data) . ');');
}

/**
Expand All @@ -941,7 +941,7 @@ public function iframe(string $callback, array $data): void
{
$this
->setContentType(self::CONTENT_TYPE_HTML, self::CHARSET_UTF8)
->send('<script type="text/javascript">window.parent.'.$callback.'('.\json_encode($data).');</script>');
->send('<script type="text/javascript">window.parent.' . $callback . '(' . \json_encode($data) . ');</script>');
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Http/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ public static function addRouteAlias(string $path, Route $route): void
* @param string $path
* @return \Utopia\Http\Route|null
*/
public static function match(string $method, string $path): Route|null
public static function match(string $method, string $path): ?Route
{
if (!array_key_exists($method, self::$routes)) {
return null;
}

$parts = array_values(array_filter(explode('/', $path), fn ($segment) => $segment !== ''));
$parts = array_values(array_filter(explode('/', $path), fn($segment) => $segment !== ''));
$length = count($parts) - 1;
$filteredParams = array_filter(self::$params, fn ($i) => $i <= $length);
$filteredParams = array_filter(self::$params, fn($i) => $i <= $length);

foreach (self::combinations($filteredParams) as $sample) {
$sample = array_filter($sample, fn (int $i) => $i <= $length);
$sample = array_filter($sample, fn(int $i) => $i <= $length);
$match = implode(
'/',
array_replace(
$parts,
array_fill_keys($sample, self::PLACEHOLDER_TOKEN)
)
array_fill_keys($sample, self::PLACEHOLDER_TOKEN),
),
);

if (array_key_exists($match, self::$routes[$method])) {
Expand Down
Loading
Loading