Extract Http\Factory\UriFactory from ServerRequestFactory
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
2640d7b50d
commit
f8cd1c258d
@ -6,47 +6,27 @@ namespace PhpMyAdmin\Http\Factory;
|
||||
|
||||
use GuzzleHttp\Psr7\HttpFactory;
|
||||
use HttpSoft\Message\ServerRequestFactory as HttpSoftServerRequestFactory;
|
||||
use HttpSoft\Message\UriFactory as HttpSoftUriFactory;
|
||||
use Laminas\Diactoros\ServerRequestFactory as LaminasServerRequestFactory;
|
||||
use Laminas\Diactoros\UriFactory as LaminasUriFactory;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use PhpMyAdmin\Http\ServerRequest;
|
||||
use Psr\Http\Message\ServerRequestFactoryInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Message\UriFactoryInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use Slim\Psr7\Factory\ServerRequestFactory as SlimServerRequestFactory;
|
||||
use Slim\Psr7\Factory\UriFactory as SlimUriFactory;
|
||||
|
||||
use function class_exists;
|
||||
use function count;
|
||||
use function current;
|
||||
use function explode;
|
||||
use function function_exists;
|
||||
use function getallheaders;
|
||||
use function in_array;
|
||||
use function is_numeric;
|
||||
use function is_string;
|
||||
use function parse_url;
|
||||
use function preg_match;
|
||||
use function strpos;
|
||||
use function strstr;
|
||||
use function substr;
|
||||
|
||||
use const PHP_URL_QUERY;
|
||||
|
||||
class ServerRequestFactory
|
||||
{
|
||||
private ServerRequestFactoryInterface $serverRequestFactory;
|
||||
|
||||
private UriFactoryInterface $uriFactory;
|
||||
|
||||
public function __construct(
|
||||
ServerRequestFactoryInterface|null $serverRequestFactory = null,
|
||||
UriFactoryInterface|null $uriFactory = null,
|
||||
) {
|
||||
public function __construct(ServerRequestFactoryInterface|null $serverRequestFactory = null)
|
||||
{
|
||||
$this->serverRequestFactory = $serverRequestFactory ?? $this->createServerRequestFactory();
|
||||
$this->uriFactory = $uriFactory ?? $this->createUriFactory();
|
||||
}
|
||||
|
||||
private function createServerRequestFactory(): ServerRequestFactoryInterface
|
||||
@ -70,27 +50,6 @@ class ServerRequestFactory
|
||||
return $factory;
|
||||
}
|
||||
|
||||
private function createUriFactory(): UriFactoryInterface
|
||||
{
|
||||
if (class_exists(Psr17Factory::class)) {
|
||||
/** @var UriFactoryInterface $factory */
|
||||
$factory = new Psr17Factory();
|
||||
} elseif (class_exists(HttpFactory::class)) {
|
||||
/** @var UriFactoryInterface $factory */
|
||||
$factory = new HttpFactory();
|
||||
} elseif (class_exists(LaminasUriFactory::class)) {
|
||||
/** @var UriFactoryInterface $factory */
|
||||
$factory = new LaminasUriFactory();
|
||||
} elseif (class_exists(HttpSoftUriFactory::class)) {
|
||||
/** @var UriFactoryInterface $factory */
|
||||
$factory = new HttpSoftUriFactory();
|
||||
} else {
|
||||
$factory = new SlimUriFactory();
|
||||
}
|
||||
|
||||
return $factory;
|
||||
}
|
||||
|
||||
public static function createFromGlobals(): ServerRequest
|
||||
{
|
||||
if (class_exists(SlimServerRequestFactory::class)) {
|
||||
@ -118,9 +77,10 @@ class ServerRequestFactory
|
||||
|
||||
private static function createServerRequestFromGlobals(self $creator): ServerRequestInterface
|
||||
{
|
||||
$uriFactory = UriFactory::create();
|
||||
$serverRequest = $creator->serverRequestFactory->createServerRequest(
|
||||
$_SERVER['REQUEST_METHOD'] ?? 'GET',
|
||||
$creator->createUriFromGlobals($_SERVER),
|
||||
$uriFactory->fromGlobals($_SERVER),
|
||||
$_SERVER,
|
||||
);
|
||||
|
||||
@ -145,68 +105,4 @@ class ServerRequestFactory
|
||||
|
||||
return $serverRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new Uri from environment.
|
||||
*
|
||||
* Initially based on the \Slim\Psr7\Factory\UriFactory::createFromGlobals() implementation.
|
||||
*
|
||||
* @param mixed[] $server
|
||||
*/
|
||||
private function createUriFromGlobals(array $server): UriInterface
|
||||
{
|
||||
$uri = $this->uriFactory->createUri('');
|
||||
|
||||
$uri = $uri->withScheme(! isset($server['HTTPS']) || $server['HTTPS'] === 'off' ? 'http' : 'https');
|
||||
|
||||
if (isset($server['PHP_AUTH_USER']) && is_string($server['PHP_AUTH_USER']) && $server['PHP_AUTH_USER'] !== '') {
|
||||
$uri = $uri->withUserInfo(
|
||||
$server['PHP_AUTH_USER'],
|
||||
isset($server['PHP_AUTH_PW']) && is_string($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : null,
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($server['HTTP_HOST']) && is_string($server['HTTP_HOST'])) {
|
||||
$uri = $uri->withHost($server['HTTP_HOST']);
|
||||
} elseif (isset($server['SERVER_NAME']) && is_string($server['SERVER_NAME'])) {
|
||||
$uri = $uri->withHost($server['SERVER_NAME']);
|
||||
}
|
||||
|
||||
if (isset($server['SERVER_PORT']) && is_numeric($server['SERVER_PORT']) && $server['SERVER_PORT'] >= 1) {
|
||||
$uri = $uri->withPort((int) $server['SERVER_PORT']);
|
||||
} else {
|
||||
$uri = $uri->withPort($uri->getScheme() === 'https' ? 443 : 80);
|
||||
}
|
||||
|
||||
if (preg_match('/^(\[[a-fA-F0-9:.]+])(:\d+)?\z/', $uri->getHost(), $matches)) {
|
||||
$uri = $uri->withHost($matches[1]);
|
||||
if (isset($matches[2])) {
|
||||
$uri = $uri->withPort((int) substr($matches[2], 1));
|
||||
}
|
||||
} else {
|
||||
$pos = strpos($uri->getHost(), ':');
|
||||
if ($pos !== false) {
|
||||
$port = (int) substr($uri->getHost(), $pos + 1);
|
||||
$host = (string) strstr($uri->getHost(), ':', true);
|
||||
$uri = $uri->withHost($host)->withPort($port);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($server['QUERY_STRING']) && is_string($server['QUERY_STRING'])) {
|
||||
$uri = $uri->withQuery($server['QUERY_STRING']);
|
||||
}
|
||||
|
||||
if (isset($server['REQUEST_URI']) && is_string($server['REQUEST_URI'])) {
|
||||
$uriFragments = explode('?', $server['REQUEST_URI']);
|
||||
$uri = $uri->withPath($uriFragments[0]);
|
||||
if ($uri->getQuery() === '' && count($uriFragments) > 1) {
|
||||
$query = parse_url('https://www.example.com' . $server['REQUEST_URI'], PHP_URL_QUERY);
|
||||
if (is_string($query) && $query !== '') {
|
||||
$uri = $uri->withQuery($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
}
|
||||
|
||||
124
libraries/classes/Http/Factory/UriFactory.php
Normal file
124
libraries/classes/Http/Factory/UriFactory.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Http\Factory;
|
||||
|
||||
use GuzzleHttp\Psr7\HttpFactory;
|
||||
use HttpSoft\Message\UriFactory as HttpSoftUriFactory;
|
||||
use Laminas\Diactoros\UriFactory as LaminasUriFactory;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use Psr\Http\Message\UriFactoryInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use RuntimeException;
|
||||
use Slim\Psr7\Factory\UriFactory as SlimUriFactory;
|
||||
|
||||
use function class_exists;
|
||||
use function count;
|
||||
use function explode;
|
||||
use function is_numeric;
|
||||
use function is_string;
|
||||
use function parse_url;
|
||||
use function preg_match;
|
||||
use function strpos;
|
||||
use function strstr;
|
||||
use function substr;
|
||||
|
||||
use const PHP_URL_QUERY;
|
||||
|
||||
final class UriFactory implements UriFactoryInterface
|
||||
{
|
||||
/** @psalm-var list<class-string<UriFactoryInterface>> */
|
||||
private static array $providers = [
|
||||
SlimUriFactory::class,
|
||||
LaminasUriFactory::class,
|
||||
Psr17Factory::class,
|
||||
HttpFactory::class,
|
||||
HttpSoftUriFactory::class,
|
||||
];
|
||||
|
||||
public function __construct(private UriFactoryInterface $uriFactory)
|
||||
{
|
||||
}
|
||||
|
||||
public function createUri(string $uri = ''): UriInterface
|
||||
{
|
||||
return $this->uriFactory->createUri($uri);
|
||||
}
|
||||
|
||||
/** @throws RuntimeException When no {@see UriFactoryInterface} implementation is found. */
|
||||
public static function create(): self
|
||||
{
|
||||
foreach (self::$providers as $provider) {
|
||||
if (class_exists($provider)) {
|
||||
return new self(new $provider());
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('No URI factories found.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new Uri from environment.
|
||||
*
|
||||
* Initially based on the \Slim\Psr7\Factory\UriFactory::createFromGlobals() implementation.
|
||||
*
|
||||
* @param mixed[] $server
|
||||
*/
|
||||
public function fromGlobals(array $server): UriInterface
|
||||
{
|
||||
$uri = $this->createUri('');
|
||||
|
||||
$uri = $uri->withScheme(! isset($server['HTTPS']) || $server['HTTPS'] === 'off' ? 'http' : 'https');
|
||||
|
||||
if (isset($server['PHP_AUTH_USER']) && is_string($server['PHP_AUTH_USER']) && $server['PHP_AUTH_USER'] !== '') {
|
||||
$uri = $uri->withUserInfo(
|
||||
$server['PHP_AUTH_USER'],
|
||||
isset($server['PHP_AUTH_PW']) && is_string($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : null,
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($server['HTTP_HOST']) && is_string($server['HTTP_HOST'])) {
|
||||
$uri = $uri->withHost($server['HTTP_HOST']);
|
||||
} elseif (isset($server['SERVER_NAME']) && is_string($server['SERVER_NAME'])) {
|
||||
$uri = $uri->withHost($server['SERVER_NAME']);
|
||||
}
|
||||
|
||||
if (isset($server['SERVER_PORT']) && is_numeric($server['SERVER_PORT']) && $server['SERVER_PORT'] >= 1) {
|
||||
$uri = $uri->withPort((int) $server['SERVER_PORT']);
|
||||
} else {
|
||||
$uri = $uri->withPort($uri->getScheme() === 'https' ? 443 : 80);
|
||||
}
|
||||
|
||||
if (preg_match('/^(\[[a-fA-F0-9:.]+])(:\d+)?\z/', $uri->getHost(), $matches)) {
|
||||
$uri = $uri->withHost($matches[1]);
|
||||
if (isset($matches[2])) {
|
||||
$uri = $uri->withPort((int) substr($matches[2], 1));
|
||||
}
|
||||
} else {
|
||||
$pos = strpos($uri->getHost(), ':');
|
||||
if ($pos !== false) {
|
||||
$port = (int) substr($uri->getHost(), $pos + 1);
|
||||
$host = (string) strstr($uri->getHost(), ':', true);
|
||||
$uri = $uri->withHost($host)->withPort($port);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($server['QUERY_STRING']) && is_string($server['QUERY_STRING'])) {
|
||||
$uri = $uri->withQuery($server['QUERY_STRING']);
|
||||
}
|
||||
|
||||
if (isset($server['REQUEST_URI']) && is_string($server['REQUEST_URI'])) {
|
||||
$uriFragments = explode('?', $server['REQUEST_URI']);
|
||||
$uri = $uri->withPath($uriFragments[0]);
|
||||
if ($uri->getQuery() === '' && count($uriFragments) > 1) {
|
||||
$query = parse_url('https://www.example.com' . $server['REQUEST_URI'], PHP_URL_QUERY);
|
||||
if (is_string($query) && $query !== '') {
|
||||
$uri = $uri->withQuery($query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
}
|
||||
@ -14080,6 +14080,12 @@
|
||||
<code>dataProviderPsr7Implementations</code>
|
||||
</PossiblyUnusedMethod>
|
||||
</file>
|
||||
<file src="test/classes/Http/Factory/UriFactoryTest.php">
|
||||
<PossiblyUnusedMethod>
|
||||
<code>providerForTestCreateUri</code>
|
||||
<code>uriFactoryProviders</code>
|
||||
</PossiblyUnusedMethod>
|
||||
</file>
|
||||
<file src="test/classes/Http/ResponseTest.php">
|
||||
<PossiblyUnusedMethod>
|
||||
<code>responseFactoryProviders</code>
|
||||
|
||||
139
test/classes/Http/Factory/UriFactoryTest.php
Normal file
139
test/classes/Http/Factory/UriFactoryTest.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests\Http\Factory;
|
||||
|
||||
use GuzzleHttp\Psr7\HttpFactory;
|
||||
use HttpSoft\Message\UriFactory as HttpSoftUriFactory;
|
||||
use Laminas\Diactoros\UriFactory as LaminasUriFactory;
|
||||
use Nyholm\Psr7\Factory\Psr17Factory;
|
||||
use PhpMyAdmin\Http\Factory\UriFactory;
|
||||
use PHPUnit\Framework\Attributes\BackupStaticProperties;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\UriFactoryInterface;
|
||||
use Psr\Http\Message\UriInterface;
|
||||
use ReflectionProperty;
|
||||
use RuntimeException;
|
||||
use Slim\Psr7\Factory\UriFactory as SlimUriFactory;
|
||||
use Slim\Psr7\Uri;
|
||||
|
||||
use function class_exists;
|
||||
|
||||
#[CoversClass(UriFactory::class)]
|
||||
final class UriFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @psalm-param class-string<UriFactoryInterface> $provider
|
||||
* @psalm-param class-string<UriInterface> $expectedUri
|
||||
*/
|
||||
#[DataProvider('providerForTestCreateUri')]
|
||||
public function testCreateUri(string $provider, string $expectedUri): void
|
||||
{
|
||||
$this->skipIfNotAvailable($provider);
|
||||
$uriFactory = new UriFactory(new $provider());
|
||||
$uri = $uriFactory->createUri('https://www.phpmyadmin.net/');
|
||||
$this->assertInstanceOf($expectedUri, $uri);
|
||||
}
|
||||
|
||||
/** @psalm-return iterable<string, array{class-string<UriFactoryInterface>, class-string<UriInterface>}> */
|
||||
public static function providerForTestCreateUri(): iterable
|
||||
{
|
||||
yield 'slim/psr7' => [SlimUriFactory::class, Uri::class];
|
||||
yield 'laminas/laminas-diactoros' => [LaminasUriFactory::class, \Laminas\Diactoros\Uri::class];
|
||||
yield 'nyholm/psr7' => [Psr17Factory::class, \Nyholm\Psr7\Uri::class];
|
||||
yield 'guzzlehttp/psr7' => [HttpFactory::class, \GuzzleHttp\Psr7\Uri::class];
|
||||
yield 'httpsoft/http-message' => [HttpSoftUriFactory::class, \HttpSoft\Message\Uri::class];
|
||||
}
|
||||
|
||||
/** @psalm-param class-string<UriFactoryInterface> $provider */
|
||||
#[DataProvider('uriFactoryProviders')]
|
||||
#[BackupStaticProperties(true)]
|
||||
public function testCreate(string $provider): void
|
||||
{
|
||||
$this->skipIfNotAvailable($provider);
|
||||
(new ReflectionProperty(UriFactory::class, 'providers'))->setValue([$provider]);
|
||||
$uriFactory = UriFactory::create();
|
||||
$actual = (new ReflectionProperty(UriFactory::class, 'uriFactory'))->getValue($uriFactory);
|
||||
$this->assertInstanceOf($provider, $actual);
|
||||
}
|
||||
|
||||
/** @psalm-return iterable<string, array{class-string<UriFactoryInterface>}> */
|
||||
public static function uriFactoryProviders(): iterable
|
||||
{
|
||||
yield 'slim/psr7' => [SlimUriFactory::class];
|
||||
yield 'laminas/laminas-diactoros' => [LaminasUriFactory::class];
|
||||
yield 'nyholm/psr7' => [Psr17Factory::class];
|
||||
yield 'guzzlehttp/psr7' => [HttpFactory::class];
|
||||
yield 'httpsoft/http-message' => [HttpSoftUriFactory::class];
|
||||
}
|
||||
|
||||
#[BackupStaticProperties(true)]
|
||||
public function testCreateWithoutProvider(): void
|
||||
{
|
||||
(new ReflectionProperty(UriFactory::class, 'providers'))->setValue(['InvalidUriFactoryClass']);
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('No URI factories found.');
|
||||
UriFactory::create();
|
||||
}
|
||||
|
||||
/** @psalm-param class-string<UriFactoryInterface> $provider */
|
||||
#[DataProvider('uriFactoryProviders')]
|
||||
public function testCreateFromGlobals(string $provider): void
|
||||
{
|
||||
$this->skipIfNotAvailable($provider);
|
||||
$uriFactory = new UriFactory(new $provider());
|
||||
$uri = $uriFactory->fromGlobals([
|
||||
'SERVER_PORT' => '8080',
|
||||
'REQUEST_URI' => '/index.php?route=/server/plugins',
|
||||
'PHP_AUTH_USER' => 'username',
|
||||
'PHP_AUTH_PW' => 'password',
|
||||
'SCRIPT_NAME' => '/index.php',
|
||||
'QUERY_STRING' => 'route=/server/plugins',
|
||||
'HTTP_HOST' => 'example.com:8080',
|
||||
]);
|
||||
$this->assertSame('http://username:password@example.com:8080/index.php?route=/server/plugins', (string) $uri);
|
||||
}
|
||||
|
||||
/** @psalm-param class-string<UriFactoryInterface> $provider */
|
||||
#[DataProvider('uriFactoryProviders')]
|
||||
public function testCreateFromGlobals2(string $provider): void
|
||||
{
|
||||
$this->skipIfNotAvailable($provider);
|
||||
$uriFactory = new UriFactory(new $provider());
|
||||
$uri = $uriFactory->fromGlobals([
|
||||
'SERVER_PORT' => '0',
|
||||
'PHP_AUTH_USER' => 'username',
|
||||
'SERVER_NAME' => 'example.com',
|
||||
'HTTPS' => 'on',
|
||||
]);
|
||||
$this->assertSame('https://username@example.com', (string) $uri);
|
||||
}
|
||||
|
||||
/** @psalm-param class-string<UriFactoryInterface> $provider */
|
||||
#[DataProvider('uriFactoryProviders')]
|
||||
public function testCreateFromGlobals3(string $provider): void
|
||||
{
|
||||
$this->skipIfNotAvailable($provider);
|
||||
$uriFactory = new UriFactory(new $provider());
|
||||
$uri = $uriFactory->fromGlobals([
|
||||
'HTTP_HOST' => '[2001:DB8::1]',
|
||||
'HTTPS' => 'off',
|
||||
'REQUEST_URI' => '/index.php?route=/server/plugins',
|
||||
]);
|
||||
$this->assertSame('http://[2001:db8::1]/index.php?route=/server/plugins', (string) $uri);
|
||||
}
|
||||
|
||||
/** @psalm-param class-string<UriFactoryInterface> $provider */
|
||||
private function skipIfNotAvailable(string $provider): void
|
||||
{
|
||||
if (class_exists($provider)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// This can happen when testing without the development packages.
|
||||
$this->markTestSkipped($provider . ' is not available.');
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user