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