/
FilesystemLoader.php
95 lines (81 loc) · 2.67 KB
/
FilesystemLoader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Loader;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;
use Twig\Error\LoaderError;
use Twig\Loader\FilesystemLoader as BaseFilesystemLoader;
/**
* FilesystemLoader extends the default Twig filesystem loader
* to work with the Symfony paths and template references.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FilesystemLoader extends BaseFilesystemLoader
{
protected $locator;
protected $parser;
public function __construct(FileLocatorInterface $locator, TemplateNameParserInterface $parser)
{
parent::__construct(array());
$this->locator = $locator;
$this->parser = $parser;
}
/**
* {@inheritdoc}
*
* The name parameter might also be a TemplateReferenceInterface.
*/
public function exists($name)
{
return parent::exists((string) $name);
}
/**
* Returns the path to the template file.
*
* The file locator is used to locate the template when the naming convention
* is the symfony one (i.e. the name can be parsed).
* Otherwise the template is located using the locator from the twig library.
*
* @param string|TemplateReferenceInterface $template The template
* @param bool $throw When true, a LoaderError exception will be thrown if a template could not be found
*
* @return string The path to the template file
*
* @throws LoaderError if the template could not be found
*/
protected function findTemplate($template, $throw = true)
{
$logicalName = (string) $template;
if (isset($this->cache[$logicalName])) {
return $this->cache[$logicalName];
}
$file = null;
try {
$file = parent::findTemplate($logicalName);
} catch (LoaderError $e) {
$twigLoaderException = $e;
// for BC
try {
$template = $this->parser->parse($template);
$file = $this->locator->locate($template);
} catch (\Exception $e) {
}
}
if (false === $file || null === $file) {
if ($throw) {
throw $twigLoaderException;
}
return false;
}
return $this->cache[$logicalName] = $file;
}
}