Skip to content

Commit

Permalink
added bridge for Latte 3
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed May 12, 2022
1 parent 4c47a26 commit fe2109c
Show file tree
Hide file tree
Showing 51 changed files with 1,683 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.2
php-version: 8.0
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.0
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
"nette/application": "^3.0",
"nette/di": "^3.0",
"nette/tester": "^2.0",
"latte/latte": "^2.10.2",
"latte/latte": "^2.10.2 || ^3.0",
"tracy/tracy": "^2.4",
"phpstan/phpstan-nette": "^0.12"
},
"conflict": {
"nette/di": "<3.0-stable",
"latte/latte": ">=3.0"
"latte/latte": ">=3.1"
},
"autoload": {
"classmap": ["src/"]
Expand Down
4 changes: 1 addition & 3 deletions examples/latte.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
}

$latte = new Latte\Engine;
$latte->onCompile[] = function ($latte) {
Nette\Bridges\FormsLatte\FormMacros::install($latte->getCompiler());
};
$latte->addExtension(new Nette\Bridges\FormsLatte\FormsExtension);

$latte->render(__DIR__ . '/latte/page.latte', ['form' => $form]);
2 changes: 1 addition & 1 deletion src/Bridges/FormsLatte/FormMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


/**
* Latte macros for Nette\Forms.
* Latte v2 macros for Nette\Forms.
*
* - {form name} ... {/form}
* - {input name}
Expand Down
34 changes: 34 additions & 0 deletions src/Bridges/FormsLatte/FormsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte;

use Latte;


/**
* Latte v3 extension for Nette Forms.
*/
final class FormsExtension extends Latte\Extension
{
public function getTags(): array
{
return [
'form' => [Nodes\FormNode::class, 'create'],
'formContext' => [Nodes\FormNode::class, 'create'],
'formContainer' => [Nodes\FormContainerNode::class, 'create'],
'label' => [Nodes\LabelNode::class, 'create'],
'input' => [Nodes\InputNode::class, 'create'],
'inputError' => [Nodes\InputErrorNode::class, 'create'],
'formPrint' => [Nodes\FormPrintNode::class, 'create'],
'formClassPrint' => [Nodes\FormPrintNode::class, 'create'],
'n:name' => [Nodes\NNameNode::class, 'create'],
];
}
}
64 changes: 64 additions & 0 deletions src/Bridges/FormsLatte/Nodes/FormContainerNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;


/**
* {formContainer ...}
*/
class FormContainerNode extends StatementNode
{
public ExpressionNode $name;
public AreaNode $content;


/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static|AreaNode> */
public static function create(Tag $tag): \Generator
{
$tag->outputMode = $tag::OutputRemoveIndentation;
$tag->expectArguments();

$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression();
[$node->content] = yield;
return $node;
}


public function print(PrintContext $context): string
{
return $context->format(
'$this->global->formsStack[] = $formContainer = '
. ($this->name instanceof StringNode
? 'end($this->global->formsStack)[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : end($this->global->formsStack)[$ʟ_tmp]')
. ' %line; %node '
. 'array_pop($this->global->formsStack); $formContainer = end($this->global->formsStack);'
. "\n\n",
$this->name,
$this->position,
$this->content,
);
}


public function &getIterator(): \Generator
{
yield $this->name;
yield $this->content;
}
}
92 changes: 92 additions & 0 deletions src/Bridges/FormsLatte/Nodes/FormNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte\CompileException;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php\Expression\ArrayNode;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\Position;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;


/**
* {form name} ... {/form}
* {formContext ...}
*/
class FormNode extends StatementNode
{
public ExpressionNode $name;
public ArrayNode $attributes;
public AreaNode $content;
public bool $print;
public ?Position $endLine;


/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static|AreaNode> */
public static function create(Tag $tag): \Generator
{
if ($tag->isNAttribute()) {
throw new CompileException('Did you mean <form n:name=...> ?', $tag->position);
}

$tag->outputMode = $tag::OutputKeepIndentation;
$tag->expectArguments();
$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression();
$tag->parser->stream->tryConsume(',');
$node->attributes = $tag->parser->parseArguments();
$node->print = $tag->name === 'form';

[$node->content, $endTag] = yield;
$node->endLine = $endTag?->position;
if ($endTag && $node->name instanceof StringNode) {
$endTag->parser->stream->tryConsume($node->name->value);
}

return $node;
}


public function print(PrintContext $context): string
{
return $context->format(
'$form = $this->global->formsStack[] = '
. ($this->name instanceof StringNode
? '$this->global->uiControl[%node]'
: 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]')
. ' %line;'
. ($this->print
? 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin($form, %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",
$this->name,
$this->position,
$this->attributes,
$this->content,
$this->endLine,
);
}


public function &getIterator(): \Generator
{
yield $this->name;
yield $this->attributes;
yield $this->content;
}
}
63 changes: 63 additions & 0 deletions src/Bridges/FormsLatte/Nodes/FormPrintNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;


/**
* {formPrint [ClassName]}
* {formClassPrint [ClassName]}
*/
class FormPrintNode extends StatementNode
{
public ?ExpressionNode $name;
public string $mode;


public static function create(Tag $tag): static
{
$node = new static;
$node->name = $tag->parser->isEnd()
? null
: $tag->parser->parseUnquotedStringOrExpression();
$node->mode = $tag->name;
return $node;
}


public function print(PrintContext $context): string
{
return $context->format(
'Nette\Bridges\FormsLatte\Runtime::render%raw('
. match (true) {
!$this->name => 'end($this->global->formsStack)',
$this->name instanceof StringNode => '$this->global->uiControl[%node]',
default => 'is_object($ʟ_tmp = %node) ? $ʟ_tmp : $this->global->uiControl[$ʟ_tmp]',
}
. ') %line; exit;',
$this->mode,
$this->name,
$this->position,
);
}


public function &getIterator(): \Generator
{
if ($this->name) {
yield $this->name;
}
}
}
67 changes: 67 additions & 0 deletions src/Bridges/FormsLatte/Nodes/InputErrorNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Bridges\FormsLatte\Nodes;

use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;


/**
* {inputError ...}
*/
class InputErrorNode extends StatementNode
{
public ?ExpressionNode $name;


public static function create(Tag $tag): static
{
$tag->outputMode = $tag::OutputKeepIndentation;
$node = new static;
$node->name = $tag->parser->isEnd()
? null
: $tag->parser->parseUnquotedStringOrExpression();
return $node;
}


public function print(PrintContext $context): string
{
if (!$this->name) {
return $context->format('echo %escape($ʟ_input->getError()) %line;', $this->position);

} elseif ($this->name instanceof StringNode) {
return $context->format(
'echo %escape(end($this->global->formsStack)[%node]->getError()) %line;',
$this->name,
$this->position,
);

} else {
return $context->format(
'$ʟ_input = is_object($ʟ_tmp = %node) ? $ʟ_tmp : end($this->global->formsStack)[$ʟ_tmp];'
. 'echo %escape($ʟ_input->getError()) %line;',
$this->name,
$this->position,
);
}
}


public function &getIterator(): \Generator
{
if ($this->name) {
yield $this->name;
}
}
}
Loading

0 comments on commit fe2109c

Please sign in to comment.