Skip to content

Commit

Permalink
snippet wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 10, 2023
1 parent 466fca6 commit bfe61e7
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Application/UI/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function isControlInvalid(?string $snippet = null): bool
if ($snippet !== null) {
return $this->invalidSnippets[$snippet] ?? isset($this->invalidSnippets["\0"]);

} elseif (count($this->invalidSnippets) > 0) {
} elseif (array_keys($this->invalidSnippets, true, true)) {
return true;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Bridges/ApplicationLatte/Nodes/ControlNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ControlNode extends StatementNode
public ?ExpressionNode $method = null;
public ArrayNode $args;
public ?bool $escape = null;
public bool $inSnippetArea = false;


public static function create(Tag $tag): static
Expand All @@ -40,6 +41,7 @@ public static function create(Tag $tag): static
$stream = $tag->parser->stream;
$node = new static;
$node->name = $tag->parser->parseUnquotedStringOrExpression(colon: false);
$node->inSnippetArea = $tag->closestTag(['snippetArea']) && !$tag->closestTag(['snippet']);
if ($stream->tryConsume(':')) {
$node->method = $tag->parser->parseExpression();
}
Expand Down Expand Up @@ -94,11 +96,14 @@ public function print(PrintContext $context): string
$this->name,
);

$fetchCode .= $this->inSnippetArea
? 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->snippetMode = $this->global->uiControl->snippetMode;'
: 'if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);';

if ($this->escape) {
return $context->format(
<<<'XX'
%raw
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
ob_start(fn() => '');
$ʟ_tmp->%raw(%args) %line;
$ʟ_fi = new LR\FilterInfo(%dump); echo %modifyContent(ob_get_clean());
Expand All @@ -117,7 +122,6 @@ public function print(PrintContext $context): string
return $context->format(
<<<'XX'
%raw
if ($ʟ_tmp instanceof Nette\Application\UI\Renderable) $ʟ_tmp->redrawControl(null, false);
$ʟ_tmp->%raw(%args) %line;


Expand Down
12 changes: 6 additions & 6 deletions src/Bridges/ApplicationLatte/SnippetRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ public function enter(string $name, string $type): void
$this->nestingLevel++;
}

$this->stack[] = [$name, $obStarted];
if ($name !== '') {
$this->control->redrawControl($name, false);
$this->stack[] = [$name, $obStarted, $this->control->snippetMode];
if ($type !== self::TypeArea) {
$this->control->snippetMode = false;
}
$this->control->redrawControl($name, false);
}


Expand All @@ -78,7 +79,7 @@ public function leave(): void
return;
}

[$name, $obStarted] = array_pop($this->stack);
[$name, $obStarted, $this->control->snippetMode] = array_pop($this->stack);
if ($this->nestingLevel > 0 && --$this->nestingLevel === 0) {
$content = ob_get_clean();
$this->payload ??= $this->control->getPresenter()->getPayload();
Expand Down Expand Up @@ -107,7 +108,6 @@ public function renderSnippets(array $blocks, array $params): bool
}

$this->renderingSnippets = true;
$this->control->snippetMode = false;
foreach ($blocks as $name => $block) {
if (!$this->control->isControlInvalid($name)) {
continue;
Expand All @@ -116,8 +116,8 @@ public function renderSnippets(array $blocks, array $params): bool
$function = reset($block->functions);
$function($params);
}
$this->renderingSnippets = false;

$this->control->snippetMode = true;
$this->renderChildren();
return true;
}
Expand Down
91 changes: 91 additions & 0 deletions tests/Bridges.Latte3/renderSnippets8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* Test: renderSnippets and control wrapped in a snippet
* @phpVersion 8.0
*/

declare(strict_types=1);

use Nette\Http;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';

if (version_compare(Latte\Engine::VERSION, '3', '<')) {
Tester\Environment::skip('Test for Latte 3');
}


class TestControl extends Nette\Application\UI\Control
{
public int $counter = 0;


public function render(int $arg = 0)
{
$this->counter++;
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this));
$latte->render('{snippet foo}hello {$arg}{/snippet}', ['arg' => $arg]);
}
}

class TestPresenter extends Nette\Application\UI\Presenter
{
public function createComponentTest()
{
return new TestControl;
}


public function render()
{
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addExtension(new Nette\Bridges\ApplicationLatte\UIExtension($this));
$latte->render('{snippetArea foo}{control test 123}{/snippetArea}');
}
}


$presenter = new TestPresenter;
$presenter->injectPrimary(null, null, null, new Http\Request(new Http\UrlScript('/')), new Http\Response);
$presenter->snippetMode = true;
$presenter->redrawControl('foo');
$presenter['test']->redrawControl('foo');
$presenter->render();
Assert::same([
'snippets' => [
'snippet-test-foo' => 'hello 123',
],
], (array) $presenter->payload);
Assert::same(1, $presenter['test']->counter);


$presenter = new TestPresenter;
$presenter->injectPrimary(null, null, null, new Http\Request(new Http\UrlScript('/')), new Http\Response);
$presenter->snippetMode = true;
$presenter->redrawControl('foo');
$presenter['test']->redrawControl();
$presenter->render();
Assert::same([
'snippets' => [
'snippet-test-foo' => 'hello 123',
],
], (array) $presenter->payload);
Assert::same(2, $presenter['test']->counter);


$presenter = new TestPresenter;
$presenter->injectPrimary(null, null, null, new Http\Request(new Http\UrlScript('/')), new Http\Response);
$presenter->snippetMode = true;
$presenter['test']->redrawControl('foo');
$presenter->render();
Assert::same([
'snippets' => [
'snippet-test-foo' => 'hello 0',
],
], (array) $presenter->payload);
Assert::same(1, $presenter['test']->counter);

0 comments on commit bfe61e7

Please sign in to comment.