Selaa lähdekoodia

simplified TemplateNameParser::parse() return value

Fabien Potencier 14 vuotta sitten
vanhempi
commit
40a70cd6f4

+ 13 - 17
src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php

@@ -16,8 +16,8 @@ use Symfony\Component\HttpKernel\Kernel;
 
 /**
  * TemplateNameParser parsers template name from the short notation
- * "bundle:section:template.renderer.format" to a template name
- * and an array of options.
+ * "bundle:section:template.renderer.format" to an array of
+ * template parameters.
  *
  * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  */
@@ -36,11 +36,7 @@ class TemplateNameParser extends BaseTemplateNameParser
     }
 
     /**
-     * Parses a template to a template name and an array of options.
-     *
-     * @param string $name A template name
-     *
-     * @return array An array composed of the template name and an array of options
+     * {@inheritdoc}
      */
     public function parse($name)
     {
@@ -70,21 +66,21 @@ class TemplateNameParser extends BaseTemplateNameParser
             }
         }
 
-        $options = array(
-            // bundle is used as part of the template path, so we need /
-            'bundle'     => str_replace('\\', '/', $bundle),
-            'controller' => $parts[1],
-        );
-
         $elements = explode('.', $parts[2]);
         if (3 !== count($elements)) {
             throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.renderer.format").', $name));
         }
 
-        $parts[2] = $elements[0];
-        $options['renderer'] = $elements[1];
-        $options['format'] = $elements[2];
+        $parameters = array(
+            // bundle is used as part of the template path, so we need /
+            'bundle'     => str_replace('\\', '/', $bundle),
+            'controller' => $parts[1],
+            'name'       => $elements[0],
+            'renderer'   => $elements[1],
+            'format'     => $elements[2],
+        );
+
 
-        return array($parts[2], $options);
+        return $parameters;
     }
 }

+ 7 - 7
src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php

@@ -32,13 +32,13 @@ class TemplateNameParserTest extends TestCase
     public function getParseTests()
     {
         return array(
-            array('FooBundle:Post:index.php.html', array('index', array('bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html'))),
-            array('FooBundle:Post:index.twig.html', array('index', array('bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'twig', 'format' => 'html'))),
-            array('FooBundle:Post:index.php.xml', array('index', array('bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'xml'))),
-            array('SensioFooBundle:Post:index.php.html', array('index', array('bundle' => 'Sensio/FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html'))),
-            array('SensioCmsFooBundle:Post:index.php.html', array('index', array('bundle' => 'Sensio/Cms/FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html'))),
-            array(':Post:index.php.html', array('index', array('bundle' => '', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html'))),
-            array('::index.php.html', array('index', array('bundle' => '', 'controller' => '', 'renderer' => 'php', 'format' => 'html'))),
+            array('FooBundle:Post:index.php.html', array('name' => 'index', 'bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html')),
+            array('FooBundle:Post:index.twig.html', array('name' => 'index', 'bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'twig', 'format' => 'html')),
+            array('FooBundle:Post:index.php.xml', array('name' => 'index', 'bundle' => 'FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'xml')),
+            array('SensioFooBundle:Post:index.php.html', array('name' => 'index', 'bundle' => 'Sensio/FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html')),
+            array('SensioCmsFooBundle:Post:index.php.html', array('name' => 'index', 'bundle' => 'Sensio/Cms/FooBundle', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html')),
+            array(':Post:index.php.html',array('name' => 'index', 'bundle' => '', 'controller' => 'Post', 'renderer' => 'php', 'format' => 'html')),
+            array('::index.php.html', array('name' => 'index', 'bundle' => '', 'controller' => '', 'renderer' => 'php', 'format' => 'html')),
         );
     }
 

+ 6 - 8
src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php

@@ -59,17 +59,15 @@ class FilesystemLoader extends \Twig_Loader_Filesystem
             return $this->cache[$name];
         }
 
-        list($tpl, $options) = $this->nameParser->parse($name);
+        $parameters = $this->nameParser->parse($name);
 
-        $this->validateName($tpl);
-        $this->validateName($options['bundle']);
-        $this->validateName($options['controller']);
-        $this->validateName($options['format']);
-
-        $options['name'] = $tpl;
+        $this->validateName($parameters['name']);
+        $this->validateName($parameters['bundle']);
+        $this->validateName($parameters['controller']);
+        $this->validateName($parameters['format']);
 
         $replacements = array();
-        foreach ($options as $key => $value) {
+        foreach ($parameters as $key => $value) {
             $replacements['%'.$key.'%'] = $value;
         }
 

+ 5 - 5
src/Symfony/Component/Templating/Loader/CacheLoader.php

@@ -51,22 +51,22 @@ class CacheLoader extends Loader
      */
     public function load($template)
     {
-        list($template, $options) = $this->nameParser->parse($template);
+        $parameters = $this->nameParser->parse($template);
 
-        $tmp = md5($template.serialize($options)).'.tpl';
+        $tmp = md5(serialize($parameters)).'.tpl';
         $dir = $this->dir.DIRECTORY_SEPARATOR.substr($tmp, 0, 2);
         $file = substr($tmp, 2);
         $path = $dir.DIRECTORY_SEPARATOR.$file;
 
         if (file_exists($path)) {
             if (null !== $this->debugger) {
-                $this->debugger->log(sprintf('Fetching template "%s" from cache', $template));
+                $this->debugger->log(sprintf('Fetching template "%s" from cache', $parameters['name']));
             }
 
             return new FileStorage($path);
         }
 
-        if (false === $storage = $this->loader->load($template, $options)) {
+        if (false === $storage = $this->loader->load($parameters['name'], $parameters)) {
             return false;
         }
 
@@ -79,7 +79,7 @@ class CacheLoader extends Loader
         file_put_contents($path, $content);
 
         if (null !== $this->debugger) {
-            $this->debugger->log(sprintf('Storing template "%s" in cache', $template));
+            $this->debugger->log(sprintf('Storing template "%s" in cache', $parameters['name']));
         }
 
         return new FileStorage($path);

+ 4 - 7
src/Symfony/Component/Templating/Loader/FilesystemLoader.php

@@ -50,16 +50,14 @@ class FilesystemLoader extends Loader
      */
     public function load($template)
     {
-        list($template, $options) = $this->nameParser->parse($template);
+        $parameters = $this->nameParser->parse($template);
 
-        if (self::isAbsolutePath($template) && file_exists($template)) {
-            return new FileStorage($template);
+        if (self::isAbsolutePath($parameters['name']) && file_exists($parameters['name'])) {
+            return new FileStorage($parameters['name']);
         }
 
-        $options['name'] = $template;
-
         $replacements = array();
-        foreach ($options as $key => $value) {
+        foreach ($parameters as $key => $value) {
             $replacements['%'.$key.'%'] = $value;
         }
 
@@ -91,7 +89,6 @@ class FilesystemLoader extends Loader
      * Returns true if the template is still fresh.
      *
      * @param string    $template The template name
-     * @param array     $options  An array of options
      * @param timestamp $time     The last modification time of the cached template
      */
     public function isFresh($template, $time)

+ 5 - 3
src/Symfony/Component/Templating/TemplateNameParser.php

@@ -19,14 +19,16 @@ namespace Symfony\Component\Templating;
 class TemplateNameParser implements TemplateNameParserInterface
 {
     /**
-     * Parses a template to a template name and an array of options.
+     * Parses a template to an array of parameters.
+     *
+     * The only mandatory parameter is the template name (name).
      *
      * @param string $name A template name
      *
-     * @return array An array composed of the template name and an array of options
+     * @return array An array of parameters
      */
     public function parse($name)
     {
-        return array($name, array());
+        return array('name' => $name);
     }
 }

+ 4 - 2
src/Symfony/Component/Templating/TemplateNameParserInterface.php

@@ -19,11 +19,13 @@ namespace Symfony\Component\Templating;
 interface TemplateNameParserInterface
 {
     /**
-     * Parses a template to a template name and an array of options.
+     * Parses a template to an array of parameters.
+     *
+     * The only mandatory parameter is the template name (name).
      *
      * @param string $name A template name
      *
-     * @return array An array composed of the template name and an array of options
+     * @return array An array of parameters
      */
     function parse($name);
 }