-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
alexandre-daubois
wants to merge
1
commit into
symfony:1.x
Choose a base branch
from
alexandre-daubois:uri-polyfill
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+998
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Polyfill\Php85\Exception; | ||
|
||
class ParsingException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.