From 4a8d0f383b8cfb0204f018648556aff48a3a5666 Mon Sep 17 00:00:00 2001 From: Giorgio Pogliani Date: Thu, 12 Sep 2024 15:29:36 +0200 Subject: [PATCH] Revert "Yield ready update #22 (#23)" This reverts commit 69c39d2f10db527dbbe22d7afd6ec171fac06d3a. --- .github/workflows/run-tests.yml | 2 +- composer.json | 11 ++++----- phpunit.xml.dist | 40 ++++++++++++++++++++++++--------- src/Node/ComponentNode.php | 9 +++----- src/Node/SlotNode.php | 7 ++---- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 9c2c5b8..6528b58 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,7 +9,7 @@ jobs: fail-fast: true matrix: os: [ubuntu-latest] - php: [8.1, 8.2, 8.3] + php: [8.0, 8.1, 8.2] dependency-version: [prefer-lowest, prefer-stable] name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} diff --git a/composer.json b/composer.json index b02c86b..db799bb 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,15 @@ } ], "require": { - "php": ">= 8.1 < 8.4", - "twig/twig": ">=3.9" + "php": "^7.4|^8.0", + "twig/twig": "^2.12.5 || >=3.0 <3.9" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", - "pestphp/pest": "^2.0", - "phpunit/phpunit": "^10.1", - "symfony/var-dumper": "^5.2" + "pestphp/pest": "^1.0", + "phpunit/phpunit": "^9.3", + "symfony/var-dumper": "^5.2", + "vimeo/psalm": "^3.11" }, "autoload": { "psr-4": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 9b0f925..1d5bedd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,13 +1,31 @@ - - - - tests - - - - - src - - + + + + tests + + + + + ./src + + + + + + + + + + diff --git a/src/Node/ComponentNode.php b/src/Node/ComponentNode.php index 22aa1cd..77e0f95 100644 --- a/src/Node/ComponentNode.php +++ b/src/Node/ComponentNode.php @@ -6,7 +6,6 @@ use Performing\TwigComponents\Configuration; use Performing\TwigComponents\View\ComponentAttributeBag; use Performing\TwigComponents\View\ComponentSlot; -use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Node\Expression\AbstractExpression; use Twig\Node\Expression\ConstantExpression; @@ -14,7 +13,6 @@ use Twig\Node\IncludeNode; use Twig\Node\Node; -#[YieldReady] final class ComponentNode extends IncludeNode { const DYNAMIC_COMPONENT_NAME = 'dynamic-component'; @@ -45,17 +43,16 @@ public function compile(Compiler $compiler): void ->write('$slotsStack = $slotsStack ?? [];' . PHP_EOL) ->write('$slotsStack[] = $slots ?? [];' . PHP_EOL) ->write('$slots = [];' . PHP_EOL) - ->write('$slot = implode("", iterator_to_array((function () use (&$slots, &$context) {' . PHP_EOL) + ->write("ob_start();" . PHP_EOL) ->subcompile($this->getNode('slot')) - ->write("})() ?? new \EmptyIterator()));" . PHP_EOL) - ->write(sprintf('yield $%s->render(', $template)); + ->write('$slot = ob_get_clean();' . PHP_EOL) + ->write(sprintf('$%s->display(', $template)); $this->addTemplateArguments($compiler); $compiler ->raw(");\n") ->write('$slots = array_pop($slotsStack);' . PHP_EOL) - ->write("yield '';") ->write("}\n"); } diff --git a/src/Node/SlotNode.php b/src/Node/SlotNode.php index bbd3df7..e90b903 100644 --- a/src/Node/SlotNode.php +++ b/src/Node/SlotNode.php @@ -3,14 +3,12 @@ namespace Performing\TwigComponents\Node; use Performing\TwigComponents\View\ComponentSlot; -use Twig\Attribute\YieldReady; use Twig\Compiler; use Twig\Node\Expression\AbstractExpression; use Twig\Node\Node; use Twig\Node\NodeOutputInterface; -#[YieldReady] final class SlotNode extends Node implements NodeOutputInterface { public function __construct($name, $body, ?AbstractExpression $variables, int $lineno = 0) @@ -27,10 +25,9 @@ public function compile(Compiler $compiler): void $name = $this->getAttribute('name'); $compiler - ->write('$body = (function () use (&$slots, &$context) {') + ->write('ob_start();') ->subcompile($this->getNode('body')) - ->write('})() ?? new \EmptyIterator();' . PHP_EOL) - ->write('$body = implode("", iterator_to_array($body));' . PHP_EOL) + ->write('$body = ob_get_clean();' . PHP_EOL) ->write("\$slots['$name'] = new " . ComponentSlot::class . "(\$body, "); if ($this->hasNode('variables')) {