1
|
|
|
<?php |
2
|
|
|
namespace Agavi\Renderer; |
3
|
|
|
|
4
|
|
|
// +---------------------------------------------------------------------------+ |
5
|
|
|
// | This file is part of the Agavi package. | |
6
|
|
|
// | Copyright (c) 2005-2011 the Agavi Project. | |
7
|
|
|
// | | |
8
|
|
|
// | For the full copyright and license information, please view the LICENSE | |
9
|
|
|
// | file that was distributed with this source code. You can also view the | |
10
|
|
|
// | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
11
|
|
|
// | vi: set noexpandtab: | |
12
|
|
|
// | Local Variables: | |
13
|
|
|
// | indent-tabs-mode: t | |
14
|
|
|
// | End: | |
15
|
|
|
// +---------------------------------------------------------------------------+ |
16
|
|
|
use Agavi\Config\Config; |
17
|
|
|
use Agavi\Util\Toolkit; |
18
|
|
|
use Agavi\View\TemplateLayer; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Renderer for Smarty (versions 2 and 3). |
22
|
|
|
* |
23
|
|
|
* @package agavi |
24
|
|
|
* @subpackage renderer |
25
|
|
|
* |
26
|
|
|
* @author David Zülke <[email protected]> |
27
|
|
|
* @author TANAKA Koichi <[email protected]> |
28
|
|
|
* @copyright Authors |
29
|
|
|
* @copyright The Agavi Project |
30
|
|
|
* |
31
|
|
|
* @since 0.11.0 |
32
|
|
|
* |
33
|
|
|
* @version $Id$ |
34
|
|
|
*/ |
35
|
|
|
class SmartyRenderer extends Renderer implements ReusableRendererInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @constant string The directory inside the cache dir where templates will |
39
|
|
|
* be stored in compiled form. |
40
|
|
|
*/ |
41
|
|
|
const COMPILE_DIR = 'templates'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @constant string The subdirectory inside the compile dir where templates |
45
|
|
|
* will be stored in compiled form. |
46
|
|
|
*/ |
47
|
|
|
const COMPILE_SUBDIR = 'smarty'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @constant string The directory inside the cache dir where cached content |
51
|
|
|
* will be stored. |
52
|
|
|
*/ |
53
|
|
|
const CACHE_DIR = 'content'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var Smarty Smarty template engine. |
57
|
|
|
*/ |
58
|
|
|
protected $smarty = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string A string with the default template file extension, |
62
|
|
|
* including the dot. |
63
|
|
|
*/ |
64
|
|
|
protected $defaultExtension = '.tpl'; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var bool Internal flag to indicate the Smarty version used. |
68
|
|
|
*/ |
69
|
|
|
protected $isSmarty2 = true; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Pre-serialization callback. |
73
|
|
|
* |
74
|
|
|
* Excludes the Smarty instance to prevent excessive serialization load. |
75
|
|
|
* |
76
|
|
|
* @author David Zülke <[email protected]> |
77
|
|
|
* @since 0.11.0 |
78
|
|
|
*/ |
79
|
|
|
public function __sleep() |
80
|
|
|
{ |
81
|
|
|
$keys = parent::__sleep(); |
82
|
|
|
unset($keys[array_search('smarty', $keys)]); |
83
|
|
|
return $keys; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Create an instance of Smarty and initialize it correctly. |
88
|
|
|
* |
89
|
|
|
* @return Smarty The Smarty instance. |
90
|
|
|
* |
91
|
|
|
* @author David Zülke <[email protected]> |
92
|
|
|
* @since 1.0.2 |
93
|
|
|
*/ |
94
|
|
|
protected function createEngineInstance() |
95
|
|
|
{ |
96
|
|
|
if (!class_exists('Smarty')) { |
97
|
|
|
if (defined('SMARTY_DIR')) { |
98
|
|
|
// if SMARTY_DIR constant is defined, we'll use it |
99
|
|
|
require(SMARTY_DIR . 'Smarty.class.php'); |
100
|
|
|
} else { |
101
|
|
|
// otherwise we resort to include_path |
102
|
|
|
require('Smarty.class.php'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new Smarty(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Grab a cleaned up smarty instance. |
111
|
|
|
* |
112
|
|
|
* @return Smarty A Smarty instance. |
113
|
|
|
* |
114
|
|
|
* @author David Zülke <[email protected]> |
115
|
|
|
* @author TANAKA Koichi <[email protected]> |
116
|
|
|
* @since 0.9.0 |
117
|
|
|
*/ |
118
|
|
|
protected function getEngine() |
119
|
|
|
{ |
120
|
|
|
if ($this->smarty) { |
121
|
|
|
if ($this->isSmarty2) { |
122
|
|
|
$this->smarty->clear_all_assign(); |
123
|
|
|
$this->smarty->clear_config(); |
124
|
|
|
} |
125
|
|
|
return $this->smarty; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->smarty = $this->createEngineInstance(); |
129
|
|
|
|
130
|
|
|
$this->isSmarty2 = !defined("Smarty::SMARTY_VERSION") || !preg_match('#^Smarty.?3#', Smarty::SMARTY_VERSION, $matches); |
131
|
|
|
|
132
|
|
|
if ($this->isSmarty2) { |
133
|
|
|
$this->smarty->config_dir = Config::get('core.config_dir'); |
134
|
|
|
} else { |
135
|
|
|
$this->smarty->setConfigDir(Config::get('core.config_dir')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$parentMode = fileperms(Config::get('core.cache_dir')); |
139
|
|
|
|
140
|
|
|
$compileDir = Config::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::COMPILE_DIR . DIRECTORY_SEPARATOR . self::COMPILE_SUBDIR; |
141
|
|
|
Toolkit::mkdir($compileDir, $parentMode, true); |
142
|
|
|
if ($this->isSmarty2) { |
143
|
|
|
$this->smarty->compile_dir = $compileDir; |
144
|
|
|
} else { |
145
|
|
|
$this->smarty->setCompileDir($compileDir); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$cacheDir = Config::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::CACHE_DIR; |
149
|
|
|
Toolkit::mkdir($cacheDir, $parentMode, true); |
150
|
|
|
if ($this->isSmarty2) { |
151
|
|
|
$this->smarty->cache_dir = $cacheDir; |
152
|
|
|
} else { |
153
|
|
|
$this->smarty->setCacheDir($cacheDir); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (Config::get('core.debug', false)) { |
|
|
|
|
157
|
|
|
$this->smarty->debugging = true; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
foreach ((array)$this->getParameter('smarty_variables') as $key => $value) { |
161
|
|
|
$this->smarty->$key = $value; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this->smarty; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Render the presentation and return the result. |
169
|
|
|
* |
170
|
|
|
* @param TemplateLayer $layer The template layer to render. |
171
|
|
|
* @param array $attributes The template variables. |
172
|
|
|
* @param array $slots The slots. |
173
|
|
|
* @param array $moreAssigns Associative array of additional assigns. |
174
|
|
|
* |
175
|
|
|
* @return string A rendered result. |
176
|
|
|
* |
177
|
|
|
* @author David Zülke <[email protected]> |
178
|
|
|
* @since 0.11.0 |
179
|
|
|
*/ |
180
|
|
|
public function render(TemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array()) |
181
|
|
|
{ |
182
|
|
|
$engine = $this->getEngine(); |
183
|
|
|
if ($this->isSmarty2) { |
184
|
|
|
$assignFunc = 'assign_by_ref'; |
185
|
|
|
$data = $engine; |
186
|
|
|
} else { |
187
|
|
|
$assignFunc = 'assignByRef'; |
188
|
|
|
$data = $engine->createData($engine); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
View Code Duplication |
if ($this->extractVars) { |
|
|
|
|
192
|
|
|
foreach ($attributes as $name => &$value) { |
193
|
|
|
$data->$assignFunc($name, $value); |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
$data->$assignFunc($this->varName, $attributes); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$data->$assignFunc($this->slotsVarName, $slots); |
200
|
|
|
|
201
|
|
|
foreach ($this->assigns as $key => $getter) { |
202
|
|
|
$data->assign($key, $this->context->$getter()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$finalMoreAssigns =& self::buildMoreAssigns($moreAssigns, $this->moreAssignNames); |
206
|
|
|
foreach ($finalMoreAssigns as $key => &$value) { |
207
|
|
|
$data->$assignFunc($key, $value); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// hack because stupid smarty cannot handle php streams... my god |
211
|
|
|
$resource = str_replace('://', ':', $layer->getResourceStreamIdentifier()); |
212
|
|
|
return $engine->fetch($resource, $this->isSmarty2 ? null : $data); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: