Skip to content

Commit

Permalink
Introduce PHP 8.5 polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-daubois committed Sep 17, 2024
1 parent 731f6e1 commit fdbffb2
Show file tree
Hide file tree
Showing 15 changed files with 789 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/polyfill-php82": "self.version",
"symfony/polyfill-php83": "self.version",
"symfony/polyfill-php84": "self.version",
"symfony/polyfill-php85": "self.version",
"symfony/polyfill-iconv": "self.version",
"symfony/polyfill-intl-grapheme": "self.version",
"symfony/polyfill-intl-icu": "self.version",
Expand Down Expand Up @@ -62,6 +63,7 @@
"src/Intl/Icu/Resources/stubs",
"src/Intl/MessageFormatter/Resources/stubs",
"src/Intl/Normalizer/Resources/stubs",
"src/Php85/Resources/stubs",
"src/Php84/Resources/stubs",
"src/Php83/Resources/stubs",
"src/Php82/Resources/stubs",
Expand Down
7 changes: 7 additions & 0 deletions src/Php85/Exception/ParsingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Polyfill\Php85\Exception;

class ParsingException extends \RuntimeException
{
}
19 changes: 19 additions & 0 deletions src/Php85/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
14 changes: 14 additions & 0 deletions src/Php85/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Symfony Polyfill / Php85
========================

This component provides features added to PHP 8.5 core:

- [RFC3986 and WHATWG compliant URI parsing support](https://wiki.php.net/rfc/url_parsing_api)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

License
=======

This library is released under the [MIT license](LICENSE).
18 changes: 18 additions & 0 deletions src/Php85/Resources/stubs/Uri/Rfc3986Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Uri;

if (\PHP_VERSION_ID < 80500) {
class Rfc3986Uri extends \Symfony\Polyfill\Php85\Uri\Rfc3986Uri
{
}
}
20 changes: 20 additions & 0 deletions src/Php85/Resources/stubs/Uri/Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Uri;

use Symfony\Polyfill\Php85 as p;

if (\PHP_VERSION_ID < 80500) {
abstract class Uri extends p\Uri\Uri
{
}
}
18 changes: 18 additions & 0 deletions src/Php85/Resources/stubs/Uri/WhatWgError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Uri;

if (\PHP_VERSION_ID < 80500) {
final class WhatWgError extends \Symfony\Polyfill\Php85\Uri\WhatWgError
{
}
}
18 changes: 18 additions & 0 deletions src/Php85/Resources/stubs/Uri/WhatWgUri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Uri;

if (\PHP_VERSION_ID < 80500) {
class WhatWgUri extends \Symfony\Polyfill\Php85\Uri\WhatWgUri
{
}
}
81 changes: 81 additions & 0 deletions src/Php85/Uri/Rfc3986Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Symfony\Polyfill\Php85\Uri;

use Symfony\Polyfill\Php85\Exception\ParsingException;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @internal
*/
class Rfc3986Uri extends \Uri\Uri
{
public function __construct(string $uri, ?string $baseUrl = null)
{
if ('' === trim($uri)) {
throw new \ValueError('Argument #1 ($uri) cannot be empty');
}

if (null !== $baseUrl && '' === trim($baseUrl)) {
throw new \ValueError('Argument #2 ($baseUrl) cannot be empty');
}

try {
$this->parse($uri, $baseUrl);
} catch (ParsingException) {
throw new \Error('Argument #1 ($uri) must be a valid URI');
}
}

private function parse(string $uri, ?string $baseUrl): void
{
if (!preg_match('/^[a-zA-Z][a-zA-Z\d+\-.]*:/', $uri) && null !== $baseUrl) {
// uri is a relative uri and bse url exists
$this->parse(rtrim($baseUrl, '/').'/'.ltrim($uri, '/'), null);

return;
}

if (preg_match('/[^\x20-\x7e]/', $uri)) {
// the string contains non-ascii chars
throw new ParsingException();
}

preg_match(self::URI_GLOBAL_REGEX, $uri, $matches);
if (!$matches || !isset($matches['scheme']) || '' === $matches['scheme']) {
//throw new InvalidUriException($uri);
}

if (preg_match('~'.$matches['scheme'].':/(?!/)~', $uri)) {
//throw new InvalidUriException($uri);
}

if (isset($matches['authority'])) {
if (!str_contains($uri, '://') && '' !== $matches['authority']) {
//throw new InvalidUriException($uri);
}

preg_match(self::URI_AUTHORITY_REGEX, $matches['authority'], $authMatches);

$matches = array_merge($matches, $authMatches);
unset($matches['authority']);
}

$matches = array_filter($matches, function (string $value) { return '' !== $value; });

if (isset($matches['host']) && false === \filter_var($matches['host'], FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) {
// the host contains invalid code points
throw new ParsingException();
}

$this->scheme = $matches['scheme'] ?? null;
$this->user = isset($matches['user']) ? rawurldecode($matches['user']) : null;
$this->password = isset($matches['pass']) ? rawurldecode($matches['pass']) : null;
$this->host = $matches['host'] ?? null;
$this->port = $matches['port'] ?? null;
$this->path = isset($matches['path']) ? ltrim($matches['path'], '/') : null;
$this->query = $matches['query'] ?? null;
$this->fragment = $matches['fragment'] ?? null;
}
}
133 changes: 133 additions & 0 deletions src/Php85/Uri/Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Polyfill\Php85\Uri;

/**
* @author Alexandre Daubois <alex.daubois@gmail.com>
*
* @internal
*/
abstract class Uri implements \Stringable
{
protected const URI_GLOBAL_REGEX = '/^(?:(?P<scheme>[^:\/?#]+):)?(?:\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(?:\?(?P<query>[^#]*))?(?:#(?P<fragment>.*))?$/';
protected const URI_AUTHORITY_REGEX = '/^(?:(?P<user>[^:@]*)(?::(?P<pass>[^@]*))?@)?(?P<host>[^:]*)(?::(?P<port>\d*))?$/';

/**
* @var string|null
*/
protected $scheme;

/**
* @var string|null
*/
protected $user;

/**
* @var string|null
*/
protected $password;

/**
* @var string|null
*/
protected $host;

/**
* @var int|null
*/
protected $port;

/**
* @var string|null
*/
protected $path;

/**
* @var string|null
*/
protected $query;

/**
* @var string|null
*/
protected $fragment;

public static function fromRfc3986(string $uri, ?string $baseUrl = null): ?static
{
try {
return new Rfc3986Uri($uri, $baseUrl);
} catch (\ValueError $error) {
throw $error;
} catch (\Error $error) {
return null;
}
}

/**
* @param array<int, WhatWgError> $errors
*/
public static function fromWhatWg(string $uri, ?string $baseUrl = null, &$errors = null): ?static
{
$uri = new WhatWgUri($uri, $baseUrl, $errors);

if ($errors) {
return null;
}

return $uri;
}

public function getScheme(): ?string
{
return $this->scheme;
}

public function getUser(): ?string
{
return $this->user;
}

public function getPassword(): ?string
{
return $this->password;
}

public function getHost(): ?string
{
return $this->host;
}

public function getPort(): ?int
{
return $this->port;
}

public function getPath(): ?string
{
return $this->path;
}

public function getQuery(): ?string
{
return $this->query;
}

public function getFragment(): ?string
{
return $this->fragment;
}

public function __toString(): string
{

}
}
Loading

0 comments on commit fdbffb2

Please sign in to comment.