Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PHP 8.5 polyfill #498

Draft
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Draft
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: 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 <[email protected]>
*
* 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The classes in core don't have a parent. Shall we alias the class instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see class Rfc3986Uri extends Uri in the RFC. Do I miss your point?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but Symfony\Polyfill\Php85\Uri\Rfc3986Uri does not appear in the original inheritance chain. What is the point of the Symfony namespaced classes anyway? Do we need them?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having all the logic being in internal namespaced classes allows to test the logic of those classes even on versions where the polyfill is not applied because the native class exist. That's why all our polyfill classes have such internal parent class.

{
}
}
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 <[email protected]>
*
* 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 <[email protected]>
*
* 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 <[email protected]>
*
* 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
{
}
}
121 changes: 121 additions & 0 deletions src/Php85/Uri/Rfc3986Uri.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Symfony\Polyfill\Php85\Uri;

use Symfony\Polyfill\Php85\Exception\ParsingException;

/**
* @author Alexandre Daubois <[email protected]>
*
* @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');
alexandre-daubois marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

$this->initialized = true;
}

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;
}

public function __toString()
{
$uri = '';

if (null !== $this->scheme) {
$uri .= $this->scheme.':';
}

if (null !== $this->host) {
$uri .= '//';
if (null !== $this->user) {
$uri .= rawurlencode($this->user);
if (null !== $this->password) {
$uri .= ':'.rawurlencode($this->password);
}
$uri .= '@';
}
$uri .= $this->host;
if (null !== $this->port) {
$uri .= ':'.$this->port;
}
}

if (null !== $this->path) {
$uri .= '/'.$this->path;
}

if (null !== $this->query) {
$uri .= '?'.$this->query;
}

if (null !== $this->fragment) {
$uri .= '#'.$this->fragment;
}

return $uri;
}
}
Loading
Loading