| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Bridges\FormsLatte; |
| 9 |
|
| 10 |
use Packetery\Latte; |
| 11 |
use Packetery\Latte\CompileException; |
| 12 |
use Packetery\Latte\MacroNode; |
| 13 |
use Packetery\Latte\Macros\MacroSet; |
| 14 |
use Packetery\Latte\PhpWriter; |
| 15 |
/** |
| 16 |
* Latte macros for \Packetery\Nette\Forms. |
| 17 |
* |
| 18 |
* - {form name} ... {/form} |
| 19 |
* - {input name} |
| 20 |
* - {label name /} or {label name}... {/label} |
| 21 |
* - {inputError name} |
| 22 |
* - {formContainer name} ... {/formContainer} |
| 23 |
* - {formContext name} ... {/formContext} |
| 24 |
*/ |
| 25 |
final class FormMacros extends MacroSet |
| 26 |
{ |
| 27 |
public static function install(Latte\Compiler $compiler) : void |
| 28 |
{ |
| 29 |
$me = new static($compiler); |
| 30 |
$me->addMacro('form', [$me, 'macroForm'], 'echo \\Packetery\\Nette\\Bridges\\FormsLatte\\Runtime::renderFormEnd(array_pop($this->global->formsStack));'); |
| 31 |
$me->addMacro('formContext', [$me, 'macroFormContext'], 'array_pop($this->global->formsStack);'); |
| 32 |
$me->addMacro('formContainer', [$me, 'macroFormContainer'], 'array_pop($this->global->formsStack); $formContainer = end($this->global->formsStack)'); |
| 33 |
$me->addMacro('label', [$me, 'macroLabel'], [$me, 'macroLabelEnd'], null, self::AUTO_EMPTY); |
| 34 |
$me->addMacro('input', [$me, 'macroInput']); |
| 35 |
$me->addMacro('name', [$me, 'macroName'], [$me, 'macroNameEnd'], [$me, 'macroNameAttr']); |
| 36 |
$me->addMacro('inputError', [$me, 'macroInputError']); |
| 37 |
$me->addMacro('formPrint', [$me, 'macroFormPrint']); |
| 38 |
} |
| 39 |
/********************* macros ****************d*g**/ |
| 40 |
/** |
| 41 |
* {form ...} |
| 42 |
*/ |
| 43 |
public function macroForm(MacroNode $node, PhpWriter $writer) |
| 44 |
{ |
| 45 |
if ($node->modifiers) { |
| 46 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 47 |
} |
| 48 |
if ($node->prefix) { |
| 49 |
throw new CompileException('Did you mean <form n:name=...> ?'); |
| 50 |
} |
| 51 |
$name = $node->tokenizer->fetchWord(); |
| 52 |
if ($name == null) { |
| 53 |
// null or false |
| 54 |
throw new CompileException('Missing form name in ' . $node->getNotation()); |
| 55 |
} |
| 56 |
$node->replaced = \true; |
| 57 |
$node->tokenizer->reset(); |
| 58 |
return $writer->write('echo \\Packetery\\Nette\\Bridges\\FormsLatte\\Runtime::renderFormBegin($form = $this->global->formsStack[] = ' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . '$this->global->uiControl[%node.word], %node.array)' . " /* line {$node->startLine} */;"); |
| 59 |
} |
| 60 |
/** |
| 61 |
* {formContext ...} |
| 62 |
*/ |
| 63 |
public function macroFormContext(MacroNode $node, PhpWriter $writer) |
| 64 |
{ |
| 65 |
if ($node->modifiers) { |
| 66 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 67 |
} |
| 68 |
if ($node->prefix) { |
| 69 |
throw new CompileException('Did you mean <form n:name=...> ?'); |
| 70 |
} |
| 71 |
$name = $node->tokenizer->fetchWord(); |
| 72 |
if ($name == null) { |
| 73 |
// null or false |
| 74 |
throw new CompileException('Missing form name in ' . $node->getNotation()); |
| 75 |
} |
| 76 |
$node->tokenizer->reset(); |
| 77 |
return $writer->write('$form = $this->global->formsStack[] = ' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . '$this->global->uiControl[%node.word]' . " /* line {$node->startLine} */;"); |
| 78 |
} |
| 79 |
/** |
| 80 |
* {formContainer ...} |
| 81 |
*/ |
| 82 |
public function macroFormContainer(MacroNode $node, PhpWriter $writer) |
| 83 |
{ |
| 84 |
if ($node->modifiers) { |
| 85 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 86 |
} |
| 87 |
$name = $node->tokenizer->fetchWord(); |
| 88 |
if ($name == null) { |
| 89 |
// null or false |
| 90 |
throw new CompileException('Missing name in ' . $node->getNotation()); |
| 91 |
} |
| 92 |
$node->tokenizer->reset(); |
| 93 |
return $writer->write('$this->global->formsStack[] = $formContainer = ' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . 'end($this->global->formsStack)[%node.word]' . " /* line {$node->startLine} */;"); |
| 94 |
} |
| 95 |
/** |
| 96 |
* {label ...} |
| 97 |
*/ |
| 98 |
public function macroLabel(MacroNode $node, PhpWriter $writer) |
| 99 |
{ |
| 100 |
if ($node->modifiers) { |
| 101 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 102 |
} |
| 103 |
$words = $node->tokenizer->fetchWords(); |
| 104 |
if (!$words) { |
| 105 |
throw new CompileException('Missing name in ' . $node->getNotation()); |
| 106 |
} |
| 107 |
$node->replaced = \true; |
| 108 |
$name = \array_shift($words); |
| 109 |
return $writer->write(($name[0] === '$' ? '$ʟ_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; if ($ʟ_label = $ʟ_input' : 'if ($ʟ_label = end($this->global->formsStack)[%0.word]') . '->%1.raw) echo $ʟ_label' . ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : ''), $name, $words ? 'getLabelPart(' . \implode(', ', \array_map([$writer, 'formatWord'], $words)) . ')' : 'getLabel()'); |
| 110 |
} |
| 111 |
/** |
| 112 |
* {/label} |
| 113 |
*/ |
| 114 |
public function macroLabelEnd(MacroNode $node, PhpWriter $writer) |
| 115 |
{ |
| 116 |
if ($node->content != null) { |
| 117 |
$node->openingCode = \rtrim($node->openingCode, '?> ') . '->startTag() ?>'; |
| 118 |
return $writer->write('if ($ʟ_label) echo $ʟ_label->endTag()'); |
| 119 |
} |
| 120 |
} |
| 121 |
/** |
| 122 |
* {input ...} |
| 123 |
*/ |
| 124 |
public function macroInput(MacroNode $node, PhpWriter $writer) |
| 125 |
{ |
| 126 |
if ($node->modifiers) { |
| 127 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 128 |
} |
| 129 |
$words = $node->tokenizer->fetchWords(); |
| 130 |
if (!$words) { |
| 131 |
throw new CompileException('Missing name in ' . $node->getNotation()); |
| 132 |
} |
| 133 |
$node->replaced = \true; |
| 134 |
$name = \array_shift($words); |
| 135 |
return $writer->write(($name[0] === '$' ? '$ʟ_input = $_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word]; echo $ʟ_input' : 'echo end($this->global->formsStack)[%0.word]') . '->%1.raw' . ($node->tokenizer->isNext() ? '->addAttributes(%node.array)' : '') . " /* line {$node->startLine} */;", $name, $words ? 'getControlPart(' . \implode(', ', \array_map([$writer, 'formatWord'], $words)) . ')' : 'getControl()'); |
| 136 |
} |
| 137 |
/** |
| 138 |
* <form n:name>, <input n:name>, <select n:name>, <textarea n:name>, <label n:name> and <button n:name> |
| 139 |
*/ |
| 140 |
public function macroNameAttr(MacroNode $node, PhpWriter $writer) |
| 141 |
{ |
| 142 |
$words = $node->tokenizer->fetchWords(); |
| 143 |
if (!$words) { |
| 144 |
throw new CompileException('Missing name in ' . $node->getNotation()); |
| 145 |
} |
| 146 |
$name = \array_shift($words); |
| 147 |
$tagName = \strtolower($node->htmlNode->name); |
| 148 |
$node->empty = $tagName === 'input'; |
| 149 |
$definedHtmlAttributes = \array_keys($node->htmlNode->attrs); |
| 150 |
if (isset($node->htmlNode->macroAttrs['class'])) { |
| 151 |
$definedHtmlAttributes[] = 'class'; |
| 152 |
} |
| 153 |
if ($tagName === 'form') { |
| 154 |
$node->openingCode = $writer->write('<?php $form = $this->global->formsStack[] = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '') . "\$this->global->uiControl[%0.word] /* line {$node->startLine} */; ?>", $name); |
| 155 |
return $writer->write('echo \\Packetery\\Nette\\Bridges\\FormsLatte\\Runtime::renderFormBegin(end($this->global->formsStack), %0.var, false)', \array_fill_keys($definedHtmlAttributes, null)); |
| 156 |
} else { |
| 157 |
$method = $tagName === 'label' ? 'getLabel' : 'getControl'; |
| 158 |
return $writer->write('$ʟ_input = $_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '') . 'end($this->global->formsStack)[%0.word]; echo $ʟ_input->%1.raw' . ($definedHtmlAttributes ? '->addAttributes(%2.var)' : '') . '->attributes()' . " /* line {$node->startLine} */;", $name, $method . 'Part(' . \implode(', ', \array_map([$writer, 'formatWord'], $words)) . ')', \array_fill_keys($definedHtmlAttributes, null)); |
| 159 |
} |
| 160 |
} |
| 161 |
public function macroName(MacroNode $node, PhpWriter $writer) |
| 162 |
{ |
| 163 |
if (!$node->prefix) { |
| 164 |
throw new CompileException("Unknown macro {{$node->name}}, use n:{$node->name} attribute."); |
| 165 |
} elseif ($node->prefix !== MacroNode::PREFIX_NONE) { |
| 166 |
throw new CompileException("Unknown attribute n:{$node->prefix}-{$node->name}, use n:{$node->name} attribute."); |
| 167 |
} |
| 168 |
} |
| 169 |
public function macroNameEnd(MacroNode $node, PhpWriter $writer) |
| 170 |
{ |
| 171 |
$tagName = \strtolower($node->htmlNode->name); |
| 172 |
if ($tagName === 'form') { |
| 173 |
$node->innerContent .= '<?php echo \\Packetery\\Nette\\Bridges\\FormsLatte\\Runtime::renderFormEnd(array_pop($this->global->formsStack), false)' . " /* line {$node->startLine} */; ?>"; |
| 174 |
} elseif ($tagName === 'label') { |
| 175 |
if ($node->htmlNode->empty) { |
| 176 |
$node->innerContent = "<?php echo \$ʟ_input->getLabelPart()->getHtml() /* line {$node->startLine} */; ?>"; |
| 177 |
} |
| 178 |
} elseif ($tagName === 'button') { |
| 179 |
if ($node->htmlNode->empty) { |
| 180 |
$node->innerContent = "<?php echo htmlspecialchars(\$ʟ_input->getCaption()) /* line {$node->startLine} */; ?>"; |
| 181 |
} |
| 182 |
} else { |
| 183 |
// select, textarea |
| 184 |
$node->innerContent = "<?php echo \$ʟ_input->getControl()->getHtml() /* line {$node->startLine} */; ?>"; |
| 185 |
} |
| 186 |
} |
| 187 |
/** |
| 188 |
* {inputError ...} |
| 189 |
*/ |
| 190 |
public function macroInputError(MacroNode $node, PhpWriter $writer) |
| 191 |
{ |
| 192 |
if ($node->modifiers) { |
| 193 |
throw new CompileException('Modifiers are not allowed in ' . $node->getNotation()); |
| 194 |
} |
| 195 |
$name = $node->tokenizer->fetchWord(); |
| 196 |
$node->replaced = \true; |
| 197 |
if (!$name) { |
| 198 |
return $writer->write("echo %escape(\$ʟ_input->getError()) /* line {$node->startLine} */;"); |
| 199 |
} elseif ($name[0] === '$') { |
| 200 |
return $writer->write('$ʟ_input = is_object(%0.word) ? %0.word : end($this->global->formsStack)[%0.word];' . "echo %escape(\$ʟ_input->getError()) /* line {$node->startLine} */;", $name); |
| 201 |
} else { |
| 202 |
return $writer->write("echo %escape(end(\$this->global->formsStack)[%0.word]->getError()) /* line {$node->startLine} */;", $name); |
| 203 |
} |
| 204 |
} |
| 205 |
/** |
| 206 |
* {formPrint [ClassName]} |
| 207 |
*/ |
| 208 |
public function macroFormPrint(MacroNode $node, PhpWriter $writer) |
| 209 |
{ |
| 210 |
$name = $node->tokenizer->fetchWord(); |
| 211 |
if ($name == null) { |
| 212 |
// null or false |
| 213 |
throw new CompileException('Missing form name in ' . $node->getNotation()); |
| 214 |
} |
| 215 |
$node->tokenizer->reset(); |
| 216 |
return $writer->write('\\Packetery\\Nette\\Bridges\\FormsLatte\\Runtime::renderBlueprint(' . ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '') . '$this->global->uiControl[%node.word]); exit;'); |
| 217 |
} |
| 218 |
} |
| 219 |
|