Skip to content

Commit f67e049

Browse files
committed
Latte: rewritten Runtime as non-static class
1 parent 2287630 commit f67e049

19 files changed

+215
-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): void
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): void
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
@@ -61,20 +61,21 @@ public static function create(Tag $tag): \Generator
6161
public function print(PrintContext $context): string
6262
{
6363
return $context->format(
64-
'$form = $this->global->formsStack[] = '
64+
'$this->global->forms->begin($form = '
6565
. ($this->name instanceof StringNode
6666
? '$this->global->uiControl[%node]'
67-
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
68-
. ' %line;'
69-
. 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);'
67+
: '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])')
68+
. ') %line;'
7069
. ($this->print
71-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;'
70+
? 'echo $this->global->forms->renderFormBegin(%node) %1.line;'
7271
: '')
7372
. ' %3.node '
7473
. ($this->print
75-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack))'
76-
: 'array_pop($this->global->formsStack)')
77-
. " %4.line;\n\n",
74+
? 'echo $this->global->forms->renderFormEnd()'
75+
: '')
76+
. ' %4.line;'
77+
. '$this->global->forms->end();'
78+
. "\n\n",
7879
$this->name,
7980
$this->position,
8081
$this->attributes,

src/Bridges/FormsLatte/Nodes/FormPrintNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function print(PrintContext $context): string
4242
'Nette\Forms\Blueprint::%raw('
4343
. ($this->name
4444
? 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]'
45-
: 'end($this->global->formsStack)')
45+
: '$this->global->forms->current()')
4646
. ') %2.line; exit;',
4747
$this->mode === 'formPrint' ? 'latte' : 'dataClass',
4848
$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 & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Nette\Bridges\FormsLatte;
1111

12-
use Nette;
12+
use Nette\Forms\Container;
1313
use Nette\Forms\Form;
1414
use Nette\Utils\Html;
1515

@@ -20,22 +20,16 @@
2020
*/
2121
class Runtime
2222
{
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-
}
23+
/** @var Container[] */
24+
private array $stack = [];
3225

3326

3427
/**
3528
* Renders form begin.
3629
*/
37-
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
30+
public function renderFormBegin(array $attrs, bool $withTags = true): string
3831
{
32+
$form = $this->current();
3933
$el = $form->getElementPrototype();
4034
$el->action = (string) $el->action;
4135
$el = clone $el;
@@ -51,8 +45,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5145
/**
5246
* Renders form end.
5347
*/
54-
public static function renderFormEnd(Form $form, bool $withTags = true): string
48+
public function renderFormEnd(bool $withTags = true): string
5549
{
50+
$form = $this->current();
5651
$s = '';
5752
if ($form->isMethod('get')) {
5853
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
@@ -75,12 +70,35 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7570
}
7671

7772

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

0 commit comments

Comments
 (0)