forked from symfony/twig-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwigEngine.php
131 lines (114 loc) · 3.6 KB
/
TwigEngine.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Twig;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\StreamingEngineInterface;
use Symfony\Component\Templating\TemplateNameParserInterface;
use Symfony\Component\Templating\TemplateReferenceInterface;
/**
* This engine knows how to render Twig templates.
*
* @author Fabien Potencier <[email protected]>
*/
class TwigEngine implements EngineInterface, StreamingEngineInterface
{
protected $environment;
protected $parser;
/**
* Constructor.
*
* @param \Twig_Environment $environment A \Twig_Environment instance
* @param TemplateNameParserInterface $parser A TemplateNameParserInterface instance
*/
public function __construct(\Twig_Environment $environment, TemplateNameParserInterface $parser)
{
$this->environment = $environment;
$this->parser = $parser;
}
/**
* {@inheritdoc}
*
* It also supports \Twig_Template as name parameter.
*
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
*/
public function render($name, array $parameters = array())
{
return $this->load($name)->render($parameters);
}
/**
* {@inheritdoc}
*
* It also supports \Twig_Template as name parameter.
*
* @throws \Twig_Error if something went wrong like a thrown exception while rendering the template
*/
public function stream($name, array $parameters = array())
{
$this->load($name)->display($parameters);
}
/**
* {@inheritdoc}
*
* It also supports \Twig_Template as name parameter.
*/
public function exists($name)
{
if ($name instanceof \Twig_Template) {
return true;
}
$loader = $this->environment->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) {
return $loader->exists((string) $name);
}
try {
// cast possible TemplateReferenceInterface to string because the
// EngineInterface supports them but Twig_LoaderInterface does not
$loader->getSource((string) $name);
} catch (\Twig_Error_Loader $e) {
return false;
}
return true;
}
/**
* {@inheritdoc}
*
* It also supports \Twig_Template as name parameter.
*/
public function supports($name)
{
if ($name instanceof \Twig_Template) {
return true;
}
$template = $this->parser->parse($name);
return 'twig' === $template->get('engine');
}
/**
* Loads the given template.
*
* @param string|TemplateReferenceInterface|\Twig_Template $name A template name or an instance of
* TemplateReferenceInterface or \Twig_Template
*
* @return \Twig_TemplateInterface A \Twig_TemplateInterface instance
*
* @throws \InvalidArgumentException if the template does not exist
*/
protected function load($name)
{
if ($name instanceof \Twig_Template) {
return $name;
}
try {
return $this->environment->loadTemplate((string) $name);
} catch (\Twig_Error_Loader $e) {
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
}
}