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 Oct 29, 2023
1 parent a29698a commit 21752ae
Show file tree
Hide file tree
Showing 21 changed files with 219 additions and 204 deletions.
4 changes: 2 additions & 2 deletions src/Bridges/FormsLatte/FormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function getTags(): array
public function getProviders(): array
{
return [
'formsStack' => [],
'forms' => new Runtime,
];
}


public function getCacheKey(Latte\Engine $engine): mixed
{
return 1;
return 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
13 changes: 6 additions & 7 deletions src/Bridges/FormsLatte/Nodes/FormNNameNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -62,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
17 changes: 9 additions & 8 deletions src/Bridges/FormsLatte/Nodes/FormNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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,
);
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
55 changes: 37 additions & 18 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,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();

Check failure on line 35 in src/Bridges/FormsLatte/Runtime.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Nette\Forms\Container::getElementPrototype().
$el->action = (string) $el->action;
$el = clone $el;
Expand All @@ -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')) {

Check failure on line 54 in src/Bridges/FormsLatte/Runtime.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Nette\Forms\Container::isMethod().
foreach (preg_split('#[;&]#', (string) parse_url($form->getElementPrototype()->action, PHP_URL_QUERY), -1, PREG_SPLIT_NO_EMPTY) as $param) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 <form n:name> tag?');
return $form[$item];
}


public function end(): void
{
array_pop($this->stack);
}


public function current(): Container
{
return end($this->stack) ?: throw new \LogicException('Form declaration is missing, did you use {form} or <form n:name> tag?');
}
}
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
15 changes: 7 additions & 8 deletions tests/Forms.Latte3/expected/forms.button.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<?php
%A%
$form = $this->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 '<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 21752ae

Please sign in to comment.