From 5441e9bc9046a7fbcedb5f945daa126e90a03850 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 11 Jan 2017 15:50:59 +0100 Subject: [PATCH] [FrameworkBundle] Fix relative paths used as cache keys --- .../Templating/Loader/TemplateLocator.php | 9 +++++++-- .../FrameworkBundle/Tests/Fixtures/templates.php | 5 +++++ .../Tests/Templating/Loader/TemplateLocatorTest.php | 11 +++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/templates.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php index 286b7c62e4..369e95a1f0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php @@ -24,6 +24,8 @@ class TemplateLocator implements FileLocatorInterface protected $locator; protected $cache; + private $cacheHits = array(); + /** * Constructor. * @@ -71,12 +73,15 @@ class TemplateLocator implements FileLocatorInterface $key = $this->getCacheKey($template); + if (isset($this->cacheHits[$key])) { + return $this->cacheHits[$key]; + } if (isset($this->cache[$key])) { - return $this->cache[$key]; + return $this->cacheHits[$key] = realpath($this->cache[$key]) ?: $this->cache[$key]; } try { - return $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath); + return $this->cacheHits[$key] = $this->locator->locate($template->getPath(), $currentPath); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException(sprintf('Unable to find template "%s" : "%s".', $template, $e->getMessage()), 0, $e); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/templates.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/templates.php new file mode 100644 index 0000000000..b97a6ba6a7 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/templates.php @@ -0,0 +1,5 @@ + __DIR__.'/../Fixtures/Resources/views/this.is.a.template.format.engine', +); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php index 1b28ec4f78..b00a14a06f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php @@ -35,6 +35,17 @@ class TemplateLocatorTest extends TestCase $this->assertEquals('/path/to/template', $locator->locate($template)); } + public function testLocateATemplateFromCacheDir() + { + $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine'); + + $fileLocator = $this->getFileLocator(); + + $locator = new TemplateLocator($fileLocator, __DIR__.'/../../Fixtures'); + + $this->assertEquals(realpath(__DIR__.'/../../Fixtures/Resources/views/this.is.a.template.format.engine'), $locator->locate($template)); + } + public function testThrowsExceptionWhenTemplateNotFound() { $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');