3 class Template_Syntax_Exception
extends Exception
9 const CONTEXT_ARGUMENT
= 10;
10 const CONTEXT_STRING
= 11;
12 public $left_delimiter = '{';
13 public $right_delimiter = '}';
15 protected $_allowed_in_variable = array(T_VARIABLE
, T_OBJECT_OPERATOR
, T_STRING
,
16 T_CONSTANT_ENCAPSED_STRING
, T_ENCAPSED_AND_WHITESPACE
, T_DOUBLE_COLON
, '(', ')');
18 protected $_allowed_in_string = array(T_STRING
,
19 T_CONSTANT_ENCAPSED_STRING
, T_ENCAPSED_AND_WHITESPACE
);
21 public $reserved_var_name = '(?:smarty|tpl|templatelite)';
23 public function __construct()
27 protected function processVariable($variable, $modifiers = array())
29 return '--var('.$variable.')';
33 public function parse($content)
37 $pos = strpos($content, $this->left_delimiter);
39 while ($pos !== false)
41 if (($end = strpos($content,
45 $tokens = token_get_all('<?php '.$content.' ?>');
47 foreach ($tokens as $token)
50 list($token, $text, $line) = $token;
58 echo token_name($token) . ": ".$text;
67 public function parseArguments($string)
70 $status = 0; // 0 = nothing, 1 = arg named, waiting for equal sign, 2 = waiting for content
74 foreach (token_get_all('<?php '.$string.'?>') as $t)
97 throw new Template_Syntax_Exception("Expecting '=' sign after argument name");
104 throw new Template_Syntax_Exception("Unexpected '=' sign");
108 case T_ENCAPSED_AND_WHITESPACE
:
110 case T_OBJECT_OPERATOR
:
111 case T_CONSTANT_ENCAPSED_STRING
:
115 throw new Template_Syntax_Exception("Expecting '=' sign after argument name");
118 if ($t[0] == T_CONSTANT_ENCAPSED_STRING
)
119 $arg_value = substr($t[1], 1, -1);
127 $args[$arg_name] = $arg_value;
128 $arg_name = $arg_value = null;
135 throw new Template_Syntax_Exception("Expecting '=' sign after argument name");
145 if ($arg_value != null && $arg_name != null && !array_key_exists($arg_name, $args))
147 $args[$arg_name] = $arg_value;
153 public function parseArgumentContent($content)
155 $content = trim($content);
160 $quotes = $content[0] . substr($content, -1);
162 if ($quotes == "\"\"" ||
$quotes == '\'\'')
164 $content = substr($content, 1, -1);
165 $inline = ($quotes == "\'\'") ?
false : true;
167 $current_var = false;
169 foreach (token_get_all('<?php "'.$content.'" ?>') as $t)
171 if ($current_var === true)
173 if ($t[0] == T_VARIABLE ||
($t[0] == T_STRING
&& $t[1][0] == '$'))
175 $current_var = $t[1];
180 $current_var = false;
186 list($variable, $modifiers) = $this->parseVariable($current_var);
187 $out .= $this->processVariable($variable, $modifiers, self
::CONTEXT_STRING
);
188 $current_var = false;
190 elseif ($inline && $t[0] == T_VARIABLE
)
192 $current_var = $t[1];
194 elseif ($t[0] == '`')
199 if ($current_var === false && in_array($t[0], $this->_allowed_in_string
))
201 $out .= isset($t[1]) ?
$t[1] : $t[0];
205 echo (isset($t[1]) ?
token_name($t[0]) . ' ' . htmlspecialchars($t[1]) : htmlspecialchars($t[0])). "<br />";
213 list($variable, $modifiers) = $this->parseVariable($content);
214 return $this->processVariable($variable, $modifiers, self
::CONTEXT_ARGUMENT
);
218 public function parseVariable($string)
221 $modifiers = array();
222 $current_modifier = false;
223 $current_arg = false;
225 foreach (token_get_all('<?php '.$string.'?>') as $t)
227 if ($t[0] == T_STRING
)
229 if ($current_modifier === true)
231 $modifiers[] = array($t[1]);
232 $current_modifier = count($modifiers) - 1;
238 $content = isset($t[1]) ?
$t[1] : $t[0];
240 if ($t[0] == T_CONSTANT_ENCAPSED_STRING
)
241 $content = substr($content, 1, -1);
244 $current_modifier = true;
245 elseif ($t[0] == ':')
247 elseif ($current_modifier === false && in_array($t[0], $this->_allowed_in_variable
))
249 $variable .= $content;
251 elseif (is_int($current_modifier) && in_array($t[0], $this->_allowed_in_variable
))
253 if (!array_key_exists($current_arg, $modifiers[$current_modifier]))
255 $modifiers[$current_modifier][$current_arg] = '';
258 $modifiers[$current_modifier][$current_arg] .= $content;
265 return array($variable, $modifiers);
268 public function parseTokens($string)
271 foreach (token_get_all('<?php '.$string.'?>') as $t)
276 echo '<th>'.token_name($t[0]).'</th>';
277 echo '<td>'.htmlspecialchars($t[1]).'</td>';
278 echo '<td>'.($t[2]).'</td>';
283 echo '<td>'.htmlspecialchars($t).'</td>';