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)
43: {
44: $this->macros[$name] = array($begin, $end);
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: $node->isEmpty = !isset($this->macros[$node->name][1]);
85: return $this->compile($node, $this->macros[$node->name][0]);
86: }
87:
88:
89:
90: 91: 92: 93:
94: public function nodeClosed(MacroNode $node)
95: {
96: return $this->compile($node, $this->macros[$node->name][1]);
97: }
98:
99:
100:
101: 102: 103: 104:
105: private function compile(MacroNode $node, $def)
106: {
107: $node->tokenizer->reset();
108: $writer = Latte\PhpWriter::using($node, $this->compiler);
109: if (is_string($def)) {
110: $code = $writer->write($def);
111: } else {
112: $code = callback($def)->invoke($node, $writer);
113: if ($code === FALSE) {
114: return FALSE;
115: }
116: }
117: return "<?php $code ?>";
118: }
119:
120:
121:
122: 123: 124:
125: public function getCompiler()
126: {
127: return $this->compiler;
128: }
129:
130: }
131: