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