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
Expand Up @@ -2,9 +2,9 @@

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

use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Http\Http;
use Utopia\Http\Response;
use Utopia\Http\Adapter\Swoole\Server;
use Utopia\Validator\Text;

Http::get('/')
Expand Down
25 changes: 24 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
{
"preset": "per"
"preset": "per",
"rules": {
"native_function_invocation": {
"include": ["@compiler_optimized"],
"scope": "namespaced",
"strict": true
},
"no_unused_imports": true,
"ordered_imports": {
"sort_algorithm": "alpha"
},
"no_useless_else": true,
"no_useless_return": true,
"no_empty_statement": true,
"no_empty_phpdoc": true,
"no_empty_comment": true,
"array_syntax": {
"syntax": "short"
},
"single_quote": true,
"trailing_comma_in_multiline": {
"elements": ["arrays", "arguments", "parameters"]
}
}
}
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php70\Rector\FuncCall\RandomFunctionRector;
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
Expand All @@ -20,6 +19,7 @@
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;

return RectorConfig::configure()
->withPaths([
Expand Down
22 changes: 11 additions & 11 deletions src/Http/Adapter/FPM/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function getIP(): string
}

// Leftmost IP address is the address of the originating client
$ips = \explode(',', $headerValue);
$ip = \trim($ips[0]);
$ips = explode(',', $headerValue);
$ip = trim($ips[0]);

// Validate IP format (supports both IPv4 and IPv6)
if (\filter_var($ip, FILTER_VALIDATE_IP)) {
if (filter_var($ip, FILTER_VALIDATE_IP)) {
return $ip;
}
}
Expand All @@ -97,7 +97,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 @@ -107,7 +107,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 @@ -233,7 +233,7 @@ public function getHeader(string $key, string $default = ''): string

$value = $headers[$key];

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

/**
Expand Down Expand Up @@ -278,14 +278,14 @@ protected function generateInput(): array
$contentType = $this->getHeader('content-type');

// Get content-type without the charset
$length = \strpos($contentType, ';');
$length = strpos($contentType, ';');
$length = (empty($length)) ? \strlen($contentType) : $length;
$contentType = \substr($contentType, 0, $length);
$contentType = substr($contentType, 0, $length);

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

$this->payload = match ($contentType) {
'application/json' => \json_decode($this->rawPayload, true),
'application/json' => json_decode($this->rawPayload, true),
default => $_POST,
};

Expand Down Expand Up @@ -323,7 +323,7 @@ protected function generateHeaders(): array

foreach ($_SERVER as $name => $value) {
if (str_starts_with($name, 'HTTP_')) {
$headers[\str_replace(' ', '-', \strtolower(\str_replace('_', ' ', \substr($name, 5))))] = $value;
$headers[str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($name, 5))))] = $value;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/Adapter/FPM/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function write(string $content): bool
*/
public function end(?string $content = null): void
{
if (!is_null($content)) {
if (!\is_null($content)) {
echo $content;
}
}
Expand All @@ -53,10 +53,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 All @@ -74,6 +74,6 @@ protected function sendCookie(string $name, string $value, array $options): void
unset($options['expire']);

// Set the cookie
\setcookie($name, $value, $options);
setcookie($name, $value, $options);
}
}
10 changes: 5 additions & 5 deletions src/Http/Adapter/Swoole/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,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 @@ -121,8 +121,8 @@ public function getPort(): string
*/
public function getHostname(): string
{
$hostname = \parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
return strtolower(strval($hostname));
$hostname = parse_url($this->getProtocol() . '://' . $this->getHeader('x-forwarded-host', $this->getHeader('host')), PHP_URL_HOST);
return strtolower(\strval($hostname));
}

