Skip to content

Commit 96e3a8e

Browse files
committed
Latte: Runtime for v2 renamed to Runtime2
1 parent 03ae81c commit 96e3a8e

7 files changed

Lines changed: 137 additions & 26 deletions

File tree

src/Bridges/FormsLatte/FormMacros.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class FormMacros extends MacroSet
3131
public static function install(Latte\Compiler $compiler): void
3232
{
3333
$me = new static($compiler);
34-
$me->addMacro('form', [$me, 'macroForm'], 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));');
34+
$me->addMacro('form', [$me, 'macroForm'], 'echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));');
3535
$me->addMacro('formContext', [$me, 'macroFormContext'], 'array_pop($this->global->formsStack);');
3636
$me->addMacro('formContainer', [$me, 'macroFormContainer'], 'array_pop($this->global->formsStack); $formContainer = end($this->global->formsStack)');
3737
$me->addMacro('label', [$me, 'macroLabel'], [$me, 'macroLabelEnd'], null, self::AUTO_EMPTY);
@@ -67,7 +67,7 @@ public function macroForm(MacroNode $node, PhpWriter $writer)
6767
$node->replaced = true;
6868
$node->tokenizer->reset();
6969
return $writer->write(
70-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = '
70+
'echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = '
7171
. ($name[0] === '$'
7272
? 'is_object($ʟ_tmp = %node.word) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
7373
: '$this->global->uiControl[%node.word]')
@@ -230,7 +230,7 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer)
230230
$name,
231231
);
232232
return $writer->write(
233-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %0.var, false)',
233+
'echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), %0.var, false)',
234234
array_fill_keys($definedHtmlAttributes, null),
235235
);
236236
} else {
@@ -265,7 +265,7 @@ public function macroNameEnd(MacroNode $node, PhpWriter $writer)
265265
{
266266
$tagName = strtolower($node->htmlNode->name);
267267
if ($tagName === 'form') {
268-
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false)'
268+
$node->innerContent .= '<?php echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false)'
269269
. " /* line $node->startLine */; ?>";
270270
} elseif ($tagName === 'label') {
271271
if ($node->htmlNode->empty) {
@@ -319,7 +319,7 @@ public function macroFormPrint(MacroNode $node, PhpWriter $writer)
319319

320320
$node->tokenizer->reset();
321321
return $writer->write(
322-
'Nette\Bridges\FormsLatte\Runtime::render' . $node->name . '('
322+
'Nette\Bridges\FormsLatte\Runtime2::render' . $node->name . '('
323323
. ($name[0] === '$' ? 'is_object(%node.word) ? %node.word : ' : '')
324324
. '$this->global->uiControl[%node.word]); exit;',
325325
);

src/Bridges/FormsLatte/Runtime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
/**
19-
* Runtime helpers for Latte v2 & v3.
19+
* Runtime helpers for Latte v3.
2020
* @internal
2121
*/
2222
class Runtime
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Bridges\FormsLatte;
11+
12+
use Latte;
13+
use Nette;
14+
use Nette\Forms\Form;
15+
use Nette\Utils\Html;
16+
17+
18+
/**
19+
* Runtime helpers for Latte v2.
20+
* @internal
21+
*/
22+
class Runtime2
23+
{
24+
use Nette\StaticClass;
25+
26+
/**
27+
* Renders form begin.
28+
*/
29+
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
30+
{
31+
$form->fireRenderEvents();
32+
foreach ($form->getControls() as $control) {
33+
$control->setOption('rendered', false);
34+
}
35+
36+
$el = $form->getElementPrototype();
37+
$el->action = (string) $el->action;
38+
$el = clone $el;
39+
if ($form->isMethod('get')) {
40+
$el->action = preg_replace('~\?[^#]*~', '', $el->action, 1);
41+
}
42+
43+
$el->addAttributes($attrs);
44+
return $withTags ? $el->startTag() : $el->attributes();
45+
}
46+
47+
48+
/**
49+
* Renders form end.
50+
*/
51+
public static function renderFormEnd(Form $form, bool $withTags = true): string
52+
{
53+
$s = '';
54+
if ($form->isMethod('get')) {
55+
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
56+
$parts = explode('=', $param, 2);
57+
$name = urldecode($parts[0]);
58+
$prefix = explode('[', $name, 2)[0];
59+
if (!isset($form[$prefix])) {
60+
$s .= Html::el('input', ['type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])]);
61+
}
62+
}
63+
}
64+
65+
foreach ($form->getControls() as $control) {
66+
if ($control->getOption('type') === 'hidden' && !$control->getOption('rendered')) {
67+
$s .= $control->getControl();
68+
}
69+
}
70+
71+
if (iterator_count($form->getComponents(true, Nette\Forms\Controls\TextInput::class)) < 2) {
72+
$s .= "<!--[if IE]><input type=IEbug disabled style=\"display:none\"><![endif]-->\n";
73+
}
74+
75+
return $s . ($withTags ? $form->getElementPrototype()->endTag() . "\n" : '');
76+
}
77+
78+
79+
/**
80+
* Generates blueprint of form.
81+
*/
82+
public static function renderFormPrint(Form $form): void
83+
{
84+
$blueprint = class_exists(Latte\Runtime\Blueprint::class)
85+
? new Latte\Runtime\Blueprint
86+
: new Latte\Essential\Blueprint;
87+
$end = $blueprint->printCanvas();
88+
$blueprint->printHeader('Form ' . $form->getName());
89+
$blueprint->printCode((new Nette\Forms\Rendering\LatteRenderer)->render($form), 'latte');
90+
echo $end;
91+
}
92+
93+
94+
/**
95+
* Generates blueprint of form data class.
96+
*/
97+
public static function renderFormClassPrint(Form $form): void
98+
{
99+
$blueprint = class_exists(Latte\Runtime\Blueprint::class)
100+
? new Latte\Runtime\Blueprint
101+
: new Latte\Essential\Blueprint;
102+
$end = $blueprint->printCanvas();
103+
$blueprint->printHeader('Form Data Class ' . $form->getName());
104+
$generator = new Nette\Forms\Rendering\DataClassGenerator;
105+
$blueprint->printCode($generator->generateCode($form));
106+
$generator->propertyPromotion = true;
107+
$blueprint->printCode($generator->generateCode($form));
108+
109+
echo $end;
110+
}
111+
}

tests/Forms.Latte2/expected/FormMacros.button.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
%A%
33
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 1 */;
44
echo '<form';
5-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
5+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), [], false);
66
echo '>
77
<button';
88
$ʟ_input = $_input = end($this->global->formsStack)["send"];
@@ -23,7 +23,7 @@
2323
echo LR\Filters::escapeHtmlText($ʟ_input->getCaption()) /* line 8 */;
2424
echo '</button>
2525
';
26-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line 1 */;
26+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false) /* line 1 */;
2727
echo '</form>
2828
';
2929
%A%

tests/Forms.Latte2/expected/FormMacros.formContainer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
3+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
44
echo '
55
<table>
66
<tr>
@@ -94,6 +94,6 @@
9494
</tr>
9595
</table>
9696
';
97-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
97+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));
9898
echo "\n";
9999
%A%

