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