/**
Expand Down Expand Up @@ -291,11 +291,11 @@ protected function generateInput(): array

// Get content-type without the charset
$length = strpos($contentType, ';');
$length = (empty($length)) ? strlen($contentType) : $length;
$length = (empty($length)) ? \strlen($contentType) : $length;
$contentType = substr($contentType, 0, $length);

$this->payload = match ($contentType) {
'application/json' => json_decode(strval($this->swoole->rawContent()), true),
'application/json' => json_decode(\strval($this->swoole->rawContent()), true),
default => $this->swoole->post,
};

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Adapter/Swoole/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Response extends UtopiaResponse
public function __construct(SwooleResponse $response)
{
$this->swoole = $response;
parent::__construct(\microtime(true));
parent::__construct(microtime(true));
}

public function getSwooleResponse(): SwooleResponse
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Adapter/Swoole/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Utopia\Http\Adapter\Swoole;

use Swoole\Coroutine;
use Utopia\Http\Adapter;
use Utopia\DI\Container;
use Swoole\Http\Server as SwooleServer;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server as SwooleServer;
use Utopia\DI\Container;
use Utopia\Http\Adapter;

class Server extends Adapter
{
protected SwooleServer $server;
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
protected const string REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
protected Container $container;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Http/Adapter/SwooleCoroutine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Utopia\Http\Adapter\SwooleCoroutine;

use Swoole\Coroutine;
use Utopia\Http\Adapter;
use Utopia\DI\Container;
use Swoole\Coroutine\Http\Server as SwooleServer;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Utopia\DI\Container;
use Utopia\Http\Adapter;

class Server extends Adapter
{
protected const REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';
protected const string REQUEST_CONTAINER_CONTEXT_KEY = '__utopia_http_request_container';

protected SwooleServer $server;
protected Container $container;
Expand Down
22 changes: 11 additions & 11 deletions src/Http/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Files
/**
* @var array<string, mixed>
*/
public const EXTENSIONS = [
public const array EXTENSIONS = [
'css' => 'text/css',
'js' => 'text/javascript',
'svg' => 'image/svg+xml',
Expand Down Expand Up @@ -79,7 +79,7 @@ public function load(string $directory, ?string $root = null): void

$root ??= $directory;

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

if ($handle === false) {
throw new Exception("Failed to open directory: {$directory}");
Expand All @@ -88,11 +88,11 @@ public function load(string $directory, ?string $root = null): void
while ($path = readdir($handle)) {
$extension = pathinfo($path, PATHINFO_EXTENSION);

if (in_array($path, ['.', '..'])) {
if (\in_array($path, ['.', '..'])) {
continue;
}

if (in_array($extension, ['php', 'phtml'])) {
if (\in_array($extension, ['php', 'phtml'])) {
continue;
}

Expand All @@ -103,20 +103,20 @@ public function load(string $directory, ?string $root = null): void
$dirPath = $directory . '/' . $path;

if (is_dir($dirPath)) {
$this->load($dirPath, strval($root));
$this->load($dirPath, \strval($root));

continue;
}

$key = substr($dirPath, strlen(strval($root)));
$key = substr($dirPath, \strlen(\strval($root)));

if (array_key_exists($key, $this->loaded)) {
if (\array_key_exists($key, $this->loaded)) {
continue;
}

$this->loaded[$key] = [
'contents' => file_get_contents($dirPath),
'mimeType' => (array_key_exists($extension, self::EXTENSIONS))
'mimeType' => (\array_key_exists($extension, self::EXTENSIONS))
? self::EXTENSIONS[$extension]
: mime_content_type($dirPath),
];
Expand All @@ -132,7 +132,7 @@ public function load(string $directory, ?string $root = null): void
*/
public function isFileLoaded(string $uri): bool
{
return array_key_exists($uri, $this->loaded);
return \array_key_exists($uri, $this->loaded);
}

/**
Expand All @@ -143,7 +143,7 @@ public function isFileLoaded(string $uri): bool
*/
public function getFileContents(string $uri): mixed
{
if (!array_key_exists($uri, $this->loaded)) {
if (!\array_key_exists($uri, $this->loaded)) {
throw new Exception('File not found or not loaded: ' . $uri);
}

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

Expand Down
Loading
Loading