diff --git a/src/Bridges/FormsLatte/FormsExtension.php b/src/Bridges/FormsLatte/FormsExtension.php index 3e13a0b88..92dd0b20d 100644 --- a/src/Bridges/FormsLatte/FormsExtension.php +++ b/src/Bridges/FormsLatte/FormsExtension.php @@ -38,7 +38,13 @@ public function getTags(): array public function getProviders(): array { return [ - 'formsStack' => [], + 'forms' => new Runtime, ]; } + + + public function getCacheKey(Latte\Engine $engine): array + { + return ['version' => 2]; + } } diff --git a/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php b/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php index 78ce293a6..bbf3681ea 100644 --- a/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FieldNNameNode.php @@ -64,7 +64,7 @@ private function init(Tag $tag) $elName = strtolower($el->name); $tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format( - 'echo ($ʟ_input = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global))' + 'echo ($ʟ_input = $this->global->forms->item(%node))' . ($elName === 'label' ? '->getLabelPart(%node)' : '->getControlPart(%node)') . ($usedAttributes ? '->addAttributes(%dump)' : '') . '->attributes() %3.line;', diff --git a/src/Bridges/FormsLatte/Nodes/FormContainerNode.php b/src/Bridges/FormsLatte/Nodes/FormContainerNode.php index 36cd2aa9f..9569d8a85 100644 --- a/src/Bridges/FormsLatte/Nodes/FormContainerNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormContainerNode.php @@ -41,9 +41,9 @@ public static function create(Tag $tag): \Generator public function print(PrintContext $context): string { return $context->format( - '$this->global->formsStack[] = $formContainer = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global) %line; ' + '$this->global->forms->begin($formContainer = $this->global->forms->item(%node)) %line; ' . '%node ' - . 'array_pop($this->global->formsStack); $formContainer = end($this->global->formsStack);' + . '$this->global->forms->end(); $formContainer = $this->global->forms->current();' . "\n\n", $this->name, $this->position, diff --git a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php index 75119298e..b46ceb866 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNNameNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNNameNode.php @@ -42,14 +42,13 @@ public static function create(Tag $tag): \Generator public function print(PrintContext $context): string { return $context->format( - '$form = $this->global->formsStack[] = ' + '$this->global->forms->begin($form = ' . ($this->name instanceof StringNode ? '$this->global->uiControl[%node]' - : 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]') - . ' %line;' - . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);' + : '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])') + . ') %line;' . '%node ' - . 'array_pop($this->global->formsStack);', + . '$this->global->forms->end();', $this->name, $this->position, $this->content, @@ -62,7 +61,7 @@ private function init(Tag $tag) $el = $tag->htmlElement; $tag->replaceNAttribute(new AuxiliaryNode(fn(PrintContext $context) => $context->format( - 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %dump, false) %line;', + 'echo $this->global->forms->renderFormBegin(%dump, false) %line;', array_fill_keys(FieldNNameNode::findUsedAttributes($el), null), $this->position, ))); @@ -70,7 +69,7 @@ private function init(Tag $tag) $el->content = new Latte\Compiler\Nodes\FragmentNode([ $el->content, new AuxiliaryNode(fn(PrintContext $context) => $context->format( - 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) %line;', + 'echo $this->global->forms->renderFormEnd(false) %line;', $this->position, )), ]); diff --git a/src/Bridges/FormsLatte/Nodes/FormNode.php b/src/Bridges/FormsLatte/Nodes/FormNode.php index 2146a18a3..b98d6e3f9 100644 --- a/src/Bridges/FormsLatte/Nodes/FormNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormNode.php @@ -61,20 +61,21 @@ public static function create(Tag $tag): \Generator public function print(PrintContext $context): string { return $context->format( - '$form = $this->global->formsStack[] = ' + '$this->global->forms->begin($form = ' . ($this->name instanceof StringNode ? '$this->global->uiControl[%node]' - : 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]') - . ' %line;' - . 'Nette\Bridges\FormsLatte\Runtime::initializeForm($form);' + : '(is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp])') + . ') %line;' . ($this->print - ? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %node) %1.line;' + ? 'echo $this->global->forms->renderFormBegin(%node) %1.line;' : '') . ' %3.node ' . ($this->print - ? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(array_pop($this->global->formsStack))' - : 'array_pop($this->global->formsStack)') - . " %4.line;\n\n", + ? 'echo $this->global->forms->renderFormEnd()' + : '') + . ' %4.line;' + . '$this->global->forms->end();' + . "\n\n", $this->name, $this->position, $this->attributes, diff --git a/src/Bridges/FormsLatte/Nodes/FormPrintNode.php b/src/Bridges/FormsLatte/Nodes/FormPrintNode.php index 427beedbb..eb25d4b2c 100644 --- a/src/Bridges/FormsLatte/Nodes/FormPrintNode.php +++ b/src/Bridges/FormsLatte/Nodes/FormPrintNode.php @@ -39,10 +39,10 @@ public static function create(Tag $tag): static public function print(PrintContext $context): string { return $context->format( - 'Nette\Bridges\FormsLatte\Runtime::render%raw(' + '$this->global->forms->render%raw(' . ($this->name ? 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]' - : 'end($this->global->formsStack)') + : '$this->global->forms->current()') . ') %2.line; exit;', $this->mode, $this->name, diff --git a/src/Bridges/FormsLatte/Nodes/InputErrorNode.php b/src/Bridges/FormsLatte/Nodes/InputErrorNode.php index 014e16d69..c30a6bba4 100644 --- a/src/Bridges/FormsLatte/Nodes/InputErrorNode.php +++ b/src/Bridges/FormsLatte/Nodes/InputErrorNode.php @@ -37,7 +37,7 @@ public static function create(Tag $tag): static public function print(PrintContext $context): string { return $context->format( - 'echo %escape(Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->getError()) %line;', + 'echo %escape($this->global->forms->item(%node)->getError()) %line;', $this->name, $this->position, ); diff --git a/src/Bridges/FormsLatte/Nodes/InputNode.php b/src/Bridges/FormsLatte/Nodes/InputNode.php index d9f6841eb..be5a4e3b8 100644 --- a/src/Bridges/FormsLatte/Nodes/InputNode.php +++ b/src/Bridges/FormsLatte/Nodes/InputNode.php @@ -48,7 +48,7 @@ public static function create(Tag $tag): static public function print(PrintContext $context): string { return $context->format( - 'echo Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->' + 'echo $this->global->forms->item(%node)->' . ($this->part ? ('getControlPart(%node)') : 'getControl()') . ($this->attributes->items ? '->addAttributes(%2.node)' : '') . ' %3.line;', diff --git a/src/Bridges/FormsLatte/Nodes/LabelNode.php b/src/Bridges/FormsLatte/Nodes/LabelNode.php index cf1316c59..82990d9e0 100644 --- a/src/Bridges/FormsLatte/Nodes/LabelNode.php +++ b/src/Bridges/FormsLatte/Nodes/LabelNode.php @@ -63,7 +63,7 @@ public static function create(Tag $tag): \Generator public function print(PrintContext $context): string { return $context->format( - 'echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item(%node, $this->global)->' + 'echo ($ʟ_label = $this->global->forms->item(%node)->' . ($this->part ? 'getLabelPart(%node)' : 'getLabel()') . ')' . ($this->attributes->items ? '?->addAttributes(%2.node)' : '') diff --git a/src/Bridges/FormsLatte/Runtime.php b/src/Bridges/FormsLatte/Runtime.php index b9979c899..b1c91f584 100644 --- a/src/Bridges/FormsLatte/Runtime.php +++ b/src/Bridges/FormsLatte/Runtime.php @@ -11,6 +11,7 @@ use Latte; use Nette; +use Nette\Forms\Container; use Nette\Forms\Form; use Nette\Utils\Html; @@ -21,22 +22,16 @@ */ class Runtime { - use Nette\StaticClass; - - public static function initializeForm(Form $form): void - { - $form->fireRenderEvents(); - foreach ($form->getControls() as $control) { - $control->setOption('rendered', false); - } - } + /** @var Container[] */ + private array $stack = []; /** * Renders form begin. */ - public static function renderFormBegin(Form $form, array $attrs, bool $withTags = true): string + public function renderFormBegin(array $attrs, bool $withTags = true): string { + $form = $this->current(); $el = $form->getElementPrototype(); $el->action = (string) $el->action; $el = clone $el; @@ -52,8 +47,9 @@ public static function renderFormBegin(Form $form, array $attrs, bool $withTags /** * Renders form end. */ - public static function renderFormEnd(Form $form, bool $withTags = true): string + public function renderFormEnd(bool $withTags = true): string { + $form = $this->current(); $s = ''; if ($form->isMethod('get')) { foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) { @@ -79,7 +75,7 @@ public static function renderFormEnd(Form $form, bool $withTags = true): string /** * Generates blueprint of form. */ - public static function renderFormPrint(Form $form): void + public function renderFormPrint(Form $form): void { $blueprint = class_exists(Latte\Runtime\Blueprint::class) ? new Latte\Runtime\Blueprint @@ -94,7 +90,7 @@ public static function renderFormPrint(Form $form): void /** * Generates blueprint of form data class. */ - public static function renderFormClassPrint(Form $form): void + public function renderFormClassPrint(Form $form): void { $blueprint = class_exists(Latte\Runtime\Blueprint::class) ? new Latte\Runtime\Blueprint @@ -110,12 +106,35 @@ public static function renderFormClassPrint(Form $form): void } - public static function item($item, $global): object + public function item($item): object { - if (is_object($item)) { - return $item; + return is_object($item) + ? $item + : $this->current()[$item]; + } + + + public function begin(Container $form): void + { + $this->stack[] = $form; + + if ($form instanceof Form) { + $form->fireRenderEvents(); + foreach ($form->getControls() as $control) { + $control->setOption('rendered', false); + } } - $form = end($global->formsStack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or
', - Runtime::renderFormEnd($form), + $runtime->renderFormEnd(), ); diff --git a/tests/Forms.Latte3/Runtime.renderFormClassPrint.phpt b/tests/Forms.Latte3/Runtime.renderFormClassPrint.phpt index 4a0b3df07..197003ba6 100644 --- a/tests/Forms.Latte3/Runtime.renderFormClassPrint.phpt +++ b/tests/Forms.Latte3/Runtime.renderFormClassPrint.phpt @@ -12,7 +12,7 @@ $form = new Form('signForm'); $form->addText('name')->setRequired(); ob_start(); -Nette\Bridges\FormsLatte\Runtime::renderFormClassPrint($form); +(new Nette\Bridges\FormsLatte\Runtime)->renderFormClassPrint($form); $res = ob_get_clean(); Assert::match( diff --git a/tests/Forms.Latte3/Runtime.renderFormPrint.phpt b/tests/Forms.Latte3/Runtime.renderFormPrint.phpt index 36fe1b427..6616e36dc 100644 --- a/tests/Forms.Latte3/Runtime.renderFormPrint.phpt +++ b/tests/Forms.Latte3/Runtime.renderFormPrint.phpt @@ -14,7 +14,7 @@ $form->addText('name')->setRequired('Enter your name'); $form->addSubmit('submit', 'Send'); ob_start(); -Nette\Bridges\FormsLatte\Runtime::renderFormPrint($form); +(new Nette\Bridges\FormsLatte\Runtime)->renderFormPrint($form); $res = ob_get_clean(); Assert::match( diff --git a/tests/Forms.Latte3/expected/forms.button.php b/tests/Forms.Latte3/expected/forms.button.php index 39b37e7dd..8af2b21b2 100644 --- a/tests/Forms.Latte3/expected/forms.button.php +++ b/tests/Forms.Latte3/expected/forms.button.php @@ -1,28 +1,27 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; - Nette\Bridges\FormsLatte\Runtime::initializeForm($form); + $this->global->forms->begin($form = $this->global->uiControl['myForm']) /* line %d% */; echo ' '; - array_pop($this->global->formsStack); + $this->global->forms->end(); %A% diff --git a/tests/Forms.Latte3/expected/forms.formContainer.php b/tests/Forms.Latte3/expected/forms.formContainer.php index 893cc8e3a..6bd65b4e0 100644 --- a/tests/Forms.Latte3/expected/forms.formContainer.php +++ b/tests/Forms.Latte3/expected/forms.formContainer.php @@ -1,46 +1,45 @@ global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */; - Nette\Bridges\FormsLatte\Runtime::initializeForm($form); - echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, []) /* line %d% */; + $this->global->forms->begin($form = $this->global->uiControl['myForm']) /* line %d% */; + echo $this->global->forms->renderFormBegin([]) /* line %d% */; echo ''; - echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item('input1', $this->global)->getLabel()) /* line %d% */; + echo ($ʟ_label = $this->global->forms->item('input1')->getLabel()) /* line %d% */; echo ' | '; - echo Nette\Bridges\FormsLatte\Runtime::item('input1', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input1')->getControl() /* line %d% */; echo ' |
---|---|
'; - echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item('input2', $this->global)->getLabel()) /* line %d% */; + echo ($ʟ_label = $this->global->forms->item('input2')->getLabel()) /* line %d% */; echo ' | '; - echo Nette\Bridges\FormsLatte\Runtime::item('input2', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input2')->getControl() /* line %d% */; echo ' |
'; - echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item('input3', $this->global)->getLabel()) /* line %d% */; + echo ($ʟ_label = $this->global->forms->item('input3')->getLabel()) /* line %d% */; echo ' | '; - echo Nette\Bridges\FormsLatte\Runtime::item('input3', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input3')->getControl() /* line %d% */; echo ' |
Checkboxes |
';
- $this->global->formsStack[] = $formContainer = Nette\Bridges\FormsLatte\Runtime::item('cont2', $this->global) /* line %d% */;
+ $this->global->forms->begin($formContainer = $this->global->forms->item('cont2')) /* line %d% */;
echo '
|
'; - echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item('input7', $this->global)->getLabel()) /* line %d% */; + echo ($ʟ_label = $this->global->forms->item('input7')->getLabel()) /* line %d% */; echo ' | '; - echo Nette\Bridges\FormsLatte\Runtime::item('input7', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input7')->getControl() /* line %d% */; echo ' |
Items | @@ -73,12 +72,12 @@ $items = [1, 2, 3] /* line %d% */; foreach ($items as $item) /* line %d% */ { if (!isset($formContainer[$item])) /* line %d% */ continue; - $this->global->formsStack[] = $formContainer = Nette\Bridges\FormsLatte\Runtime::item($item, $this->global) /* line %d% */; + $this->global->forms->begin($formContainer = $this->global->forms->item($item)) /* line %d% */; echo ' '; - echo Nette\Bridges\FormsLatte\Runtime::item('input', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input')->getControl() /* line %d% */; echo "\n"; - array_pop($this->global->formsStack); - $formContainer = end($this->global->formsStack); + $this->global->forms->end(); + $formContainer = $this->global->forms->current(); } @@ -86,18 +85,19 @@ echo ' |
'; - echo ($ʟ_label = Nette\Bridges\FormsLatte\Runtime::item('input8', $this->global)->getLabel()) /* line %d% */; + echo ($ʟ_label = $this->global->forms->item('input8')->getLabel()) /* line %d% */; echo ' | '; - echo Nette\Bridges\FormsLatte\Runtime::item('input8', $this->global)->getControl() /* line %d% */; + echo $this->global->forms->item('input8')->getControl() /* line %d% */; echo ' |