|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\FrameworkBundle\Templating; |
|
13 |
|
14 use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser; |
|
15 use Symfony\Component\Templating\TemplateReferenceInterface; |
|
16 use Symfony\Component\HttpKernel\KernelInterface; |
|
17 |
|
18 /** |
|
19 * TemplateNameParser converts template names from the short notation |
|
20 * "bundle:section:template.format.engine" to TemplateReferenceInterface |
|
21 * instances. |
|
22 * |
|
23 * @author Fabien Potencier <fabien@symfony.com> |
|
24 */ |
|
25 class TemplateNameParser extends BaseTemplateNameParser |
|
26 { |
|
27 protected $kernel; |
|
28 protected $cache; |
|
29 |
|
30 /** |
|
31 * Constructor. |
|
32 * |
|
33 * @param KernelInterface $kernel A KernelInterface instance |
|
34 */ |
|
35 public function __construct(KernelInterface $kernel) |
|
36 { |
|
37 $this->kernel = $kernel; |
|
38 $this->cache = array(); |
|
39 } |
|
40 |
|
41 /** |
|
42 * {@inheritdoc} |
|
43 */ |
|
44 public function parse($name) |
|
45 { |
|
46 if ($name instanceof TemplateReferenceInterface) { |
|
47 return $name; |
|
48 } else if (isset($this->cache[$name])) { |
|
49 return $this->cache[$name]; |
|
50 } |
|
51 |
|
52 // normalize name |
|
53 $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'))); |
|
54 |
|
55 if (false !== strpos($name, '..')) { |
|
56 throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name)); |
|
57 } |
|
58 |
|
59 $parts = explode(':', $name); |
|
60 if (3 !== count($parts)) { |
|
61 throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); |
|
62 } |
|
63 |
|
64 $elements = explode('.', $parts[2]); |
|
65 if (3 > count($elements)) { |
|
66 throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.format.engine").', $name)); |
|
67 } |
|
68 $engine = array_pop($elements); |
|
69 $format = array_pop($elements); |
|
70 |
|
71 $template = new TemplateReference($parts[0], $parts[1], implode('.', $elements), $format, $engine); |
|
72 |
|
73 if ($template->get('bundle')) { |
|
74 try { |
|
75 $this->kernel->getBundle($template->get('bundle')); |
|
76 } catch (\Exception $e) { |
|
77 throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e); |
|
78 } |
|
79 } |
|
80 |
|
81 return $this->cache[$name] = $template; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Convert a filename to a template. |
|
86 * |
|
87 * @param string $file The filename |
|
88 * |
|
89 * @return TemplateReferenceInterface A template |
|
90 */ |
|
91 public function parseFromFilename($file) |
|
92 { |
|
93 $parts = explode('/', strtr($file, '\\', '/')); |
|
94 |
|
95 $elements = explode('.', array_pop($parts)); |
|
96 if (3 > count($elements)) { |
|
97 return false; |
|
98 } |
|
99 $engine = array_pop($elements); |
|
100 $format = array_pop($elements); |
|
101 |
|
102 return new TemplateReference('', implode('/', $parts), implode('.', $elements), $format, $engine); |
|
103 } |
|
104 |
|
105 } |