1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NMacroSet extends NObject implements IMacro
22: {
23:
24: private $compiler;
25:
26:
27: private $macros;
28:
29:
30:
31: public function __construct(NLatteCompiler $compiler)
32: {
33: $this->compiler = $compiler;
34: }
35:
36:
37:
38: public function addMacro($name, $begin, $end = NULL, $attr = NULL)
39: {
40: $this->macros[$name] = array($begin, $end, $attr);
41: $this->compiler->addMacro($name, $this);
42: return $this;
43: }
44:
45:
46:
47: public static function install(NLatteCompiler $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(NMacroNode $node)
79: {
80: if ($this->macros[$node->name][2] && $node->htmlNode) {
81: $node->isEmpty = TRUE;
82: $this->compiler->setContext(NLatteCompiler::CONTEXT_DOUBLE_QUOTED);
83: $res = $this->compile($node, $this->macros[$node->name][2]);
84: $this->compiler->setContext(NULL);
85: if (!$node->attrCode) {
86: $node->attrCode = "<?php $res ?>";
87: }
88: } else {
89: $node->isEmpty = !isset($this->macros[$node->name][1]);
90: $res = $this->compile($node, $this->macros[$node->name][0]);
91: if (!$node->openingCode) {
92: $node->openingCode = "<?php $res ?>";
93: }
94: }
95: return $res !== FALSE;
96: }
97:
98:
99:
100: 101: 102: 103:
104: public function nodeClosed(NMacroNode $node)
105: {
106: $res = $this->compile($node, $this->macros[$node->name][1]);
107: if (!$node->closingCode) {
108: $node->closingCode = "<?php $res ?>";
109: }
110: }
111:
112:
113:
114: 115: 116: 117:
118: private function compile(NMacroNode $node, $def)
119: {
120: $node->tokenizer->reset();
121: $writer = NPhpWriter::using($node, $this->compiler);
122: if (is_string($def)&& substr($def, 0, 1) !== "\0") {
123: return $writer->write($def);
124: } else {
125: return callback($def)->invoke($node, $writer);
126: }
127: }
128:
129:
130:
131: 132: 133:
134: public function getCompiler()
135: {
136: return $this->compiler;
137: }
138:
139: }
140: