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