diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php index 286b7c62e4d2a..369e95a1f08aa 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 @@ public function locate($template, $currentPath = null, $first = true) $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 0000000000000..b97a6ba6a7c57 --- /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 1b28ec4f78deb..b00a14a06f004 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 @@ public function testLocateATemplate() $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');