Skip to content

Commit

Permalink
Latte: rewritten Runtime as non-static class
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 27, 2022
1 parent 6130c94 commit ee771d7
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 181 deletions.
8 changes: 7 additions & 1 deletion src/Bridges/FormsLatte/FormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/FieldNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/FormsLatte/Nodes/FormContainerNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +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;'
: '(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,
Expand All @@ -61,15 +61,15 @@ 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,
)));

$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,
)),
]);
Expand Down
16 changes: 9 additions & 7 deletions src/Bridges/FormsLatte/Nodes/FormNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +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;'
: '(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,
Expand Down
4 changes: 2 additions & 2 deletions src/Bridges/FormsLatte/Nodes/FormPrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/InputErrorNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/InputNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;',
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/Nodes/LabelNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)' : '')
Expand Down
43 changes: 32 additions & 11 deletions src/Bridges/FormsLatte/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Latte;
use Nette;
use Nette\Forms\Container;
use Nette\Forms\Form;
use Nette\Utils\Html;

Expand All @@ -21,13 +22,16 @@
*/
class Runtime
{
use Nette\StaticClass;
/** @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();
$form->fireRenderEvents();
foreach ($form->getControls() as $control) {
$control->setOption('rendered', false);
Expand All @@ -48,8 +52,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) {
Expand Down Expand Up @@ -79,7 +84,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
Expand All @@ -94,7 +99,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
Expand All @@ -110,12 +115,28 @@ 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;
}
$form = end($global->formsStack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
return $form[$item];
return is_object($item)
? $item
: $this->current()[$item];
}


public function current(): Container
{
return end($this->stack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
}


public function begin(Container $form): void
{
$this->stack[] = $form;
}


public function end(): void
{
array_pop($this->stack);
}
}
7 changes: 5 additions & 2 deletions tests/Forms.Latte3/Runtime.get.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ $form->addText('arg1');
$form->addText('arg2');
$form->setAction('http://example.com/?do=foo-submit&arg0=1&arg1=1&arg2[x]=1#toc');

$runtime = new Runtime;
$runtime->begin($form);

Assert::same(
'<form action="http://example.com/#toc" method="get">',
Runtime::renderFormBegin($form, []),
$runtime->renderFormBegin([]),
);

Assert::match(
'<input type="hidden" name="do" value="foo-submit"><input type="hidden" name="arg0" value="1"></form>',
Runtime::renderFormEnd($form),
$runtime->renderFormEnd(),
);
2 changes: 1 addition & 1 deletion tests/Forms.Latte3/Runtime.renderFormClassPrint.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms.Latte3/Runtime.renderFormPrint.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 7 additions & 7 deletions tests/Forms.Latte3/expected/forms.button.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php
%A%
$form = $this->global->formsStack[] = $this->global->uiControl['myForm'] /* line %d% */;
$this->global->forms->begin($form = $this->global->uiControl['myForm']) /* line %d% */;
echo '<form';
echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), [], false) /* line %d% */;
echo $this->global->forms->renderFormBegin([], false) /* line %d% */;
echo '>
<button';
echo ($薀_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($薀_input = $this->global->forms->item('send'))->getControlPart()->attributes() /* line %d% */;
echo '>
description of button
</button>
<button';
echo ($薀_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($薀_input = $this->global->forms->item('send'))->getControlPart()->attributes() /* line %d% */;
echo '></button>
<button';
echo ($薀_input = Nette\Bridges\FormsLatte\Runtime::item('send', $this->global))->getControlPart()->attributes() /* line %d% */;
echo ($薀_input = $this->global->forms->item('send'))->getControlPart()->attributes() /* line %d% */;
echo '>';
echo LR\Filters::escapeHtmlText($薀_input->getCaption()) /* line %d% */;
echo '</button>
';
echo Nette\Bridges\FormsLatte\Runtime::renderFormEnd(end($this->global->formsStack), false) /* line %d% */;
echo $this->global->forms->renderFormEnd(false) /* line %d% */;
echo '</form>
';
array_pop($this->global->formsStack);
$this->global->forms->end();
%A%
Loading

0 comments on commit ee771d7

Please sign in to comment.