Bläddra i källkod

[FrameworkBundle] Enforce templates instances of TemplateReferenceInterface

Victor Berchet 14 år sedan
förälder
incheckning
e80a693cfe

+ 9 - 5
src/Symfony/Bundle/FrameworkBundle/Templating/Loader/CachedTemplateLocator.php

@@ -49,13 +49,16 @@ class CachedTemplateLocator extends TemplateLocator
      *
      * @return string The full path for the file
      *
+     * @throws \InvalidArgumentException When the template is not an instance of TemplateReferenceInterface
      * @throws \InvalidArgumentException When file is not found
      */
     public function locate($template, $currentPath = null, $first = true)
     {
-        $key = $template->getSignature();
+        if (!$template instanceof TemplateReferenceInterface) {
+            throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface.");
+        }
 
-        $path = $this->getCachedTemplatePath($key);
+        $path = $this->getCachedTemplatePath($template);
 
         return $path === null ? parent::locate($template) : $path;
     }
@@ -63,12 +66,13 @@ class CachedTemplateLocator extends TemplateLocator
     /**
      * Returns the template path from the cache
      * 
-     * @param string $key The template signature
+     * @param TemplateReferenceInterface $template The template
      * 
-     * @return string|null The path when it is present in the call, false otherwise
+     * @return string|null The path when it is present in the cache, false otherwise
      */
-    protected function getCachedTemplatePath($key)
+    protected function getCachedTemplatePath(TemplateReferenceInterface $template)
     {
+        $key = $template->getSignature();
         return isset($this->templates[$key]) ? $this->templates[$key] : null;
     }
 }

+ 6 - 1
src/Symfony/Bundle/FrameworkBundle/Templating/Loader/TemplateLocator.php

@@ -47,10 +47,15 @@ class TemplateLocator implements FileLocatorInterface
      *
      * @return string The full path for the file
      *
-     * @throws \InvalidArgumentException When file is not found
+     * @throws \InvalidArgumentException When the template is not an instance of TemplateReferenceInterface
+     * @throws \InvalidArgumentException When the template file can not be found
      */
     public function locate($template, $currentPath = null, $first = true)
     {
+        if (!$template instanceof TemplateReferenceInterface) {
+            throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface.");
+        }
+
         $key = $template->getSignature();
 
         if (isset($this->cache[$key])) {

+ 17 - 2
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/CachedTemplateLocatorTest.php

@@ -21,7 +21,7 @@ class CachedTemplateLocatorTest extends TestCase
     public function testLocateACachedTemplate()
     {
         $template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
-        
+
         $locator = $this
             ->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\Loader\CachedTemplateLocator')
             ->setMethods(array('getCachedTemplatePath'))
@@ -32,10 +32,25 @@ class CachedTemplateLocatorTest extends TestCase
         $locator
             ->expects($this->once())
             ->method('getCachedTemplatePath')
-            ->with($template->getSignature())
+            ->with($template)
             ->will($this->returnValue('/cached/path/to/template'))
         ;
 
         $this->assertEquals('/cached/path/to/template', $locator->locate($template));
     }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testThrowsAnExceptionWhenTemplateIsNotATemplateReferenceInterface()
+    {
+        $locator = $this
+            ->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\Loader\CachedTemplateLocator')
+            ->setMethods(array('getCacheTemplatePath'))
+            ->disableOriginalConstructor()
+            ->getMock()
+        ;
+
+        $locator->locate('template');
+    }
 }

+ 9 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Loader/TemplateLocatorTest.php

@@ -55,6 +55,15 @@ class TemplateLocatorTest extends TestCase
         $locator->locate($template);
     }
 
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testThrowsAnExceptionWhenTemplateIsNotATemplateReferenceInterface()
+    {
+        $locator = new TemplateLocator($this->getFileLocator(), '/path/to/fallback');
+        $locator->locate('template');
+    }
+
     protected function getFileLocator()
     {
         return $this