Skip to content

Commit 8b7ba7a

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

21 files changed

+219
-181
lines changed

src/Bridges/FormsLatte/FormsExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public function getTags(): array
3838
public function getProviders(): array
3939
{
4040
return [
41-
'formsStack' => [],
41+
'forms' => new Runtime,
4242
];
4343
}
44+
45+
46+
public function getCacheKey(Latte\Engine $engine): array
47+
{
48+
return ['version' => 2];
49+
}
4450
}

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 ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global))'
67+
'echo ($ʟ_input = $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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +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;'
48+
: '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])')
49+
. ') %line;'
5050
. '%node '
51-
. 'array_pop($this->global->formsStack);',
51+
. '$this->global->forms->end();',
5252
$this->name,
5353
$this->position,
5454
$this->content,
@@ -61,15 +61,15 @@ private function init(Tag $tag)
6161
$el = $tag->htmlElement;
6262

6363
$tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format(
64-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %dump, false) %line;',
64+
'echo $this->global->forms->renderFormBegin(%dump, false) %line;',
6565
array_fill_keys(FieldNNameNode::findUsedAttributes($el), null),
6666
$this->position,
6767
)));
6868

6969
$el->content = new Latte\Compiler\Nodes\FragmentNode([
7070
$el->content,
7171
new AuxiliaryNode(fn(PrintContext $context) => $context->format(
72-
'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) %line;',
72+
'echo $this->global->forms->renderFormEnd(false) %line;',
7373
$this->position,
7474
)),
7575
]);

src/Bridges/FormsLatte/Nodes/FormNode.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +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;'
67+
: '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])')
68+
. ') %line;'
6969
. ($this->print
70-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;'
70+
? 'echo $this->global->forms->renderFormBegin(%node) %1.line;'
7171
: '')
7272
. ' %3.node '
7373
. ($this->print
74-
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack))'
75-
: 'array_pop($this->global->formsStack)')
76-
. " %4.line;\n\n",
74+
? 'echo $this->global->forms->renderFormEnd()'
75+
: '')
76+
. ' %4.line;'
77+
. '$this->global->forms->end();'
78+
. "\n\n",
7779
$this->name,
7880
$this->position,
7981
$this->attributes,

src/Bridges/FormsLatte/Nodes/FormPrintNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public static function create(Tag $tag): static
3939
public function print(PrintContext $context): string
4040
{
4141
return $context->format(
42-
'Nette\Bridges\FormsLatte\Runtime::render%raw('
42+
'$this->global->forms->render%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,
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: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Latte;
1313
use Nette;
14+
use Nette\Forms\Container;
1415
use Nette\Forms\Form;
1516
use Nette\Utils\Html;
1617

@@ -21,13 +22,16 @@
2122
*/
2223
class Runtime
2324
{
24-
use Nette\StaticClass;
25+
/** @var Container[] */
26+
private array $stack = [];
27+
2528

2629
/**
2730
* Renders form begin.
2831
*/
29-
public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string
32+
public function renderFormBegin(array $attrs, bool $withTags = true): string
3033
{
34+
$form = $this->current();
3135
$form->fireRenderEvents();
3236
foreach ($form->getControls() as $control) {
3337
$control->setOption('rendered', false);
@@ -48,8 +52,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags
4852
/**
4953
* Renders form end.
5054
*/
51-
public static function renderFormEnd(Form $form, bool $withTags = true): string
55+
public function renderFormEnd(bool $withTags = true): string
5256
{
57+
$form = $this->current();
5358
$s = '';
5459
if ($form->isMethod('get')) {
5560
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
@@ -79,7 +84,7 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string
7984
/**
8085
* Generates blueprint of form.
8186
*/
82-
public static function renderFormPrint(Form $form): void
87+
public function renderFormPrint(Form $form): void
8388
{
8489
$blueprint = class_exists(Latte\Runtime\Blueprint::class)
8590
? new Latte\Runtime\Blueprint
@@ -94,7 +99,7 @@ public static function renderFormPrint(Form $form): void
9499
/**
95100
* Generates blueprint of form data class.
96101
*/
97-
public static function renderFormClassPrint(Form $form): void
102+
public function renderFormClassPrint(Form $form): void
98103
{
99104
$blueprint = class_exists(Latte\Runtime\Blueprint::class)
100105
? new Latte\Runtime\Blueprint
@@ -110,12 +115,28 @@ public static function renderFormClassPrint(Form $form): void
110115
}
111116

112117

113-
public static function item($item, $global): object
118+
public function item($item): object
114119
{
115-
if (is_object($item)) {
116-
return $item;
117-
}
118-
$form = end($global->formsStack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
119-
return $form[$item];
120+
return is_object($item)
121+
? $item
122+
: $this->current()[$item];
123+
}
124+
125+
126+
public function begin(Container $form): void
127+
{
128+
$this->stack[] = $form;
129+
}
130+
131+
132+
public function end(): void
133+
{
134+
array_pop($this->stack);
135+
}
136+
137+
138+
public function current(): Container
139+
{
140+
return end($this->stack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
120141
}
121142
}

0 commit comments

Comments
 (0)