diff --git a/lib/Twig/Loader/Filesystem.php b/lib/Twig/Loader/Filesystem.php index 0f5c4b8ab..2d2153edd 100644 --- a/lib/Twig/Loader/Filesystem.php +++ b/lib/Twig/Loader/Filesystem.php @@ -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; } } diff --git a/test/Twig/Tests/Loader/FilesystemTest.php b/test/Twig/Tests/Loader/FilesystemTest.php index 98284474e..b7e2b9a0f 100644 --- a/test/Twig/Tests/Loader/FilesystemTest.php +++ b/test/Twig/Tests/Loader/FilesystemTest.php @@ -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')); + } } diff --git a/test/Twig/Tests/Loader/Fixtures/phar/phar-sample.phar b/test/Twig/Tests/Loader/Fixtures/phar/phar-sample.phar new file mode 100644 index 000000000..092bbfae3 Binary files /dev/null and b/test/Twig/Tests/Loader/Fixtures/phar/phar-sample.phar differ