feature #35667 [DomCrawler] Rename UriExpander.php -> UriResolver (lyrixx)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[DomCrawler] Rename UriExpander.php -> UriResolver

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | refs https://github.com/symfony/symfony-docs/pull/13054#discussion_r377138605
| License       | MIT
| Doc PR        | https://github.com/symfony/symfony-docs/pull/13054 // don't open a new issue in doc repo

Commits
-------

3217f8182a [DomCrawler] Rename UriExpander.php -> UriResolver
This commit is contained in:
Fabien Potencier 2020-02-11 07:10:22 +01:00
commit 1b52d6035a
3 changed files with 24 additions and 23 deletions

View File

@ -5,7 +5,7 @@ CHANGELOG
----- -----
* Added an internal cache layer on top of the CssSelectorConverter * Added an internal cache layer on top of the CssSelectorConverter
* Added `UriExpander` to expand an URL according to another URL * Added `UriResolver` to resolve an URI according to a base URI
5.0.0 5.0.0
----- -----

View File

@ -12,19 +12,19 @@
namespace Symfony\Component\DomCrawler\Tests; namespace Symfony\Component\DomCrawler\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Symfony\Component\DomCrawler\UriExpander; use Symfony\Component\DomCrawler\UriResolver;
class UriExpanderTest extends TestCase class UriResolverTest extends TestCase
{ {
/** /**
* @dataProvider provideExpandUriTests * @dataProvider provideResolverTests
*/ */
public function testExpandUri(string $uri, string $currentUri, string $expected) public function testResolver(string $uri, string $baseUri, string $expected)
{ {
$this->assertEquals($expected, UriExpander::expand($uri, $currentUri)); $this->assertEquals($expected, UriResolver::resolve($uri, $baseUri));
} }
public function provideExpandUriTests() public function provideResolverTests()
{ {
return [ return [
['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'], ['/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'],

View File

@ -12,22 +12,23 @@
namespace Symfony\Component\DomCrawler; namespace Symfony\Component\DomCrawler;
/** /**
* Expand an URI according a current URI. * The UriResolver class takes an URI (relative, absolute, fragment, etc.)
* and turns it into an absolute URI against another given base URI.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Grégoire Pineau <lyrixx@lyrixx.info> * @author Grégoire Pineau <lyrixx@lyrixx.info>
*/ */
class UriExpander class UriResolver
{ {
/** /**
* Expand an URI according to a current Uri. * Resolves a URI according to a base URI.
* *
* For example if $uri=/foo/bar and $currentUri=https://symfony.com it will * For example if $uri=/foo/bar and $baseUri=https://symfony.com it will
* return https://symfony.com/foo/bar * return https://symfony.com/foo/bar
* *
* If the $uri is not absolute you must pass an absolute $currentUri * If the $uri is not absolute you must pass an absolute $baseUri
*/ */
public static function expand(string $uri, ?string $currentUri): string public static function resolve(string $uri, ?string $baseUri): string
{ {
$uri = trim($uri); $uri = trim($uri);
@ -36,43 +37,43 @@ class UriExpander
return $uri; return $uri;
} }
if (null === $currentUri) { if (null === $baseUri) {
throw new \InvalidArgumentException('The URI is relative, so you must define its base URI passing an absolute URL.'); throw new \InvalidArgumentException('The URI is relative, so you must define its base URI passing an absolute URL.');
} }
// empty URI // empty URI
if (!$uri) { if (!$uri) {
return $currentUri; return $baseUri;
} }
// an anchor // an anchor
if ('#' === $uri[0]) { if ('#' === $uri[0]) {
return self::cleanupAnchor($currentUri).$uri; return self::cleanupAnchor($baseUri).$uri;
} }
$baseUri = self::cleanupUri($currentUri); $baseUriCleaned = self::cleanupUri($baseUri);
if ('?' === $uri[0]) { if ('?' === $uri[0]) {
return $baseUri.$uri; return $baseUriCleaned.$uri;
} }
// absolute URL with relative schema // absolute URL with relative schema
if (0 === strpos($uri, '//')) { if (0 === strpos($uri, '//')) {
return preg_replace('#^([^/]*)//.*$#', '$1', $baseUri).$uri; return preg_replace('#^([^/]*)//.*$#', '$1', $baseUriCleaned).$uri;
} }
$baseUri = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUri); $baseUriCleaned = preg_replace('#^(.*?//[^/]*)(?:\/.*)?$#', '$1', $baseUriCleaned);
// absolute path // absolute path
if ('/' === $uri[0]) { if ('/' === $uri[0]) {
return $baseUri.$uri; return $baseUriCleaned.$uri;
} }
// relative path // relative path
$path = parse_url(substr($currentUri, \strlen($baseUri)), PHP_URL_PATH); $path = parse_url(substr($baseUri, \strlen($baseUriCleaned)), PHP_URL_PATH);
$path = self::canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri); $path = self::canonicalizePath(substr($path, 0, strrpos($path, '/')).'/'.$uri);
return $baseUri.('' === $path || '/' !== $path[0] ? '/' : '').$path; return $baseUriCleaned.('' === $path || '/' !== $path[0] ? '/' : '').$path;
} }
/** /**