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: public $parser;
25:
26:
27: private $macros;
28:
29:
30:
31: public function __construct(NParser $parser)
32: {
33: $this->parser = $parser;
34: }
35:
36:
37:
38: public function addMacro($name, $begin, $end = NULL)
39: {
40: $this->macros[$name] = array($begin, $end);
41: $this->parser->addMacro($name, $this);
42: }
43:
44:
45:
46: public static function install(NParser $parser)
47: {
48: return new self($parser);
49: }
50:
51:
52:
53: 54: 55: 56:
57: public function initialize()
58: {
59: }
60:
61:
62:
63: 64: 65: 66:
67: public function finalize()
68: {
69: }
70:
71:
72:
73: 74: 75: 76:
77: public function nodeOpened(NMacroNode $node)
78: {
79: $node->isEmpty = !isset($this->macros[$node->name][1]);
80: return $this->compile($node, $this->macros[$node->name][0]);
81: }
82:
83:
84:
85: 86: 87: 88:
89: public function nodeClosed(NMacroNode $node)
90: {
91: return $this->compile($node, $this->macros[$node->name][1]);
92: }
93:
94:
95:
96: 97: 98: 99:
100: private function compile(NMacroNode $node, $def)
101: {
102: $writer = NPhpWriter::using($node, $this->parser->context);
103: if (is_string($def)&& substr($def, 0, 1) !== "\0") {
104: $code = $writer->write($def);
105: } else {
106: $code = callback($def)->invoke($node, $writer);
107: if ($code === FALSE) {
108: return FALSE;
109: }
110: }
111: return "<?php $code ?>";
112: }
113:
114: }
115: