Skip to content

Commit

Permalink
Implement "relative_url" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMasterov committed Apr 6, 2016
1 parent c8ace5e commit 4714ba3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/Extension/RequestExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function getName()
public function getFunctions()
{
return [
new \Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl'])
new \Twig_SimpleFunction('absolute_url', [$this, 'generateAbsoluteUrl']),
new \Twig_SimpleFunction('relative_url', [$this, 'generateRelativeUrl'])
];
}

Expand All @@ -39,14 +40,59 @@ public function generateAbsoluteUrl($path)
}

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

if (! $this->hasLeadingSlash($path)) {
$path = rtrim($uri->getPath(), '/').'/'.$path;
$path = rtrim($uri->getPath(), '/') . '/' . $path;
}

return $uri->getScheme().'://'.$host.$path;
return $uri->getScheme() . '://' . $host.$path;
}

/**
* @param string $path
* @return string
*/
public function generateRelativeUrl($path)
{
if ($this->isNetworkPath($path)
|| ! $this->hasLeadingSlash($path)
) {
return $path;
}

$uri = $this->request->getUri();

$basePath = $uri->getPath();
if ($path === $basePath) {
return '';
}

$baseParts = explode('/', $basePath, -1);
$pathParts = explode('/', $path);

foreach ($baseParts as $i => $segment) {
if (isset($pathParts[$i]) && $segment === $pathParts[$i]) {
unset($baseParts[$i], $pathParts[$i]);
} else {
break;
}
}

$path = str_repeat('../', count($baseParts)) . implode('/', $pathParts);

if (empty($path)) {
return './';
}

if (empty($baseParts)
&& false !== strpos(current($pathParts), ':')
) {
$path = './' . $path;
}

return $path;
}

/**
Expand All @@ -55,7 +101,8 @@ public function generateAbsoluteUrl($path)
*/
private function isNetworkPath($path)
{
return false !== strpos($path, '://') || '//' === substr($path, 0, 2);
return false !== strpos($path, '://')
|| '//' === substr($path, 0, 2);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions tests/Extension/RequestExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testAddExtension()

$this->assertArrayHasKey('request', $twig->getExtensions());
$this->assertArrayHasKey('absolute_url', $twig->getFunctions());
$this->assertArrayHasKey('relative_url', $twig->getFunctions());
}

/**
Expand Down Expand Up @@ -59,4 +60,41 @@ public function getGenerateAbsoluteUrlData()
['//', 'http://localhost', '//']
];
}

/**
* @dataProvider getGenerateRelativeUrlData()
*/
public function testGenerateRelativeUrl($path, $url, $expected)
{
$uri = new Uri($url);
$request = new ServerRequest(
$server = [],
$files = [],
$uri = $uri,
$method = 'GET',
$body = 'php://input',
$headers = []
);

$extension = new RequestExtension($request);
$relativeUrl = $extension->generateRelativeUrl($path);

$this->assertEquals($expected, $relativeUrl);
}

public function getGenerateRelativeUrlData()
{
return [
['/a/b/c/foo.png', 'http://localhost/a/b/c/d', 'foo.png'],
['/a/b/foo.png', 'http://localhost/a/b/c/d', '../foo.png'],
['/a/b/c/d', 'http://localhost/a/b/c/d', ''],
['/a/b/c/', 'http://localhost/a/b/c/d', './'],
['/a/b/c/other', 'http://localhost/a/b/c/d', 'other'],
['/a/b/z/foo.png', 'http://localhost/a/b/c/d', '../z/foo.png'],
['/a/b/c/this:that', 'http://localhost/a/b/c/d', './this:that'],
['/a/b/c/foo/this:that', 'http://localhost/a/b/c/d', 'foo/this:that'],
['/', 'http://localhost', '/'],
['//', 'http://localhost', '//']
];
}
}

0 comments on commit 4714ba3

Please sign in to comment.