-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPhptalEngine.php
194 lines (156 loc) · 6.77 KB
/
PhptalEngine.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Neni\PhptalBundle\Phptal;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Neni\PhptalBundle\Phptal\PhptalPopulaterInterface;
use Neni\PhptalBundle\Phptal\PhptalResolverInterface;
use Neni\PhptalBundle\Phptal\Helper\PhptalGenericHelper;
class PhptalEngine implements EngineInterface
{
protected $container;
protected $resolver;
protected $populater;
protected $options;
public function __construct(ContainerInterface $container, PhptalResolverInterface $resolver, PhptalPopulaterInterface $populater, $options)
{
$this->container = $container;
$this->resolver = $resolver;
$this->populater = $populater;
$this->options = $options;
}
/**
* Renders a template.
* @param string $name A template name
* @param array $parameters An array of parameters to pass to the template
* the default options engine can be override by $parameters['_engine_'][option]
* @return string The evaluated template as a string
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
*/
public function render($name, array $parameters = array() )
{
$template = new \PHPTAL();
// engine options by parameters to relpace configuration
if(isset($parameters['_engine_'])&&(is_array($parameters['_engine_']))){
$options = $parameters['_engine_'];
}else{
$options = array();
}
// code cache destination
$tmpdir = (isset($options['cache_dir']))?$options['cache_dir']:$this->options['cache_dir'];
if(!is_dir($tmpdir)){mkdir($tmpdir);}
$template->setPhpCodeDestination($tmpdir);
// code cache durration
$template->setCacheLifetime( (isset($options['cache_dir']))?$options['cache_lifetime']:$this->options['cache_lifetime'] );
// encoding
$template->setEncoding( (isset($options['charset']))?$options['charset']:$this->options['charset'] );
// output mod
if(!isset($options['output_format'])){
$options['output_format'] = $this->options['output_mode'];
}
if($options['output_format']=='XHTML'){
$template->setOutputMode( \PHPTAL::XHTML );
}elseif($options['output_format']=='HTML5'){
$template->setOutputMode( \PHPTAL::HTML5 );
}elseif($options['output_format']=='XML'){
$template->setOutputMode( \PHPTAL::XML );
}else{
throw new \InvalidArgumentException('Unsupported output mode ' . $options['output_format']);
}
// force reparse (for debug prefilter)
$template->setForceReparse( (isset($options['force_reparse']))?$options['force_reparse']:$this->options['force_reparse'] );
// pre filters
$filtres = $this->options['pre_filters'];
foreach($filtres as $filtre){
$template->addPreFilter( new $filtre['class']($filtre['params']) );
}
// post filters
$filtres = $this->options['post_filters'];
if($filtres){
$template->setPostFilter(new PhptalPostFilters($filtres));
}
// set SourceResolver
if(!isset($options['resolver'])){
$template->addSourceResolver($this->resolver);
}else{
if($this->container->has($options['resolver'])){
$resolver = $this->container->get($options['resolver']);
$r = new \ReflectionClass( get_class($resolver) );
if (!$r->implementsInterface('Neni\\PhptalBundle\\Phptal\\PhptalResolverInterface')) {
throw new \InvalidArgumentException(sprintf('The service "%s" does implements PhptalResolverInterface.', $options['resolver']));
}else{
$template->addSourceResolver( $resolver );
}
}else{
throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $options['resolver']));
}
}
// set source template
$template->setTemplate($name);
// set data
if(!isset($options['populater'])){
unset($parameters['_engine_']);
$this->populater->populate($template, $parameters);
}else{
if($this->container->has($options['populater'])){
$populater = $this->container->get($options['populater']);
$r = new \ReflectionClass( get_class($populater) );
if (!$r->implementsInterface('Neni\\PhptalBundle\\Phptal\\PhptalPopulaterInterface')) {
throw new \InvalidArgumentException(sprintf('The service "%s" does implements PhptalPopulaterInterface.', $options['populater']));
}else{
unset($parameters['_engine_']);
$populater->populate($template, $parameters);
}
}else{
throw new \InvalidArgumentException(sprintf('The service "%s" does not exist.', $options['populater']));
}
}
// generic helper
$template->Helper = new PhptalGenericHelper($this->container, $parameters);
// perform
try{
$result = $template->execute();
}catch (PHPTAL_TemplateException $e){
throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
return $result;
}
/**
* Test if the template exists.
* @param string $name A template name
* @return Boolean true if the template exists, false otherwise
*/
public function exists($name)
{
return $this->resolver->exists($name);
}
/**
* Test if this class is able to render a template.
* @param string $name A template name
* @return boolean True if this class supports the given resource, false otherwise
*/
public function supports($name)
{
return $this->resolver->supports($name);
}
/**
* Renders a view and returns a Response.
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
* @return Response A Response instance
*/
public function renderResponse($view, array $parameters = array(), Response $response = null )
{
if (null === $response) {
$response = new Response();
}
$response->setContent($this->render($view, $parameters));
return $response;
}
}