Skip to content

Commit 2c85c21

Browse files
committed
Latte: rewritten Runtime as non-static class
1 parent f846dca commit 2c85c21

19 files changed

+216
-201
lines changed

src/Bridges/FormsLatte/FormsExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public function getTags(): array
3838
public function getProviders(): array
3939
{
4040
return [
41-
'formsStack' => [],
41+
'forms' => new Runtime,
4242
];
4343
}
4444

4545

4646
public function getCacheKey(Latte\Engine $engine): mixed
4747
{
48-
return 1;
48+
return 2;
4949
}
5050
}

src/Bridges/FormsLatte/Nodes/FieldNNameNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private function init(Tag $tag)
6464
$elName = strtolower($el->name);
6565

6666
$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
67-
'echo ($ʟ_elem = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)'
67+
'echo ($ʟ_elem = $this->global->forms->item(%node)'
6868
. ($elName === 'label' ? '->getLabelPart(%node))' : '->getControlPart(%node))')
6969
. ($usedAttributes ? '->addAttributes(%dump)' : '')
7070
. '->attributes() %3.line;',

src/Bridges/FormsLatte/Nodes/FormContainerNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public static function create(Tag $tag): \Generator
4141
public function print(PrintContext $context): string
4242
{
4343
return $context->format(
44-
'$this->global->formsStack[] = $formContainer = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global) %line; '
44+
'$this->global->forms->begin($formContainer = $this->global->forms->item(%node)) %line; '
4545
. '%node '
46-
. 'array_pop($this->global->formsStack); $formContainer = end($this->global->formsStack);'
46+
. '$this->global->forms->end(); $formContainer = $this->global->forms->current();'
4747
. "\n\n",
4848
$this->name,
4949
$this->position,

src/Bridges/FormsLatte/Nodes/FormNNameNode.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,13 @@ public static function create(Tag $tag): \Generator
4242
public function print(PrintContext $context): string
4343
{
4444
return $context->format(
45-
'$form = $this->global->formsStack[] = '
45+
'$this->global->forms->begin($form = '
4646
. ($this->name instanceof StringNode
4747
? '$this->global->uiControl[%node]'
48-
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
49-
. ' %line;'
50-
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
48+
: '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])')
49+
. ') %line;'
5150
. '%node '
52-
. 'array_pop($this->global->formsStack);',
51+
. '$this->global->forms->end();',
5352
$this->name,
5453
$this->position,
5554
$this->content,
@@ -62,15 +61,15 @@ private function init(Tag $tag)
6261
$el = $tag->htmlElement;
6362

6463
$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
65-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %dump, false) %line;',
64+
'echo $this->global->forms->renderFormBegin(%dump, false) %line;',
6665
array_fill_keys(FieldNNameNode::findUsedAttributes($el), null),
6766
$this->position,
6867
)));
6968

7069
$el->content = new Latte\Compiler\Nodes\FragmentNode([
7170
$el->content,
7271
new AuxiliaryNode(fn(PrintContext $context) => $context->format(
73-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) %line;',
72+
'echo $this->global->forms->renderFormEnd(false) %line;',
7473
$this->position,
7574
)),
7675
]);

src/Bridges/FormsLatte/Nodes/FormNode.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,21 @@ public static function create(Tag $tag): \Generator
6363
public function print(PrintContext $context): string
6464
{
6565
return $context->format(
66-
'$form = $this->global->formsStack[] = '
66+
'$this->global->forms->begin($form = '
6767
. ($this->name instanceof StringNode
6868
? '$this->global->uiControl[%node]'
69-
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
70-
. ' %line;'
71-
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
69+
: '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])')
70+
. ') %line;'
7271
. ($this->print
73-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;'
72+
? 'echo $this->global->forms->renderFormBegin(%node) %1.line;'
7473
: '')
7574
. ' %3.node '
7675
. ($this->print
77-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack))'
78-
: 'array_pop($this->global->formsStack)')
79-
. " %4.line;\n\n",
76+
? 'echo $this->global->forms->renderFormEnd()'
77+
: '')
78+
. ' %4.line;'
79+
. '$this->global->forms->end();'
80+
. "\n\n",
8081
$this->name,
8182
$this->position,
8283
$this->attributes,

src/Bridges/FormsLatte/Nodes/FormPrintNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function print(PrintContext $context): string
4747
'Nette\Forms\Blueprint::%raw('
4848
. ($this->name
4949
? 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
50-
: 'end($this->global->formsStack)')
50+
: '$this->global->forms->current()')
5151
. ') %2.line; exit;',
5252
$this->mode === 'formPrint' ? 'latte' : 'dataClass',
5353
$this->name,

src/Bridges/FormsLatte/Nodes/InputErrorNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function create(Tag $tag): static
3737
public function print(PrintContext $context): string
3838
{
3939
return $context->format(
40-
'echo %escape(Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->getError()) %line;',
40+
'echo %escape($this->global->forms->item(%node)->getError()) %line;',
4141
$this->name,
4242
$this->position,
4343
);

src/Bridges/FormsLatte/Nodes/InputNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static function create(Tag $tag): static
4848
public function print(PrintContext $context): string
4949
{
5050
return $context->format(
51-
'echo Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->'
51+
'echo $this->global->forms->item(%node)->'
5252
. ($this->part ? ('getControlPart(%node)') : 'getControl()')
5353
. ($this->attributes->items ? '->addAttributes(%2.node)' : '')
5454
. ' %3.line;',

src/Bridges/FormsLatte/Nodes/LabelNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static function create(Tag $tag): \Generator
6363
public function print(PrintContext $context): string
6464
{
6565
return $context->format(
66-
'echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->'
66+
'echo ($ʟ_label = $this->global->forms->item(%node)->'
6767
. ($this->part ? 'getLabelPart(%node)' : 'getLabel()')
6868
. ')'
6969
. ($this->attributes->items ? '?->addAttributes(%2.node)' : '')

src/Bridges/FormsLatte/Runtime.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Bridges\FormsLatte;
1111

1212
use Nette;
13+
use Nette\Forms\Container;
1314
use Nette\Forms\Form;
1415
use Nette\Utils\Html;
1516

@@ -20,22 +21,16 @@
2021
*/
2122
class Runtime
2223
{
23-
use Nette\StaticClass;
24-
25-
public static function initializeForm(Form $form): void
26-
{
27-
$form->fireRenderEvents();
28-
foreach ($form->getControls() as $control) {
29-
$control->setOption('rendered', false);
30-
}
31-
}
24+
/** @var Container[] */
25+
private array $stack = [];
3226

3327

3428
/**
3529
* Renders form begin.
3630
*/
37-
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
31+
public function renderFormBegin(array $attrs, bool $withTags = true): string
3832
{
33+
$form = $this->current();
3934
$el = $form->getElementPrototype();
4035
$el->action = (string) $el->action;
4136
$el = clone $el;
@@ -51,8 +46,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5146
/**
5247
* Renders form end.
5348
*/
54-
public static function renderFormEnd(Form $form, bool $withTags = true): string
49+
public function renderFormEnd(bool $withTags = true): string
5550
{
51+
$form = $this->current();
5652
$s = '';
5753
if ($form->isMethod('get')) {
5854
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
@@ -75,12 +71,35 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7571
}
7672

7773

78-
public static function item($item, $global): object
74+
public function item($item): object
7975
{
80-
if (is_object($item)) {
81-
return $item;
76+
return is_object($item)
77+
? $item
78+
: $this->current()[$item];
79+
}
80+
81+
82+
public function begin(Container $form): void
83+
{
84+
$this->stack[] = $form;
85+
86+
if ($form instanceof Form) {
87+
$form->fireRenderEvents();
88+
foreach ($form->getControls() as $control) {
89+
$control->setOption('rendered', false);
90+
}
8291
}
83-
$form = end($global->formsStack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
84-
return $form[$item];
92+
}
93+
94+
95+
public function end(): void
96+
{
97+
array_pop($this->stack);
98+
}
99+
100+
101+
public function current(): Container
102+
{
103+
return end($this->stack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
85104
}
86105
}

0 commit comments

Comments
 (0)