tests/Forms.Latte2/expected/FormMacros.forms.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
3+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], ['id' => 'myForm', 'class'=>"ajax"]) /* line 1 */;
44
echo "\n";
55
$iterations = 0;
66
foreach (['id', 'username', 'select', 'area', 'send'] as $name) /* line 2 */ {
@@ -71,17 +71,17 @@
7171
if ($ʟ_label = end($this->global->formsStack)["my"]->getLabel()) echo $ʟ_label;
7272
echo end($this->global->formsStack)["my"]->getControl() /* line 23 */;
7373
echo "\n";
74-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
74+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));
7575
echo '
7676
7777
7878
';
79-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 27 */;
80-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
79+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 27 */;
80+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));
8181
echo '
8282
8383
';
84-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 29 */;
84+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 29 */;
8585
echo "\n";
8686
$iterations = 0;
8787
foreach ($form['sex']->items as $key => $label) /* line 31 */ {
@@ -199,14 +199,14 @@
199199
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 58 */;
200200
if (1) /* line 58 */ {
201201
echo '<form id="myForm" class="ajax"';
202-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false);
202+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), ['id' => null, 'class' => null], false);
203203
echo '>
204204
<input';
205205
$ʟ_input = $_input = end($this->global->formsStack)["username"];
206206
echo $ʟ_input->getControlPart()->attributes() /* line 59 */;
207207
echo '>
208208
';
209-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line 58 */;
209+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false) /* line 58 */;
210210
echo '</form>
211211
';
212212
}
@@ -216,29 +216,29 @@
216216
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 63 */;
217217
echo '<form';
218218
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 63 */;
219-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), ['class' => null], false);
219+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), ['class' => null], false);
220220
echo '>
221221
<input';
222222
$ʟ_input = $_input = end($this->global->formsStack)["username"];
223223
echo $ʟ_input->getControlPart()->addAttributes(['class' => null])->attributes() /* line 64 */;
224224
echo ($ʟ_tmp = array_filter(['nclass'])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line 64 */;
225225
echo '>
226226
';
227-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line 63 */;
227+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false) /* line 63 */;
228228
echo '</form>
229229
230230
231231
';
232232
$form = $this->global->formsStack[] = is_object($ʟ_tmp = $this->global->uiControl['myForm']) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp] /* line 68 */;
233233
echo '<FORM';
234-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
234+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), [], false);
235235
echo '>
236236
<input';
237237
$ʟ_input = $_input = end($this->global->formsStack)["username"];
238238
echo $ʟ_input->getControlPart()->attributes() /* line 69 */;
239239
echo '>
240240
';
241-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line 68 */;
241+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false) /* line 68 */;
242242
echo '</FORM>
243243
244244
@@ -267,7 +267,7 @@
267267
echo $ʟ_input->getControl()->getHtml() /* line 79 */;
268268
echo '</select>
269269
';
270-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
270+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));
271271
echo '
272272
273273
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php
22
%A%
3-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
4-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack));
3+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin($form = $this->global->formsStack[] = $this->global->uiControl["myForm"], []) /* line 1 */;
4+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack));
55
echo '
66
77
';
88
$form = $this->global->formsStack[] = $this->global->uiControl["myForm"] /* line 3 */;
99
echo '<form';
10-
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false);
10+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormBegin(end($this->global->formsStack), [], false);
1111
echo '>
1212
';
13-
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack), false) /* line 3 */;
13+
echo Nette\Bridges\FormsLatte\Runtime2::renderFormEnd(array_pop($this->global->formsStack), false) /* line 3 */;
1414
echo '</form>
1515
';
1616
%A%

0 commit comments

Comments
 (0)