1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationLatte;
9:
10: use Latte;
11: use Latte\CompileException;
12: use Latte\MacroNode;
13: use Latte\PhpWriter;
14: use Nette;
15: use Nette\Utils\Strings;
16:
17:
18: 19: 20: 21: 22: 23: 24: 25:
26: class UIMacros extends Latte\Macros\MacroSet
27: {
28:
29: private $extends;
30:
31:
32: public static function install(Latte\Compiler $compiler)
33: {
34: $me = new static($compiler);
35: $me->addMacro('control', [$me, 'macroControl']);
36:
37: $me->addMacro('href', null, null, function (MacroNode $node, PhpWriter $writer) use ($me) {
38: return ' ?> href="<?php ' . $me->macroLink($node, $writer) . ' ?>"<?php ';
39: });
40: $me->addMacro('plink', [$me, 'macroLink']);
41: $me->addMacro('link', [$me, 'macroLink']);
42: $me->addMacro('ifCurrent', [$me, 'macroIfCurrent'], '}');
43: $me->addMacro('extends', [$me, 'macroExtends']);
44: $me->addMacro('layout', [$me, 'macroExtends']);
45: $me->addMacro('nonce', null, null, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";');
46: }
47:
48:
49: 50: 51: 52:
53: public function initialize()
54: {
55: $this->extends = false;
56: }
57:
58:
59: 60: 61: 62:
63: public function finalize()
64: {
65: return [$this->extends . 'Nette\Bridges\ApplicationLatte\UIRuntime::initialize($this, $this->parentName, $this->blocks);'];
66: }
67:
68:
69:
70:
71:
72: 73: 74:
75: public function macroControl(MacroNode $node, PhpWriter $writer)
76: {
77: $words = $node->tokenizer->fetchWords();
78: if (!$words) {
79: throw new CompileException('Missing control name in {control}');
80: }
81: $name = $writer->formatWord($words[0]);
82: $method = isset($words[1]) ? ucfirst($words[1]) : '';
83: $method = Strings::match($method, '#^\w*\z#') ? "render$method" : "{\"render$method\"}";
84:
85: $tokens = $node->tokenizer;
86: $pos = $tokens->position;
87: $param = $writer->formatArray();
88: $tokens->position = $pos;
89: while ($tokens->nextToken()) {
90: if ($tokens->isCurrent('=>') && !$tokens->depth) {
91: $wrap = true;
92: break;
93: }
94: }
95: if (empty($wrap)) {
96: $param = substr($param, 1, -1);
97: }
98: return "/* line $node->startLine */ "
99: . ($name[0] === '$' ? "if (is_object($name)) \$_tmp = $name; else " : '')
100: . '$_tmp = $this->global->uiControl->getComponent(' . $name . '); '
101: . 'if ($_tmp instanceof Nette\Application\UI\IRenderable) $_tmp->redrawControl(null, false); '
102: . ($node->modifiers === ''
103: ? "\$_tmp->$method($param);"
104: : $writer->write("ob_start(function () {}); \$_tmp->$method($param); echo %modify(ob_get_clean());")
105: );
106: }
107:
108:
109: 110: 111: 112: 113:
114: public function macroLink(MacroNode $node, PhpWriter $writer)
115: {
116: $node->modifiers = preg_replace('#\|safeurl\s*(?=\||\z)#i', '', $node->modifiers);
117: return $writer->using($node)
118: ->write('echo %escape(%modify('
119: . ($node->name === 'plink' ? '$this->global->uiPresenter' : '$this->global->uiControl')
120: . '->link(%node.word, %node.array?)))'
121: );
122: }
123:
124:
125: 126: 127:
128: public function macroIfCurrent(MacroNode $node, PhpWriter $writer)
129: {
130: if ($node->modifiers) {
131: throw new CompileException('Modifiers are not allowed in ' . $node->getNotation());
132: }
133: return $writer->write($node->args
134: ? 'if ($this->global->uiPresenter->isLinkCurrent(%node.word, %node.array?)) {'
135: : 'if ($this->global->uiPresenter->getLastCreatedRequestFlag("current")) {'
136: );
137: }
138:
139:
140: 141: 142:
143: public function macroExtends(MacroNode $node, PhpWriter $writer)
144: {
145: if ($node->modifiers || $node->parentNode || $node->args !== 'auto') {
146: return $this->extends = false;
147: }
148: $this->extends = $writer->write('$this->parentName = $this->global->uiPresenter->findLayoutTemplateFile();');
149: }
150:
151:
152:
153: public static function renderSnippets(Nette\Application\UI\Control $control, \stdClass $local, array $params)
154: {
155: trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
156: UIRuntime::renderSnippets($control, $local, $params);
157: }
158: }
159: