Skip to content

Commit b570869

Browse files
jkavalikf3l1x
authored andcommitted
latte3 compatibility
1 parent 28329a4 commit b570869

8 files changed

Lines changed: 220 additions & 12 deletions

File tree

.github/workflows/main.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323

2424
strategy:
2525
matrix:
26-
php-version: [ "7.4" ]
26+
php-version: [ "8.0" ]
2727
operating-system: [ "ubuntu-latest" ]
2828
fail-fast: false
2929

@@ -82,7 +82,7 @@ jobs:
8282

8383
strategy:
8484
matrix:
85-
php-version: [ "7.4" ]
85+
php-version: [ "8.0" ]
8686
operating-system: [ "ubuntu-latest" ]
8787
fail-fast: false
8888

@@ -138,7 +138,7 @@ jobs:
138138

139139
strategy:
140140
matrix:
141-
php-version: [ "7.2", "7.3", "7.4", "8.0" ]
141+
php-version: [ "7.4", "8.0", "8.1", "8.2" ]
142142
operating-system: [ "ubuntu-latest" ]
143143
composer-args: [ "" ]
144144
include:
@@ -150,7 +150,7 @@ jobs:
150150
composer-args: ""
151151
fail-fast: false
152152

153-
continue-on-error: "${{ matrix.php-version == '8.0' }}"
153+
continue-on-error: "${{ matrix.php-version == '7.4' }}"
154154

155155
steps:
156156
- name: "Checkout"

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
"wizard"
1010
],
1111
"require": {
12-
"php": ">=7.1",
12+
"php": ">=7.4",
1313
"nette/forms": "^3.0",
1414
"nette/http": "^3.0"
1515
},
1616
"require-dev": {
1717
"codeception/codeception": "^4.1.20",
1818
"codeception/module-asserts": "^1.3.1|^2.0",
1919
"codeception/module-phpbrowser": "^1.0.2|^2.0",
20-
"latte/latte": "^2.7",
20+
"latte/latte": "^2.7|^3.0",
2121
"nette/application": "~3.1.4",
2222
"nette/di": "^3.0",
2323
"nette/tester": "^2.3.2",

src/DI/WizardExtension.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Contributte\FormWizard\DI;
44

55
use Contributte\FormWizard\Latte\WizardMacros;
6+
use Latte\Engine;
67
use Nette\DI\CompilerExtension;
78
use Nette\DI\Definitions\FactoryDefinition;
9+
use Nette\DI\Definitions\Statement;
810

911
final class WizardExtension extends CompilerExtension
1012
{
@@ -16,8 +18,13 @@ public function beforeCompile(): void
1618
/** @var FactoryDefinition $latte */
1719
$latte = $builder->getDefinition('nette.latteFactory');
1820

19-
$latte->getResultDefinition()
20-
->addSetup(WizardMacros::class . '::install(?)', ['@self']);
21+
$resultDefinition = $latte->getResultDefinition();
22+
23+
if (version_compare(Engine::VERSION, '3', '<')) { // @phpstan-ignore-line
24+
$resultDefinition->addSetup(WizardMacros::class . '::install(?->getCompiler())', ['@self']);
25+
} else {
26+
$resultDefinition->addSetup('addExtension', [new Statement(\Contributte\FormWizard\Latte\WizardExtension::class)]);
27+
}
2128
}
2229

2330
}

src/Latte/StepNode.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\FormWizard\Latte;
4+
5+
use Generator;
6+
use Latte\CompileException;
7+
use Latte\Compiler\Nodes\AreaNode;
8+
use Latte\Compiler\Nodes\Php\ExpressionNode;
9+
use Latte\Compiler\Nodes\Php\Scalar\StringNode;
10+
use Latte\Compiler\Nodes\StatementNode;
11+
use Latte\Compiler\Position;
12+
use Latte\Compiler\PrintContext;
13+
use Latte\Compiler\Tag;
14+
15+
final class StepNode extends StatementNode
16+
{
17+
18+
/** @var ExpressionNode */
19+
public $name;
20+
21+
/** @var AreaNode */
22+
public $content;
23+
24+
/**
25+
* @return Generator<int, ?mixed[], array{AreaNode, ?Tag}, self>
26+
*/
27+
public static function create(Tag $tag): Generator
28+
{
29+
$tag->outputMode = $tag::OutputRemoveIndentation;
30+
$tag->expectArguments();
31+
32+
$node = new static;
33+
$node->name = $tag->parser->parseUnquotedStringOrExpression();
34+
35+
[$node->content] = yield;
36+
37+
return $node;
38+
}
39+
40+
public function print(PrintContext $context): string
41+
{
42+
$word = $this->name instanceof StringNode ? $this->name->value : intval($this->name->value ?? $this->name);
43+
44+
if (!is_numeric($word) && !in_array($word, ['success', '"success"', "'success'"], true)) {
45+
throw new CompileException('First parameter in {step} must be a numeric.');
46+
}
47+
48+
if (is_string($word) && str_contains($word, 'success')) {
49+
return $context->format(
50+
'if ($wizard->isSuccess()) {'
51+
. "\n"
52+
. ' %line %node ' // content
53+
. "\n"
54+
. "}"
55+
. "\n\n",
56+
$this->position,
57+
$this->content
58+
);
59+
}
60+
61+
return $context->format(
62+
'if ($wizard->getCurrentStep() === %raw && !$wizard->isSuccess()) { $wizardForm = $form = $wizard->getCurrentComponent();'
63+
. "\n"
64+
. ' %line %node ' // content
65+
. "\n"
66+
. "}"
67+
. "\n\n",
68+
$word,
69+
$this->position,
70+
$this->content
71+
);
72+
}
73+
public function &getIterator(): \Generator
74+
{
75+
yield $this->name;
76+
yield $this->content;
77+
}
78+
79+
}

src/Latte/WizardExtension.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\FormWizard\Latte;
4+
5+
use Contributte\FormWizard\Facade;
6+
use Contributte\FormWizard\IWizard;
7+
use Exception;
8+
use Latte\CompileException;
9+
use Latte\Engine;
10+
use Latte\Extension;
11+
use Nette\ComponentModel\IComponent;
12+
13+
class WizardExtension extends Extension
14+
{
15+
16+
public function getTags(): array
17+
{
18+
return [
19+
'wizard' => [WizardNode::class, 'create'],
20+
'step' => [StepNode::class, 'create'],
21+
];
22+
}
23+
24+
/**
25+
* @throws Exception
26+
*/
27+
public static function createFacade(IComponent $component): Facade
28+
{
29+
if (!$component instanceof IWizard) {
30+
throw new Exception('Wizard must be instance of ' . IWizard::class);
31+
}
32+
33+
return new Facade($component);
34+
}
35+
36+
}

src/Latte/WizardNode.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Contributte\FormWizard\Latte;
4+
5+
use Contributte\FormWizard\Facade;
6+
use Contributte\FormWizard\IWizard;
7+
use Generator;
8+
use Latte\Compiler\NodeHelpers;
9+
use Latte\Compiler\Nodes\AreaNode;
10+
use Latte\Compiler\Nodes\Php\ExpressionNode;
11+
use Latte\Compiler\Nodes\StatementNode;
12+
use Latte\Compiler\PrintContext;
13+
use Latte\Compiler\Tag;
14+
use Nette\ComponentModel\IComponent;
15+
16+
final class WizardNode extends StatementNode
17+
{
18+
19+
/** @var ExpressionNode */
20+
public $name;
21+
22+
/** @var AreaNode */
23+
public $content;
24+
25+
/**
26+
* @return Generator<int, ?mixed[], array{AreaNode, ?Tag}, self>
27+
*/
28+
public static function create(Tag $tag): Generator
29+
{
30+
$tag->outputMode = $tag::OutputRemoveIndentation;
31+
$tag->expectArguments();
32+
33+
$node = new static;
34+
$node->name = $tag->parser->parseUnquotedStringOrExpression();
35+
36+
[$node->content] = yield;
37+
38+
return $node;
39+
}
40+
41+
/**
42+
* @throws Exception
43+
*/
44+
public static function createFacade(IComponent $component): Facade
45+
{
46+
if (!$component instanceof IWizard) {
47+
throw new Exception('Wizard must be instance of ' . IWizard::class);
48+
}
49+
50+
return new Facade($component);
51+
}
52+
53+
public function print(PrintContext $context): string
54+
{
55+
$nameValue = static::toValue($this->name);
56+
$componentGetter = '$this->global->uiControl->getComponent("' . $nameValue . '")';
57+
// variable : getter
58+
$wizardGetter = $nameValue[0] === '$' ? sprintf('is_object(%s) ? %s : %s', $nameValue, $nameValue, $componentGetter) : $componentGetter;
59+
60+
return $context->format(
61+
'$wizard = %raw::createFacade(%raw);'
62+
. "\n"
63+
. ' %line %node ' // content
64+
. "\n\n",
65+
static::class,
66+
$wizardGetter,
67+
$this->position,
68+
$this->content
69+
);
70+
}
71+
public function &getIterator(): \Generator
72+
{
73+
yield $this->name;
74+
yield $this->content;
75+
}
76+
77+
public static function toValue($args): mixed
78+
{
79+
try {
80+
return NodeHelpers::toValue($args, constants: true);
81+
} catch (\InvalidArgumentException) {
82+
return null;
83+
}
84+
}
85+
86+
}

tests/includes/WizardPresenter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public function getCustomSession()
1818
return $this->session;
1919
}
2020

21-
protected function createTemplate(): \Nette\Application\UI\ITemplate
21+
protected function createTemplate(): \Nette\Application\UI\Template
2222
{
2323
$template = parent::createTemplate();
2424

25-
WizardMacros::install($template->getLatte());
25+
$template->getLatte()->addExtension(new \Contributte\FormWizard\Latte\WizardExtension());
2626

2727
return $template;
2828
}

tests/unit/_bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

33
use Latte\Engine;
4-
use Nette\Bridges\ApplicationLatte\ILatteFactory;
4+
use Nette\Bridges\ApplicationLatte\LatteFactory;
55

6-
class MockLatteFactory implements ILatteFactory
6+
class MockLatteFactory implements LatteFactory
77
{
88

99
public function create(): Engine

0 commit comments

Comments
 (0)