|
11 | 11 |
|
12 | 12 | use Latte; |
13 | 13 | use Latte\CompileException; |
| 14 | +use Latte\Helpers; |
14 | 15 | use Latte\MacroNode; |
15 | 16 | use Latte\PhpWriter; |
| 17 | +use Nette\Application\UI\Presenter; |
16 | 18 | use Nette\Utils\Strings; |
17 | 19 |
|
18 | 20 |
|
@@ -44,6 +46,7 @@ public static function install(Latte\Compiler $compiler): void |
44 | 46 | $me->addMacro('extends', [$me, 'macroExtends']); |
45 | 47 | $me->addMacro('layout', [$me, 'macroExtends']); |
46 | 48 | $me->addMacro('nonce', null, null, 'echo $this->global->uiNonce ? " nonce=\"{$this->global->uiNonce}\"" : "";'); |
| 49 | + $me->addMacro('templatePrint', [$me, 'macroTemplatePrint'], null, null, self::ALLOWED_IN_HEAD); |
47 | 50 | } |
48 | 51 |
|
49 | 52 |
|
@@ -147,4 +150,55 @@ public function macroExtends(MacroNode $node, PhpWriter $writer) |
147 | 150 | } |
148 | 151 | $this->extends = $writer->write('$this->parentName = $this->global->uiPresenter->findLayoutTemplateFile();'); |
149 | 152 | } |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * {templatePrint [ClassName]} |
| 157 | + */ |
| 158 | + public function macroTemplatePrint(MacroNode $node, PhpWriter $writer) |
| 159 | + { |
| 160 | + $class = $node->tokenizer->fetchWord() ?: null; |
| 161 | + return $writer->write(__CLASS__ . '::macroTemplatePrintRuntime($this, %var)', $class); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + /** |
| 166 | + * Generates blueprint of template class. |
| 167 | + */ |
| 168 | + public static function macroTemplatePrintRuntime(Latte\Runtime\Template $template, ?string $class): void |
| 169 | + { |
| 170 | + $types = array_map([Helpers::class, 'getType'], $template->getParameters()); |
| 171 | + if ($template->getParameter('control') instanceof Presenter) { |
| 172 | + unset($types['control']); |
| 173 | + $subject = $template->getParameter('presenter'); |
| 174 | + $class = $class ?: preg_replace('#Presenter$#', '', get_class($subject)) . ucfirst($subject->getView()) . 'Template'; |
| 175 | + } else { |
| 176 | + unset($types['presenter']); |
| 177 | + $subject = $template->getParameter('control'); |
| 178 | + $class = $class ?: preg_replace('#Control$#', '', get_class($subject)) . 'Template'; |
| 179 | + } |
| 180 | + unset($types['user'], $types['baseUrl'], $types['basePath'], $types['flashes']); |
| 181 | + |
| 182 | + ob_end_clean(); |
| 183 | + header('Content-Type: text/plain'); |
| 184 | + $parts = explode('\\', $class); |
| 185 | + $name = array_pop($parts); |
| 186 | + $namespace = implode('\\', $parts); |
| 187 | + $parent = Template::class; |
| 188 | + $subject = explode('\\', get_class($subject)); |
| 189 | + $subject = end($subject); |
| 190 | + echo |
| 191 | + ($namespace ? "namespace $namespace;\n\n" : '') |
| 192 | + . "class $name extends $parent\n" |
| 193 | + . "{\n" . Helpers::printProperties($types, true) . "\n}\n" |
| 194 | + . "\n\n" |
| 195 | + . "/**\n" . Helpers::printProperties($types, false) . "\n */\n" |
| 196 | + . "class $name extends $parent\n" |
| 197 | + . "{\n}\n" |
| 198 | + . "\n\n" |
| 199 | + . "/**\n * @property $name \$template\n */\n" |
| 200 | + . "class $subject\n" |
| 201 | + . "{\n}\n"; |
| 202 | + exit; |
| 203 | + } |
150 | 204 | } |
0 commit comments