1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Latte\Macros;
13:
14: use Nette,
15: Nette\Latte,
16: Nette\Latte\MacroNode;
17:
18:
19:
20: 21: 22: 23: 24:
25: class MacroSet extends Nette\Object implements Latte\IMacro
26: {
27:
28: public $parser;
29:
30:
31: private $macros;
32:
33:
34:
35: public function __construct(Latte\Parser $parser)
36: {
37: $this->parser = $parser;
38: }
39:
40:
41:
42: public function addMacro($name, $begin, $end = NULL)
43: {
44: $this->macros[$name] = array($begin, $end);
45: $this->parser->addMacro($name, $this);
46: }
47:
48:
49:
50: public static function install(Latte\Parser $parser)
51: {
52: return new static($parser);
53: }
54:
55:
56:
57: 58: 59: 60:
61: public function initialize()
62: {
63: }
64:
65:
66:
67: 68: 69: 70:
71: public function finalize()
72: {
73: }
74:
75:
76:
77: 78: 79: 80:
81: public function nodeOpened(MacroNode $node)
82: {
83: $node->isEmpty = !isset($this->macros[$node->name][1]);
84: return $this->compile($node, $this->macros[$node->name][0]);
85: }
86:
87:
88:
89: 90: 91: 92:
93: public function nodeClosed(MacroNode $node)
94: {
95: return $this->compile($node, $this->macros[$node->name][1]);
96: }
97:
98:
99:
100: 101: 102: 103:
104: private function compile(MacroNode $node, $def)
105: {
106: $writer = Latte\PhpWriter::using($node, $this->parser->context);
107: if (is_string($def)) {
108: $code = $writer->write($def);
109: } else {
110: $code = callback($def)->invoke($node, $writer);
111: if ($code === FALSE) {
112: return FALSE;
113: }
114: }
115: return "<?php $code ?>";
116: }
117:
118: }
119: