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: public function __construct(NLatteCompiler $compiler)
31: {
32: $this->compiler = $compiler;
33: }
34:
35:
36: public function addMacro($name, $begin, $end = NULL, $attr = NULL)
37: {
38: $this->macros[$name] = array($begin, $end, $attr);
39: $this->compiler->addMacro($name, $this);
40: return $this;
41: }
42:
43:
44: public static function install(NLatteCompiler $compiler)
45: {
46: return new self($compiler);
47: }
48:
49:
50: 51: 52: 53:
54: public function initialize()
55: {
56: }
57:
58:
59: 60: 61: 62:
63: public function finalize()
64: {
65: }
66:
67:
68: 69: 70: 71:
72: public function nodeOpened(NMacroNode $node)
73: {
74: if ($this->macros[$node->name][2] && $node->htmlNode) {
75: $node->isEmpty = TRUE;
76: $this->compiler->setContext(NLatteCompiler::CONTEXT_DOUBLE_QUOTED);
77: $res = $this->compile($node, $this->macros[$node->name][2]);
78: $this->compiler->setContext(NULL);
79: if (!$node->attrCode) {
80: $node->attrCode = "<?php $res ?>";
81: }
82: } else {
83: $node->isEmpty = !isset($this->macros[$node->name][1]);
84: $res = $this->compile($node, $this->macros[$node->name][0]);
85: if (!$node->openingCode) {
86: $node->openingCode = "<?php $res ?>";
87: }
88: }
89: return $res !== FALSE;
90: }
91:
92:
93: 94: 95: 96:
97: public function nodeClosed(NMacroNode $node)
98: {
99: $res = $this->compile($node, $this->macros[$node->name][1]);
100: if (!$node->closingCode) {
101: $node->closingCode = "<?php $res ?>";
102: }
103: }
104:
105:
106: 107: 108: 109:
110: private function compile(NMacroNode $node, $def)
111: {
112: $node->tokenizer->reset();
113: $writer = NPhpWriter::using($node, $this->compiler);
114: if (is_string($def)&& substr($def, 0, 1) !== "\0") {
115: return $writer->write($def);
116: } else {
117: return NCallback::create($def)->invoke($node, $writer);
118: }
119: }
120:
121:
122: 123: 124:
125: public function getCompiler()
126: {
127: return $this->compiler;
128: }
129:
130: }
131: