Skip to content

Commit 055d852

Browse files
committed
refactored template name parser to occur independently of the loaders
1 parent ba2d312 commit 055d852

9 files changed

+72
-73
lines changed

EngineInterface.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,26 @@
1414
/**
1515
* EngineInterface is the interface each engine must implement.
1616
*
17+
* All methods relies on a template name. A template name is a
18+
* "logical" name for the template (an array), and as such it does not
19+
* refers to a path on the filesystem (in fact, the template can be
20+
* stored anywhere, like in a database).
21+
*
22+
* The methods should accept any name and if it is not an array, it should
23+
* then use a TemplateNameParserInterface to convert the name to an array.
24+
*
25+
* Each template loader use the logical template name to look for
26+
* the template.
27+
*
1728
* @author Fabien Potencier <[email protected]>
1829
*/
1930
interface EngineInterface
2031
{
2132
/**
2233
* Renders a template.
2334
*
24-
* @param string $name A template name
25-
* @param array $parameters An array of parameters to pass to the template
35+
* @param mixed $name A template name
36+
* @param array $parameters An array of parameters to pass to the template
2637
*
2738
* @return string The evaluated template as a string
2839
*

Loader/CacheLoader.php

+6-10
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,33 @@ class CacheLoader extends Loader
3636
*/
3737
public function __construct(Loader $loader, $dir)
3838
{
39-
parent::__construct($loader->getTemplateNameParser());
40-
4139
$this->loader = $loader;
4240
$this->dir = $dir;
4341
}
4442

4543
/**
4644
* Loads a template.
4745
*
48-
* @param string $template The logical template name
46+
* @param array $template The template name as an array
4947
*
5048
* @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
5149
*/
5250
public function load($template)
5351
{
54-
$parameters = $this->nameParser->parse($template);
55-
56-
$tmp = md5(serialize($parameters)).'.tpl';
52+
$tmp = md5(serialize($template)).'.tpl';
5753
$dir = $this->dir.DIRECTORY_SEPARATOR.substr($tmp, 0, 2);
5854
$file = substr($tmp, 2);
5955
$path = $dir.DIRECTORY_SEPARATOR.$file;
6056

6157
if (file_exists($path)) {
6258
if (null !== $this->debugger) {
63-
$this->debugger->log(sprintf('Fetching template "%s" from cache', $parameters['name']));
59+
$this->debugger->log(sprintf('Fetching template "%s" from cache', $template['name']));
6460
}
6561

6662
return new FileStorage($path);
6763
}
6864

69-
if (false === $storage = $this->loader->load($parameters['name'], $parameters)) {
65+
if (false === $storage = $this->loader->load($template)) {
7066
return false;
7167
}
7268

@@ -79,7 +75,7 @@ public function load($template)
7975
file_put_contents($path, $content);
8076

8177
if (null !== $this->debugger) {
82-
$this->debugger->log(sprintf('Storing template "%s" in cache', $parameters['name']));
78+
$this->debugger->log(sprintf('Storing template "%s" in cache', $template['name']));
8379
}
8480

8581
return new FileStorage($path);
@@ -88,7 +84,7 @@ public function load($template)
8884
/**
8985
* Returns true if the template is still fresh.
9086
*
91-
* @param string $template The template name
87+
* @param array $template The template name as an array
9288
* @param timestamp $time The last modification time of the cached template
9389
*/
9490
public function isFresh($template, $time)

Loader/ChainLoader.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function addLoader(Loader $loader)
4848
/**
4949
* Loads a template.
5050
*
51-
* @param string $template The logical template name
51+
* @param array $template The template name as an array
5252
*
5353
* @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
5454
*/
@@ -66,7 +66,7 @@ public function load($template)
6666
/**
6767
* Returns true if the template is still fresh.
6868
*
69-
* @param string $template The template name
69+
* @param array $template The template name as an array
7070
* @param timestamp $time The last modification time of the cached template
7171
*/
7272
public function isFresh($template, $time)

Loader/FilesystemLoader.php

+7-12
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ class FilesystemLoader extends Loader
2626
/**
2727
* Constructor.
2828
*
29-
* @param TemplateNameParserInterface $nameParser A TemplateNameParserInterface instance
30-
* @param array $templatePathPatterns An array of path patterns to look for templates
29+
* @param array $templatePathPatterns An array of path patterns to look for templates
3130
*/
32-
public function __construct(TemplateNameParserInterface $nameParser, $templatePathPatterns)
31+
public function __construct($templatePathPatterns)
3332
{
34-
parent::__construct($nameParser);
35-
3633
if (!is_array($templatePathPatterns)) {
3734
$templatePathPatterns = array($templatePathPatterns);
3835
}
@@ -43,20 +40,18 @@ public function __construct(TemplateNameParserInterface $nameParser, $templatePa
4340
/**
4441
* Loads a template.
4542
*
46-
* @param string $template The logical template name
43+
* @param array $template The template name as an array
4744
*
4845
* @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
4946
*/
5047
public function load($template)
5148
{
52-
$parameters = $this->nameParser->parse($template);
53-
54-
if (self::isAbsolutePath($parameters['name']) && file_exists($parameters['name'])) {
55-
return new FileStorage($parameters['name']);
49+
if (self::isAbsolutePath($template['name']) && file_exists($template['name'])) {
50+
return new FileStorage($template['name']);
5651
}
5752

5853
$replacements = array();
59-
foreach ($parameters as $key => $value) {
54+
foreach ($template as $key => $value) {
6055
$replacements['%'.$key.'%'] = $value;
6156
}
6257

@@ -87,7 +82,7 @@ public function load($template)
8782
/**
8883
* Returns true if the template is still fresh.
8984
*
90-
* @param string $template The template name
85+
* @param array $template The template name as an array
9186
* @param timestamp $time The last modification time of the cached template
9287
*/
9388
public function isFresh($template, $time)

Loader/Loader.php

-21
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,6 @@
2121
abstract class Loader implements LoaderInterface
2222
{
2323
protected $debugger;
24-
protected $nameParser;
25-
26-
/**
27-
* Constructor.
28-
*
29-
* @param TemplateNameParserInterface $nameParser A TemplateNameParserInterface instance
30-
*/
31-
public function __construct(TemplateNameParserInterface $nameParser)
32-
{
33-
$this->nameParser = $nameParser;
34-
}
35-
36-
/**
37-
* Gets the template name parser.
38-
*
39-
* @return TemplateNameParserInterface A TemplateNameParserInterface instance
40-
*/
41-
public function getTemplateNameParser()
42-
{
43-
return $this->nameParser;
44-
}
4524

4625
/**
4726
* Sets the debugger to use for this loader.

Loader/LoaderInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface LoaderInterface
2121
/**
2222
* Loads a template.
2323
*
24-
* @param string $template The logical template name
24+
* @param array $template The template name as an array
2525
*
2626
* @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
2727
*/
@@ -30,7 +30,7 @@ function load($template);
3030
/**
3131
* Returns true if the template is still fresh.
3232
*
33-
* @param string $template The template name
33+
* @param array $template The template name as an array
3434
* @param timestamp $time The last modification time of the cached template
3535
*/
3636
function isFresh($template, $time);

PhpEngine.php

+21-15
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ class PhpEngine implements EngineInterface, \ArrayAccess
3333
protected $cache;
3434
protected $escapers;
3535
protected $globals;
36+
protected $parser;
3637

3738
/**
3839
* Constructor.
3940
*
40-
* @param LoaderInterface $loader A loader instance
41-
* @param array $helpers An array of helper instances
41+
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
42+
* @param LoaderInterface $loader A loader instance
43+
* @param array $helpers An array of helper instances
4244
*/
43-
public function __construct(LoaderInterface $loader, array $helpers = array())
45+
public function __construct(TemplateNameParserInterface $parser, LoaderInterface $loader, array $helpers = array())
4446
{
47+
$this->parser = $parser;
4548
$this->loader = $loader;
4649
$this->parents = array();
4750
$this->stack = array();
@@ -60,8 +63,8 @@ public function __construct(LoaderInterface $loader, array $helpers = array())
6063
/**
6164
* Renders a template.
6265
*
63-
* @param string $name A template name
64-
* @param array $parameters An array of parameters to pass to the template
66+
* @param mixed $name A template name
67+
* @param array $parameters An array of parameters to pass to the template
6568
*
6669
* @return string The evaluated template as a string
6770
*
@@ -100,7 +103,7 @@ public function render($name, array $parameters = array())
100103
/**
101104
* Returns true if the template exists.
102105
*
103-
* @param string $name A template name
106+
* @param mixed $name A template name
104107
*
105108
* @return Boolean true if the template exists, false otherwise
106109
*/
@@ -118,40 +121,43 @@ public function exists($name)
118121
/**
119122
* Loads the given template.
120123
*
121-
* @param string $name A template name
124+
* @param mixed $name A template name
122125
*
123126
* @return Storage A Storage instance
124127
*
125128
* @throws \InvalidArgumentException if the template cannot be found
126129
*/
127130
public function load($name)
128131
{
129-
if (isset($this->cache[$name])) {
130-
return $this->cache[$name];
132+
$template = $this->parser->parse($name);
133+
134+
$key = md5(serialize($template));
135+
if (isset($this->cache[$key])) {
136+
return $this->cache[$key];
131137
}
132138

133139
// load
134-
$template = $this->loader->load($name);
140+
$template = $this->loader->load($template);
135141

136142
if (false === $template) {
137143
throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $name));
138144
}
139145

140-
$this->cache[$name] = $template;
141-
142-
return $template;
146+
return $this->cache[$key] = $template;
143147
}
144148

145149
/**
146150
* Returns true if this class is able to render the given template.
147151
*
148-
* @param string $name A template name
152+
* @param mixed $name A template name
149153
*
150154
* @return Boolean True if this class supports the given resource, false otherwise
151155
*/
152156
public function supports($name)
153157
{
154-
return false !== strpos($name, '.php');
158+
$template = $this->parser->parse($name);
159+
160+
return 'php' === $template['engine'];
155161
}
156162

157163
/**

Loader/TemplateNameParser.php TemplateNameParser.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,36 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Templating\Loader;
12+
namespace Symfony\Component\Templating;
1313

1414
/**
1515
* TemplateNameParser is the default implementation of TemplateNameParserInterface.
1616
*
17+
* This implementation takes everything as the template name
18+
* and the extension for the engine.
19+
*
1720
* @author Fabien Potencier <[email protected]>
1821
*/
1922
class TemplateNameParser implements TemplateNameParserInterface
2023
{
2124
/**
2225
* Parses a template to an array of parameters.
2326
*
24-
* The only mandatory parameter is the template name (name).
25-
*
2627
* @param string $name A template name
2728
*
2829
* @return array An array of parameters
2930
*/
3031
public function parse($name)
3132
{
32-
return array('name' => $name);
33+
if (is_array($name)) {
34+
return $name;
35+
}
36+
37+
$engine = null;
38+
if (false !== $pos = strrpos($name, '.')) {
39+
$engine = substr($name, $pos + 1);
40+
}
41+
42+
return array('name' => $name, 'engine' => $engine);
3343
}
3444
}

Loader/TemplateNameParserInterface.php TemplateNameParserInterface.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Templating\Loader;
12+
namespace Symfony\Component\Templating;
1313

1414
/**
15-
* TemplateNameParserInterface parses template name to a template name and an array of options.
15+
* TemplateNameParserInterface parses template names to a
16+
* "normalized" array of template parameters.
17+
*
18+
* The template name array must always have at least a "name"
19+
* and an "engine" key.
1620
*
1721
* @author Fabien Potencier <[email protected]>
1822
*/
@@ -21,11 +25,9 @@ interface TemplateNameParserInterface
2125
/**
2226
* Parses a template to an array of parameters.
2327
*
24-
* The only mandatory parameter is the template name (name).
25-
*
2628
* @param string $name A template name
2729
*
28-
* @return array An array of parameters
30+
* @return array An array of template parameters
2931
*/
3032
function parse($name);
3133
}

0 commit comments

Comments
 (0)