normalizePath: Do not mangle phar URLs.

This commit is contained in:
Aljosha Papsch
2016-09-28 16:55:26 +02:00
committed by Fabien Potencier
parent 52e9e01353
commit 7dd63fc087
3 changed files with 15 additions and 1 deletions
+2 -1
View File
@@ -257,11 +257,12 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI
private function normalizePath($path)
{
$parts = explode('/', str_replace('\\', '/', $path));
$isPhar = strpos($path, 'phar://') === 0;
$new = array();
foreach ($parts as $i => $part) {
if ('..' === $part) {
array_pop($new);
} elseif ('.' !== $part && ('' !== $part || 0 === $i)) {
} elseif ('.' !== $part && ('' !== $part || 0 === $i || $isPhar && $i < 3)) {
$new[] = $part;
}
}
+13
View File
@@ -179,4 +179,17 @@ class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
$template = $twig->loadTemplate($templateName);
$this->assertSame('VALID Child', $template->renderBlock('body', array()));
}
/**
* @requires PHP 5.3
*/
public function testLoadTemplateFromPhar()
{
$loader = new Twig_Loader_Filesystem(array());
// phar-sample.phar was created with the following script:
// $f = new Phar('phar-test.phar');
// $f->addFromString('hello.twig', 'hello from phar');
$loader->addPath('phar://'.dirname(__FILE__).'/Fixtures/phar/phar-sample.phar');
$this->assertSame('hello from phar', $loader->getSource('hello.twig'));
}
}