| 1 | <?php |
| 2 | /** |
| 3 | * @package php-mustache |
| 4 | * @subpackage shared |
| 5 | * @author Ingmar Runge 2011 - https://github.com/KiNgMaR - BSD license |
| 6 | **/ |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Mustache whitespace handling: Don't spend extra CPU cycles on trying to be 100% conforming to the specs. This is the default mode. |
| 11 | **/ |
| 12 | define('MUSTACHE_WHITESPACE_LAZY', 1); |
| 13 | /** |
| 14 | * Mustache whitespace handling: Try to be 100% conforming to the specs. |
| 15 | **/ |
| 16 | define('MUSTACHE_WHITESPACE_STRICT', 2); |
| 17 | /** |
| 18 | * Mustache whitespace handling: Compact output, compact all superflous whitespace. |
| 19 | **/ |
| 20 | define('MUSTACHE_WHITESPACE_STRIP', 4); |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Very simple, but hopefully effective tokenizer for Mustache templates. |
| 25 | * @package php-mustache |
| 26 | * @subpackage shared |
| 27 | **/ |
| 28 | class MustacheTokenizer |
| 29 | { |
| 30 | /** |
| 31 | * Default opening delimiter. |
| 32 | **/ |
| 33 | const DEFAULT_DELIMITER_OPEN = '{{'; |
| 34 | /** |
| 35 | * Default closing delimiter. |
| 36 | **/ |
| 37 | const DEFAULT_DELIMITER_CLOSE = '}}'; |
| 38 | |
| 39 | /** |
| 40 | * List of special characters that denote a section. |
| 41 | **/ |
| 42 | const SECTION_TYPES = '^#'; |
| 43 | /** |
| 44 | * List of characters that denote the end of a section. |
| 45 | **/ |
| 46 | const CLOSING_SECTION_TYPES = '/'; |
| 47 | /** |
| 48 | * List of prefix characters that are specific to tags. |
| 49 | * The difference between tags and sections is that tags can not contain |
| 50 | * other tags and do not have a closing counter-part. |
| 51 | **/ |
| 52 | const TAG_TYPES = '!>&'; |
| 53 | /** |
| 54 | * Defines the tag type that denotes a comment. |
| 55 | **/ |
| 56 | const COMMENT_TYPE = '!'; |
| 57 | /** |
| 58 | * Defines the tag type that denotes a partial. |
| 59 | **/ |
| 60 | const PARTIAL_TYPE = '>'; |
| 61 | |
| 62 | /** |
| 63 | * Constant that denotes a literal token. |
| 64 | **/ |
| 65 | const TKN_LITERAL = 'LITERAL'; |
| 66 | /** |
| 67 | * Constant that denotes a section start token. |
| 68 | **/ |
| 69 | const TKN_SECTION_START = 'SECTION_START'; |
| 70 | /** |
| 71 | * Constant that denotes a section end token. |
| 72 | **/ |
| 73 | const TKN_SECTION_END = 'SECTION_END'; |
| 74 | /** |
| 75 | * Constant that denotes a tag token. |
| 76 | **/ |
| 77 | const TKN_TAG = 'TAG'; |
| 78 | /** |
| 79 | * Constant that denotes a comment tag token. |
| 80 | **/ |
| 81 | const TKN_COMMENT = 'COMMENT'; |
| 82 | /** |
| 83 | * Constant that denotes a partial tag token. |
| 84 | **/ |
| 85 | const TKN_PARTIAL = 'PARTIAL'; |
| 86 | /** |
| 87 | * Constant that denotes a tag token with escaping disabled. |
| 88 | **/ |
| 89 | const TKN_TAG_NOESCAPE = 'TAG_NOESCAPE'; |
| 90 | |
| 91 | /** |
| 92 | * Template string. |
| 93 | * @var string |
| 94 | **/ |
| 95 | protected $template = ''; |
| 96 | |
| 97 | /** |
| 98 | * List of extracted tokens. |
| 99 | * Example entry: array('t' => one of the TKN_ consts[, 'm' => modifier character from _TYPES], 'd' => data/contents) |
| 100 | * @var array |
| 101 | **/ |
| 102 | protected $tokens = array(); |
| 103 | /** |
| 104 | * @var int |
| 105 | **/ |
| 106 | protected $whitespace_mode; |
| 107 | |
| 108 | /** |
| 109 | * @param string $template |
| 110 | **/ |
| 111 | public function __construct($template, $whitespace_mode = MUSTACHE_WHITESPACE_LAZY) |
| 112 | { |
| 113 | if(is_string($template)) |
| 114 | { |
| 115 | $this->template = $template; |
| 116 | $this->whitespace_mode = $whitespace_mode; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * This tokenizer basically ignores invalid syntax (thereby keeping it in the template output as literals). |
| 122 | * @return boolean |
| 123 | **/ |
| 124 | public function tokenize() |
| 125 | { |
| 126 | $dlm_o = self::DEFAULT_DELIMITER_OPEN; |
| 127 | $dlm_c = self::DEFAULT_DELIMITER_CLOSE; |
| 128 | |
| 129 | // radically compact whitespace in the template: |
| 130 | if($this->whitespace_mode == MUSTACHE_WHITESPACE_STRIP) |
| 131 | { |
| 132 | $this->template = preg_replace('~\s+~', ' ', $this->template); |
| 133 | } |
| 134 | |
| 135 | // start tokenizing: |
| 136 | $pos = strpos($this->template, $dlm_o); |
| 137 | $prev_pos = 0; |
| 138 | $line = 0; |
| 139 | |
| 140 | while($pos !== false) |
| 141 | { |
| 142 | $end_pos = strpos($this->template, $dlm_c, $pos + strlen($dlm_o)); |
| 143 | |
| 144 | if($end_pos === false) |
| 145 | { |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if($pos > $prev_pos) |
| 150 | { |
| 151 | $this->tokens[] = array('t' => self::TKN_LITERAL, 'd' => substr($this->template, $prev_pos, $pos - $prev_pos)); |
| 152 | } |
| 153 | |
| 154 | $new_token = NULL; |
| 155 | $skip = false; |
| 156 | $advance_extra = 0; |
| 157 | |
| 158 | $tag_contents = substr($this->template, $pos + strlen($dlm_o), $end_pos - $pos - strlen($dlm_o)); |
| 159 | |
| 160 | // save this in case the modifiers changes: |
| 161 | $dlm_c_len = strlen($dlm_c); |
| 162 | |
| 163 | if(empty($tag_contents)) |
| 164 | { |
| 165 | $skip = true; |
| 166 | } |
| 167 | elseif(strpos(self::SECTION_TYPES, $tag_contents[0]) !== false) |
| 168 | { |
| 169 | // t for token, m for modifier, d for data |
| 170 | $new_token = array('t' => self::TKN_SECTION_START, 'm' => $tag_contents[0], 'd' => trim(substr($tag_contents, 1))); |
| 171 | } |
| 172 | elseif(strpos(self::CLOSING_SECTION_TYPES, $tag_contents[0]) !== false) |
| 173 | { |
| 174 | $new_token = array('t' => self::TKN_SECTION_END, 'd' => trim(substr($tag_contents, 1))); |
| 175 | } |
| 176 | elseif(preg_match('~^=\s*(\S+)\s+(\S+)\s*=$~', $tag_contents, $match)) |
| 177 | { |
| 178 | // delimiter change! |
| 179 | $dlm_o = $match[1]; |
| 180 | $dlm_c = $match[2]; |
| 181 | } |
| 182 | elseif($tag_contents[0] === self::COMMENT_TYPE) |
| 183 | { |
| 184 | $new_token = array('t' => self::TKN_COMMENT, 'd' => trim(substr($tag_contents, 1))); |
| 185 | } |
| 186 | elseif($tag_contents[0] === self::PARTIAL_TYPE) |
| 187 | { |
| 188 | $new_token = array('t' => self::TKN_PARTIAL, 'd' => trim(substr($tag_contents, 1))); |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | $t = self::TKN_TAG; |
| 193 | |
| 194 | // support {{{ / }}} for not-to-be-escaped tags |
| 195 | if($dlm_o == self::DEFAULT_DELIMITER_OPEN && $tag_contents[0] == substr(self::DEFAULT_DELIMITER_OPEN, -1)) |
| 196 | { |
| 197 | if(substr($this->template, $end_pos, $dlm_c_len + 1) == $dlm_c . substr(self::DEFAULT_DELIMITER_CLOSE, -1)) |
| 198 | { |
| 199 | $tag_contents = substr($tag_contents, 1); |
| 200 | $t = self::TKN_TAG_NOESCAPE; |
| 201 | $advance_extra += 1; // get rid of extra } from closing delimiter |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if(empty($tag_contents)) // re-check, may have changed |
| 206 | { |
| 207 | $skip = true; |
| 208 | } |
| 209 | elseif(strpos(self::TAG_TYPES, $tag_contents[0]) !== false) |
| 210 | { |
| 211 | $new_token = array('t' => $t, 'm' => $tag_contents[0], 'd' => trim(substr($tag_contents, 1))); |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | $new_token = array('t' => $t, 'd' => trim($tag_contents)); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // beautiful code is over, here comes the fugly whitespacing fixing mess! |
| 220 | |
| 221 | $standalone = NULL; |
| 222 | $sa_indent = ''; |
| 223 | if($this->whitespace_mode == MUSTACHE_WHITESPACE_STRICT) |
| 224 | { |
| 225 | if(count($this->tokens) > 0) |
| 226 | { |
| 227 | $prev_token = &$this->tokens[count($this->tokens) - 1]; |
| 228 | } |
| 229 | else |
| 230 | { |
| 231 | $prev_token = NULL; |
| 232 | } |
| 233 | |
| 234 | // slowpoke is slow... |
| 235 | $line_index = substr_count($this->template, "\n", 0, ($pos > 0 ? $pos : strlen($this->template))); |
| 236 | |
| 237 | // let's dissect this a bit: |
| 238 | // condition A: there's no new token (=delimiter change, invalid stuff) or the new token is not a tag (so a section, partial, etc.) |
| 239 | // condition B: this is the first token or at least not preceded by a different token on the same line, or only preceded by whitespace (in a literal) |
| 240 | // condition C: there's nothing but a newline or the end of the template following this token |
| 241 | $standalone = (!$new_token || ($new_token['t'] != self::TKN_TAG && $new_token['t'] != self::TKN_TAG_NOESCAPE)) && |
| 242 | ($prev_token === NULL || ($prev_token['t'] !== self::TKN_LITERAL && $prev_token['line'] != $line_index) || (bool)preg_match('~(?:' . (count($this->tokens) == 1 ? '^|' : '') . '\r?\n)([\t ]*)$~D', $prev_token['d'], $match)) |
| 243 | && (bool)preg_match('~^(\r?\n|$)~D', substr($this->template, $end_pos + $dlm_c_len + $advance_extra), $match2); |
| 244 | |
| 245 | if($standalone) |
| 246 | { |
| 247 | // capture indentation: |
| 248 | $sa_indent = isset($match[1]) ? $match[1] : ''; |
| 249 | |
| 250 | // remove it from the preceding literal token, if necessary: |
| 251 | if(strlen($sa_indent) > 0 && $prev_token['t'] === self::TKN_LITERAL) |
| 252 | { |
| 253 | $prev_token['d'] = substr($prev_token['d'], 0, -strlen($sa_indent)); |
| 254 | } |
| 255 | |
| 256 | // skip trailing newline: |
| 257 | $advance_extra += strlen($match2[1]); |
| 258 | |
| 259 | // store token properties: |
| 260 | if($new_token) |
| 261 | { |
| 262 | $new_token['sa'] = true; |
| 263 | $new_token['ind'] = $sa_indent; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | unset($line_index); |
| 270 | } |
| 271 | |
| 272 | // end of whitespace fixing mess. |
| 273 | |
| 274 | if($new_token) |
| 275 | { |
| 276 | if(isset($line_index)) $new_token['line'] = $line_index; |
| 277 | $this->tokens[] = $new_token; |
| 278 | } |
| 279 | |
| 280 | if(!$skip) |
| 281 | { |
| 282 | $prev_pos = $end_pos + $dlm_c_len + $advance_extra; |
| 283 | } |
| 284 | |
| 285 | // find next opening delimiter: |
| 286 | $pos = strpos($this->template, $dlm_o, $end_pos + $dlm_c_len + $advance_extra); |
| 287 | } |
| 288 | |
| 289 | // append remainder (literal following the last section or tag), if there's any: |
| 290 | if($prev_pos < strlen($this->template)) |
| 291 | { |
| 292 | $this->tokens[] = array('t' => self::TKN_LITERAL, 'd' => substr($this->template, $prev_pos)); |
| 293 | } |
| 294 | |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Use this method to retrieve the results from tokenize(). |
| 300 | * @return array |
| 301 | **/ |
| 302 | public function getTokens() |
| 303 | { |
| 304 | return $this->tokens; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /** |
| 310 | * Mustache parser. |
| 311 | * |
| 312 | * @package php-mustache |
| 313 | * @subpackage shared |
| 314 | **/ |
| 315 | class MustacheParser |
| 316 | { |
| 317 | /** |
| 318 | * @var array |
| 319 | **/ |
| 320 | protected $tokens; |
| 321 | /** |
| 322 | * @var MustacheParserSection |
| 323 | **/ |
| 324 | protected $tree = NULL; |
| 325 | /** |
| 326 | * @var array |
| 327 | **/ |
| 328 | protected $partials = array(); |
| 329 | /** |
| 330 | * @var array |
| 331 | **/ |
| 332 | protected $partial_callbacks = array(); |
| 333 | /** |
| 334 | * If this is a partial, its name is stored here. |
| 335 | * @var string |
| 336 | **/ |
| 337 | protected $this_partial_name = NULL; |
| 338 | /** |
| 339 | * @var int |
| 340 | * @see MUSTACHE_WHITESPACE_LAZY |
| 341 | * @see MUSTACHE_WHITESPACE_STRICT |
| 342 | * @see MUSTACHE_WHITESPACE_STRIP |
| 343 | **/ |
| 344 | protected $whitespace_mode; |
| 345 | |
| 346 | /** |
| 347 | * @param string $template |
| 348 | * @param int $whitespace_mode |
| 349 | **/ |
| 350 | public function __construct($template, $whitespace_mode = MUSTACHE_WHITESPACE_LAZY) |
| 351 | { |
| 352 | if(!is_string($template)) |
| 353 | { |
| 354 | throw new MustacheParserException(__CLASS__ . '\'s constructor expects a template string, ' . gettype($template) . ' given.'); |
| 355 | } |
| 356 | |
| 357 | $this->whitespace_mode = $whitespace_mode; |
| 358 | |
| 359 | $tokenizer = new MustacheTokenizer($template, $whitespace_mode); |
| 360 | |
| 361 | if(!$tokenizer->tokenize()) |
| 362 | { |
| 363 | throw new MustacheParserException('The tokenizer failed miserably, please check your template syntax.'); |
| 364 | } |
| 365 | |
| 366 | $this->tokens = $tokenizer->getTokens(); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * @return int |
| 371 | **/ |
| 372 | public function getWhitespaceMode() |
| 373 | { |
| 374 | return $this->whitespace_mode; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Adds a partial with name $key and template contents $tpl. |
| 379 | * @param string $key |
| 380 | * @param string $tpl |
| 381 | **/ |
| 382 | public function addPartial($key, $tpl) |
| 383 | { |
| 384 | if(is_scalar($key) && is_string($tpl)) |
| 385 | { |
| 386 | $this->partials[(string)$key] = $tpl; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Adds multiple partials. |
| 392 | * @see addPartial |
| 393 | * @param array|object $partials |
| 394 | **/ |
| 395 | public function addPartials($partials) |
| 396 | { |
| 397 | if(is_array($partials) || $partials instanceof Iterator) |
| 398 | { |
| 399 | foreach($partials as $key => $tpl) |
| 400 | { |
| 401 | $this->addPartial($key, $tpl); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Adds a callback that will be queried for unknown partials that occur during parsing. |
| 408 | * The signature of the callback is: <code>string pcb($partial_name)</code> |
| 409 | * @param callable $callback |
| 410 | **/ |
| 411 | public function addPartialsCallback($callback) |
| 412 | { |
| 413 | if(is_callable($callback)) |
| 414 | { |
| 415 | $this->partial_callbacks[] = $callback; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Empties the list of added partials and callbacks. |
| 421 | **/ |
| 422 | public function clearPartials() |
| 423 | { |
| 424 | $this->partials = array(); |
| 425 | $this->partial_callbacks = array(); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * References all partials from $partials, usually from another MustacheParser instance. |
| 430 | * @param array& $partials |
| 431 | **/ |
| 432 | protected function refPartials($this_partial_name, array& $partials, array& $partial_callbacks) |
| 433 | { |
| 434 | $this->this_partial_name = $this_partial_name; |
| 435 | $this->partials = &$partials; |
| 436 | $this->partial_callbacks = &$partial_callbacks; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @throw MustacheParserException |
| 441 | **/ |
| 442 | public function parse() |
| 443 | { |
| 444 | $open_sections = array(); |
| 445 | |
| 446 | // use a container section for the entire template: |
| 447 | $root = new MustacheParserSection('#ROOT#'); |
| 448 | |
| 449 | // walk tokens, simultanously checking for invalidities (will throw) |
| 450 | // and adding stuff into a tree under $root: |
| 451 | $parent = $root; |
| 452 | foreach($this->tokens as $token) |
| 453 | { |
| 454 | if($token['t'] == MustacheTokenizer::TKN_LITERAL) |
| 455 | { |
| 456 | if(stripos($token['d'], '<?php') !== false) |
| 457 | { |
| 458 | throw new MustacheParserException('Found PHP code start tag in literal!'); |
| 459 | } |
| 460 | |
| 461 | $parent->addChild(new MustacheParserLiteral($token['d'])); |
| 462 | } |
| 463 | elseif($token['t'] == MustacheTokenizer::TKN_SECTION_START) |
| 464 | { |
| 465 | if($token['m'] == '#') |
| 466 | { |
| 467 | $new = new MustacheParserSection($token['d'], $parent); |
| 468 | } |
| 469 | elseif($token['m'] == '^') |
| 470 | { |
| 471 | $new = new MustacheParserInvertedSection($token['d'], $parent); |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | throw new MustacheParserException('Unknown section type \'' . $token['m'] . '\'.'); |
| 476 | } |
| 477 | |
| 478 | $parent->addChild($new); |
| 479 | |
| 480 | $open_sections[] = $new; |
| 481 | $parent = $new; // descend |
| 482 | } |
| 483 | elseif($token['t'] == MustacheTokenizer::TKN_SECTION_END) |
| 484 | { |
| 485 | $top_sect = array_pop($open_sections); |
| 486 | |
| 487 | if($token['d'] != $top_sect->getName()) |
| 488 | { |
| 489 | throw new MustacheParserException('Found end tag for section \'' . $token['d'] . '\' which is not open.'); |
| 490 | } |
| 491 | |
| 492 | $parent = $top_sect->getParent(); // restore parent |
| 493 | } |
| 494 | elseif($token['t'] == MustacheTokenizer::TKN_COMMENT) |
| 495 | { |
| 496 | // it's a comment, ignore it |
| 497 | } |
| 498 | elseif($token['t'] == MustacheTokenizer::TKN_PARTIAL) |
| 499 | { |
| 500 | // resolve partial, look for recursive partials first: |
| 501 | if(is_string($this->this_partial_name) && !empty($this->this_partial_name) && !strcmp($this->this_partial_name, $token['d'])) |
| 502 | { |
| 503 | // recursive partial |
| 504 | $tag = new MustacheParserRuntimeTemplate($token['d'], $this->partials); |
| 505 | |
| 506 | if(isset($token['ind'])) $tag->setIndent($token['ind']); |
| 507 | |
| 508 | $parent->addChild($tag); |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | // find template string from existing list or query callbacks |
| 513 | $partial_tpl = NULL; |
| 514 | |
| 515 | if(isset($this->partials[$token['d']])) |
| 516 | { |
| 517 | $partial_tpl = $this->partials[$token['d']]; |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | foreach($this->partial_callbacks as $callback) |
| 522 | { |
| 523 | $partial_tpl = $callback($token['d']); |
| 524 | |
| 525 | if(!empty($partial_tpl)) |
| 526 | { |
| 527 | break; |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if(!is_null($partial_tpl)) |
| 533 | { |
| 534 | // replace partials at "compile time": |
| 535 | $partial_parser = new self($partial_tpl, $this->whitespace_mode); |
| 536 | $partial_parser->refPartials($token['d'], $this->partials, $this->partial_callbacks); |
| 537 | $partial_parser->parse(); |
| 538 | |
| 539 | // :TODO: consider indentation |
| 540 | |
| 541 | foreach($partial_parser->getTree() as $partial_child) |
| 542 | { |
| 543 | $parent->addChild($partial_child); |
| 544 | } |
| 545 | |
| 546 | unset($partial_parser); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | elseif($token['t'] == MustacheTokenizer::TKN_TAG || $token['t'] == MustacheTokenizer::TKN_TAG_NOESCAPE) |
| 551 | { |
| 552 | $modifier = isset($token['m']) ? $token['m'] : ''; |
| 553 | |
| 554 | if($modifier == '&' || $modifier == '') |
| 555 | { |
| 556 | // boring interpolation... |
| 557 | $tag = new MustacheParserVariable($token['d'], ($token['t'] != MustacheTokenizer::TKN_TAG_NOESCAPE) xor $modifier == '&'); |
| 558 | |
| 559 | if(isset($token['ind'])) $tag->setIndent($token['ind']); |
| 560 | |
| 561 | $parent->addChild($tag); |
| 562 | } |
| 563 | else |
| 564 | { |
| 565 | throw new MustacheParserException('Unknown tag type \'' . $modifier . '\'.'); |
| 566 | } |
| 567 | } |
| 568 | } // end of $token loop |
| 569 | |
| 570 | if(count($open_sections) > 0) |
| 571 | { |
| 572 | throw new MustacheParserException('Found unclosed section tag pairs.'); |
| 573 | } |
| 574 | |
| 575 | $this->tree = $root; |
| 576 | |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Returns the tree formed by parse(), encapsulated in a root MustacheParserSection of name #ROOT#. |
| 582 | * @see parse |
| 583 | * @return MustacheParserSection |
| 584 | **/ |
| 585 | public function getTree() |
| 586 | { |
| 587 | return $this->tree; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
| 592 | /** |
| 593 | * An object extracted by the parser. Used by code gens, interpreters and such. |
| 594 | * Does not contain a lot of logic, mostly a data store with some utils. |
| 595 | * The other parser object classes derive from this class. |
| 596 | * @package php-mustache |
| 597 | * @subpackage shared |
| 598 | **/ |
| 599 | abstract class MustacheParserObject |
| 600 | { |
| 601 | /** |
| 602 | * Parent element. Not all derived objects use this. |
| 603 | * @var MustacheParserSection|NULL |
| 604 | **/ |
| 605 | protected $parent = NULL; |
| 606 | /** |
| 607 | * Whitespace string that defines this object's indentation. Used by partials mostly. |
| 608 | * @var string |
| 609 | **/ |
| 610 | protected $indent = ''; |
| 611 | |
| 612 | /** |
| 613 | * Constructor. |
| 614 | * @param MustacheParserSection $parent Really only used for sections so far. |
| 615 | **/ |
| 616 | public function __construct(MustacheParserSection $parent = NULL) |
| 617 | { |
| 618 | $this->parent = $parent; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * @return MustacheParserSection|NULL |
| 623 | **/ |
| 624 | public function getParent() |
| 625 | { |
| 626 | return $this->parent; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Corrects or sets this object's parent element. Used by addChild in section objects. |
| 631 | * @see MustacheParserSection::addChild |
| 632 | **/ |
| 633 | public function _setParent(MustacheParserSection $new_parent) |
| 634 | { |
| 635 | $this->parent = $new_parent; |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Sets the whitespace/indentation to store with this element. |
| 640 | * @param string $new Whitespace characters. |
| 641 | **/ |
| 642 | public function setIndent($new) |
| 643 | { |
| 644 | $this->indent = $new; |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Returns the indent string, usually empty or a number of whitespace characters. |
| 649 | **/ |
| 650 | public function getIndent() |
| 651 | { |
| 652 | return $this->indent; |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | |
| 657 | /** |
| 658 | * An object extracted by the parser, with an entity name. Provides helper methods |
| 659 | * for dealing with dot-notation syntax. |
| 660 | * @package php-mustache |
| 661 | * @subpackage shared |
| 662 | **/ |
| 663 | abstract class MustacheParserObjectWithName extends MustacheParserObject |
| 664 | { |
| 665 | /** |
| 666 | * "Variable" name, e.g. "view" or "object.description". |
| 667 | * @var string |
| 668 | **/ |
| 669 | protected $name; |
| 670 | /** |
| 671 | * Dot-notation parts as an array. |
| 672 | * @var array |
| 673 | **/ |
| 674 | protected $dot_parts; |
| 675 | |
| 676 | /** |
| 677 | * Constructor. |
| 678 | * @param string $name |
| 679 | * @param MustacheParserSection|null $parent |
| 680 | **/ |
| 681 | public function __construct($name, MustacheParserSection $parent = NULL) |
| 682 | { |
| 683 | parent::__construct($parent); |
| 684 | $this->name = $name; |
| 685 | $this->dot_parts = ($this->name == '.' ? array('.') : explode('.', $name)); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Returns whether this object's name makes use of dot-notation. |
| 690 | * @return boolean |
| 691 | **/ |
| 692 | public function isDotNotation() |
| 693 | { |
| 694 | return (count($this->dot_parts) > 1); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Returns this object's name. |
| 699 | * @return string |
| 700 | **/ |
| 701 | public function getName() |
| 702 | { |
| 703 | return $this->name; |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Returns this object's names, as an array. Useful with dot-notation. |
| 708 | * @return array |
| 709 | **/ |
| 710 | public function getNames() |
| 711 | { |
| 712 | return $this->dot_parts; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | |
| 717 | /** |
| 718 | * A section parser object. |
| 719 | * @package php-mustache |
| 720 | * @subpackage shared |
| 721 | **/ |
| 722 | class MustacheParserSection extends MustacheParserObjectWithName implements Iterator |
| 723 | { |
| 724 | /** |
| 725 | * @var array |
| 726 | **/ |
| 727 | protected $children = array(); |
| 728 | |
| 729 | /** |
| 730 | * Constructor. |
| 731 | * @param string $name |
| 732 | * @param MustacheParserSection|null $parent |
| 733 | **/ |
| 734 | public function __construct($name, MustacheParserSection $parent = NULL) |
| 735 | { |
| 736 | parent::__construct($name, $parent); |
| 737 | $this->name = $name; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Adds a child parser object to this section. Changes $child's parent to $this. |
| 742 | * @param MustacheParserObject $child |
| 743 | **/ |
| 744 | public function addChild(MustacheParserObject $child) |
| 745 | { |
| 746 | $child->_setParent($this); |
| 747 | $this->children[] = $child; |
| 748 | } |
| 749 | |
| 750 | // Iterator interface implementation: |
| 751 | |
| 752 | private $it_pos = 0; |
| 753 | function rewind() { $this->it_pos = 0; } |
| 754 | function current() { return $this->children[$this->it_pos]; } |
| 755 | function key() { return $this->it_pos; } |
| 756 | function next() { $this->it_pos++; } |
| 757 | function valid() { return isset($this->children[$this->it_pos]); } |
| 758 | } |
| 759 | |
| 760 | |
| 761 | /** |
| 762 | * An inverted section parser object. Exactly the same as MustacheParserSection, |
| 763 | * however "$var isinstanceof MustacheParserInvertedSection" will be used to tell them apart. |
| 764 | * @package php-mustache |
| 765 | * @subpackage shared |
| 766 | **/ |
| 767 | class MustacheParserInvertedSection extends MustacheParserSection |
| 768 | { |
| 769 | |
| 770 | } |
| 771 | |
| 772 | |
| 773 | /** |
| 774 | * Parser object that represents a literal string part of a template. |
| 775 | * @package php-mustache |
| 776 | * @subpackage shared |
| 777 | **/ |
| 778 | class MustacheParserLiteral extends MustacheParserObject |
| 779 | { |
| 780 | /** |
| 781 | * @var string |
| 782 | **/ |
| 783 | protected $contents; |
| 784 | |
| 785 | /** |
| 786 | * Constructor... |
| 787 | * @param string $contents |
| 788 | **/ |
| 789 | public function __construct($contents) |
| 790 | { |
| 791 | $this->contents = $contents; |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * Damn, this is a boring class. |
| 796 | * @return string |
| 797 | **/ |
| 798 | public function getContents() |
| 799 | { |
| 800 | return $this->contents; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
| 805 | * This parser object represents a variable / {{interpolation}}. |
| 806 | * @package php-mustache |
| 807 | * @subpackage shared |
| 808 | **/ |
| 809 | class MustacheParserVariable extends MustacheParserObjectWithName |
| 810 | { |
| 811 | /** |
| 812 | * @var boolean |
| 813 | **/ |
| 814 | protected $escape; |
| 815 | |
| 816 | /** |
| 817 | * @param string name |
| 818 | * @param boolean escape |
| 819 | **/ |
| 820 | public function __construct($name, $escape) |
| 821 | { |
| 822 | parent::__construct($name); |
| 823 | $this->escape = $escape; |
| 824 | } |
| 825 | |
| 826 | /** |
| 827 | * (HTML)escape this variable's contents? |
| 828 | * @return boolean |
| 829 | **/ |
| 830 | public function escape() |
| 831 | { |
| 832 | return $this->escape; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | |
| 837 | /** |
| 838 | * Represents a piece of mustache template that *must* be evaluated at runtime. |
| 839 | * Currently only used for recursive partials. |
| 840 | * @package php-mustache |
| 841 | * @subpackage shared |
| 842 | **/ |
| 843 | class MustacheParserRuntimeTemplate extends MustacheParserObject |
| 844 | { |
| 845 | /** |
| 846 | * Partial's name |
| 847 | * @var string |
| 848 | **/ |
| 849 | protected $name; |
| 850 | /** |
| 851 | * List of all partials, required since they could be used by the "main" partial or other partials. |
| 852 | * @var array |
| 853 | **/ |
| 854 | protected $partials; |
| 855 | |
| 856 | /** |
| 857 | * @var string $name |
| 858 | * @var array $partials |
| 859 | **/ |
| 860 | public function __construct($name, array $partials) |
| 861 | { |
| 862 | $this->name = $name; |
| 863 | $this->partials = $partials; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Returns the template contents of this partial. |
| 868 | * @return string |
| 869 | **/ |
| 870 | public function lookupSelf() |
| 871 | { |
| 872 | return $this->partials[$this->name]; |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Returns a copy of the list of all partials. |
| 877 | * @return array |
| 878 | **/ |
| 879 | public function getPartials() |
| 880 | { |
| 881 | return $this->partials; |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * Returns this partial's name. |
| 886 | * @return string |
| 887 | **/ |
| 888 | public function getName() |
| 889 | { |
| 890 | return $this->name; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | |
| 895 | /** |
| 896 | * Mustache parser exception class. |
| 897 | **/ |
| 898 | class MustacheParserException extends Exception |
| 899 | { |
| 900 | |
| 901 | } |