Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b86bda3
commit ffcf09f
Showing
6 changed files
with
197 additions
and
215 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace AlexMasterov\TwigExtension; | ||
|
||
use Psr\Http\Message\UriInterface; | ||
|
||
final class AbsoluteUrlGenerator | ||
{ | ||
/** | ||
* @param UriInterface $uri | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke(UriInterface $uri) | ||
{ | ||
$host = $uri->getHost(); | ||
$path = $uri->getPath(); | ||
|
||
if (empty($host)) { | ||
return $path; | ||
} | ||
|
||
$port = $uri->getPort(); | ||
if (!empty($port)) { | ||
$host .= ":{$port}"; | ||
} | ||
|
||
$scheme = $uri->getScheme(); | ||
|
||
return "{$scheme}://{$host}{$path}"; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace AlexMasterov\TwigExtension; | ||
|
||
use Psr\Http\Message\UriInterface; | ||
|
||
final class RelativePathGenerator | ||
{ | ||
/** | ||
* @param UriInterface $uri | ||
* | ||
* @return string | ||
*/ | ||
public function __invoke(UriInterface $uri) | ||
{ | ||
$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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
<?php | ||
|
||
namespace AlexMasterov\TwigExtension\Tests; | ||
|
||
use AlexMasterov\TwigExtension\AbsoluteUrlGenerator; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
class AbsoluteUrlGeneratorTest extends TestCase | ||
{ | ||
public function testThenHostIsMissing() | ||
{ | ||
$output = '/'; | ||
|
||
$uri = $this->createMockUri('http', '', '/', ''); | ||
|
||
$generator = new AbsoluteUrlGenerator(); | ||
$url = $generator($uri); | ||
|
||
$this->assertEquals($output, $url); | ||
} | ||
|
||
public function testThenPortExists() | ||
{ | ||
$output = 'http://localhost:80'; | ||
|
||
$uri = $this->createMockUri('http', 'localhost', '', 80); | ||
|
||
$generator = new AbsoluteUrlGenerator(); | ||
$url = $generator($uri); | ||
|
||
$this->assertEquals($output, $url); | ||
} | ||
|
||
public function testGenerateAbsoluteUrl() | ||
{ | ||
$output = 'http://localhost:80/test'; | ||
|
||
$uri = $this->createMockUri('http', 'localhost', '/test', 80); | ||
|
||
$generator = new AbsoluteUrlGenerator(); | ||
$url = $generator($uri); | ||
|
||
$this->assertEquals($output, $url); | ||
} | ||
|
||
/** | ||
* @param array $partsUrl | ||
* | ||
* @return UriInterface | ||
*/ | ||
protected function createMockUri( | ||
$scheme = null, | ||
$host = null, | ||
$path = null, | ||
$port = null | ||
) { | ||
$uri = $this->createMock(UriInterface::class); | ||
$uri->expects($this->any())->method('getScheme')->willReturn($scheme); | ||
$uri->expects($this->any())->method('getHost')->willReturn($host); | ||
$uri->expects($this->any())->method('getPath')->willReturn($path); | ||
$uri->expects($this->any())->method('getPort')->willReturn($port); | ||
|
||
return $uri; | ||
} | ||
} |
Oops, something went wrong.