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