1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Latte\Macros;
9:
10: use Nette;
11: use Nette\Latte;
12: use 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: foreach (array($begin, $end, $attr) as $arg) {
38: if ($arg && !is_string($arg)) {
39: Nette\Utils\Callback::check($arg);
40: }
41: }
42:
43: $this->macros[$name] = array($begin, $end, $attr);
44: $this->compiler->addMacro($name, $this);
45: return $this;
46: }
47:
48:
49: public static function install(Latte\Compiler $compiler)
50: {
51: return new static($compiler);
52: }
53:
54:
55: 56: 57: 58:
59: public function initialize()
60: {
61: }
62:
63:
64: 65: 66: 67:
68: public function finalize()
69: {
70: }
71:
72:
73: 74: 75: 76:
77: public function nodeOpened(MacroNode $node)
78: {
79: list($begin, $end, $attr) = $this->macros[$node->name];
80: $node->isEmpty = !$end;
81:
82: if ($attr && $node->prefix === $node::PREFIX_NONE) {
83: $node->isEmpty = TRUE;
84: $this->compiler->setContext(Latte\Compiler::CONTEXT_DOUBLE_QUOTED_ATTR);
85: $res = $this->compile($node, $attr);
86: if ($res === FALSE) {
87: return FALSE;
88: } elseif (!$node->attrCode) {
89: $node->attrCode = "<?php $res ?>";
90: }
91: $this->compiler->setContext(NULL);
92:
93: } elseif ($begin) {
94: $res = $this->compile($node, $begin);
95: if ($res === FALSE) {
96: return FALSE;
97: } elseif (!$node->openingCode) {
98: $node->openingCode = "<?php $res ?>";
99: }
100:
101: } elseif (!$end) {
102: return FALSE;
103: }
104: }
105:
106:
107: 108: 109: 110:
111: public function nodeClosed(MacroNode $node)
112: {
113: if (isset($this->macros[$node->name][1])) {
114: $res = $this->compile($node, $this->macros[$node->name][1]);
115: if (!$node->closingCode) {
116: $node->closingCode = "<?php $res ?>";
117: }
118: }
119: }
120:
121:
122: 123: 124: 125:
126: private function compile(MacroNode $node, $def)
127: {
128: $node->tokenizer->reset();
129: $writer = Latte\PhpWriter::using($node, $this->compiler);
130: if (is_string($def)) {
131: return $writer->write($def);
132: } else {
133: return Nette\Utils\Callback::invoke($def, $node, $writer);
134: }
135: }
136:
137:
138: 139: 140:
141: public function getCompiler()
142: {
143: return $this->compiler;
144: }
145:
146: }
147: