Skip to content

Commit a155f3a

Browse files
committed
Latte: rewritten Runtime as non-static class
1 parent 8f507e2 commit a155f3a

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
@@ -39,13 +39,13 @@ public function getTags(): array
3939
public function getProviders(): array
4040
{
4141
return [
42-
'formsStack' => [],
42+
'forms' => new Runtime,
4343
];
4444
}
4545

4646

4747
public function getCacheKey(Latte\Engine $engine): mixed
4848
{
49-
return 1;
49+
return 2;
5050
}
5151
}

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
use function end, explode, is_object, parse_url, preg_replace, preg_split, urldecode;
@@ -22,22 +22,16 @@
2222
*/
2323
class Runtime
2424
{
25-
use Nette\StaticClass;
26-
27-
public static function initializeForm(Form $form): void
28-
{
29-
$form->fireRenderEvents();
30-
foreach ($form->getControls() as $control) {
31-
$control->setOption('rendered', false);
32-
}
33-
}
25+
/** @var Container[] */
26+
private array $stack = [];
3427

3528

3629
/**
3730
* Renders form begin.
3831
*/
39-
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
32+
public function renderFormBegin(array $attrs, bool $withTags = true): string
4033
{
34+
$form = $this->current();
4135
$el = $form->getElementPrototype();
4236
$el->action = (string) $el->action;
4337
$el = clone $el;
@@ -53,8 +47,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
5347
/**
5448
* Renders form end.
5549
*/
56-
public static function renderFormEnd(Form $form, bool $withTags = true): string
50+
public function renderFormEnd(bool $withTags = true): string
5751
{
52+
$form = $this->current();
5853
$s = '';
5954
if ($form->isMethod('get')) {
6055
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
@@ -77,12 +72,35 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7772
}
7873

7974

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

0 commit comments

Comments
 (0)