Skip to content

Commit 8115c5c

Browse files
Allow relative and abolute paths for url-argument
makeRelative allows relative and absolute paths as first argument and assumes it belongs to the same hostname.
1 parent 561d397 commit 8115c5c

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/Url.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,22 @@ public static function makeRelative($url, $baseUrl)
4545
{
4646
Assert::string($url, 'The URL must be a string. Got: %s');
4747
Assert::string($baseUrl, 'The base URL must be a string. Got: %s');
48+
Assert::contains($baseUrl, '://', '%s is not an absolute Url.');
4849

49-
list($host, $path) = self::split($url);
5050
list($baseHost, $basePath) = self::split($baseUrl);
5151

52-
if ('' !== $host && '' !== $baseHost && $host !== $baseHost) {
52+
if (false === strpos($url, '://')) {
53+
if (0 === strpos($url, '/')) {
54+
$host = $baseHost;
55+
} else {
56+
$host = '';
57+
}
58+
$path = $url;
59+
} else {
60+
list($host, $path) = self::split($url);
61+
}
62+
63+
if ('' !== $host && $host !== $baseHost) {
5364
throw new InvalidArgumentException(sprintf(
5465
'The URL "%s" cannot be made relative to "%s" since their host names are different.',
5566
$host,
@@ -79,13 +90,7 @@ public static function makeRelative($url, $baseUrl)
7990
*/
8091
private static function split($url)
8192
{
82-
if (false === ($pos = strpos($url, '://'))) {
83-
throw new InvalidArgumentException(sprintf(
84-
'"%s" is not an absolute Url.',
85-
$url
86-
));
87-
}
88-
93+
$pos = strpos($url, '://');
8994
$scheme = substr($url, 0, $pos + 3);
9095
$url = substr($url, $pos + 3);
9196

tests/UrlTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,32 @@ public function testMakeRelative($absolutePath, $basePath, $relativePath)
3131

3232
$relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
3333
$this->assertSame($relativePath, $relative);
34+
$relative = Url::makeRelative($absolutePath, $host.$basePath);
35+
$this->assertSame($relativePath, $relative);
3436
}
3537

3638
/**
37-
* @dataProvider provideMakeRelativeTests
39+
* @dataProvider provideMakeRelativeIsAlreadyRelativeTests
3840
* @covers Webmozart\PathUtil\Url
3941
*/
40-
public function testMakeRelativeWithFullUrl($absolutePath, $basePath, $relativePath)
42+
public function testMakeRelativeIsAlreadyRelative($absolutePath, $basePath, $relativePath)
4143
{
42-
$host = 'ftp://user:password@example.com:8080';
44+
$host = 'http://example.com';
4345

44-
$relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
46+
$relative = Url::makeRelative($absolutePath, $host.$basePath);
4547
$this->assertSame($relativePath, $relative);
4648
}
4749

4850
/**
49-
* @expectedException \InvalidArgumentException
50-
* @expectedExceptionMessage "/webmozart/puli/css/style.css" is not an absolute Url.
51+
* @dataProvider provideMakeRelativeTests
5152
* @covers Webmozart\PathUtil\Url
5253
*/
53-
public function testMakeRelativeWithoutFullUrl()
54+
public function testMakeRelativeWithFullUrl($absolutePath, $basePath, $relativePath)
5455
{
55-
Url::makeRelative('/webmozart/puli/css/style.css', 'http://example.com/webmozart/puli');
56+
$host = 'ftp://user:password@example.com:8080';
57+
58+
$relative = Url::makeRelative($host.$absolutePath, $host.$basePath);
59+
$this->assertSame($relativePath, $relative);
5660
}
5761

5862
/**
@@ -149,6 +153,18 @@ public function provideMakeRelativeTests()
149153

150154
// second argument shorter than first
151155
array('/webmozart/puli', '/css', '../webmozart/puli'),
156+
157+
array('', '', ''),
158+
);
159+
}
160+
161+
public function provideMakeRelativeIsAlreadyRelativeTests()
162+
{
163+
return array(
164+
array('css/style.css', '/webmozart/puli', 'css/style.css'),
165+
array('css/style.css', '', 'css/style.css'),
166+
array('css/../style.css', '', 'style.css'),
167+
array('css/./style.css', '', 'css/style.css'),
152168
);
153169
}
154170
}

0 commit comments

Comments
 (0)