Skip to content

Commit

Permalink
Blueprint::printClass() rewritten (BC break)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 17, 2024
1 parent 16de2dd commit 466863e
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 72 deletions.
68 changes: 42 additions & 26 deletions src/Latte/Essential/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

namespace Latte\Essential;

use Latte;
use Latte\Runtime\Template;
use Nette\PhpGenerator as Php;


Expand All @@ -20,50 +18,63 @@
*/
final class Blueprint
{
public function printClass(Template $template, ?string $name = null): void
public function generateTemplateClass(
array $params,
?string $name = 'Template',
?string $extends = null,
): Php\ClassType
{
if (!class_exists(Php\ClassType::class)) {
throw new \LogicException('Nette PhpGenerator is required to print template, install package `nette/php-generator`.');
throw new \LogicException('Nette PhpGenerator is required to generate blueprint, install package `nette/php-generator`.');
}

$name = $name ?: 'Template';
$namespace = new Php\PhpNamespace(Php\Helpers::extractNamespace($name));
$class = $namespace->addClass(Php\Helpers::extractShortName($name));
if ($extends) {
if (!class_exists($extends)) {
throw new \LogicException("Blueprint error: Class '$extends' doesn't exist.");
} elseif ((new \ReflectionClass($extends))->isFinal()) {
throw new \LogicException("Blueprint error: Unable to extend final class $extends");
}
$class->setExtends($extends);
$params = array_diff_key($params, get_class_vars($extends));
}
$this->addProperties($class, $params);
return $class;
}

$this->addProperties($class, $template->getParameters());
$functions = array_diff_key($template->global->fn->getAll(), (new Latte\Essential\CoreExtension)->getFunctions());
$this->addFunctions($class, $functions);

$end = $this->printCanvas();
$this->printHeader('Native types');
$this->printCode((string) $namespace);
echo $end;
public function printClass(Php\ClassType $class, bool $exit = true): void
{
$this->openCanvas();
$this->printCode((string) $class->getNamespace());
$this->closeCanvas();
if ($exit) {
exit;
}
}


/**
* @param mixed[] $vars
*/
public function printVars(array $vars): void
public function printVars(array $vars, bool $exit = true): void
{
if (!class_exists(Php\Type::class)) {
throw new \LogicException('Nette PhpGenerator is required to print template, install package `nette/php-generator`.');
}

$res = '';
foreach ($vars as $name => $value) {
if (str_starts_with($name, 'ʟ_')) {
continue;
if (!str_starts_with($name, 'ʟ_')) {
$type = $this->getType($value);
$res .= "{varType $type $$name}\n";
}

$type = $this->getType($value);
$res .= "{varType $type $$name}\n";
}

$end = $this->printCanvas();
$this->openCanvas();
$this->printHeader('varPrint');
$this->printCode($res ?: 'No variables', 'latte');
echo $end;
$this->closeCanvas();
if ($exit) {
exit;
}
}


Expand Down Expand Up @@ -134,12 +145,17 @@ public function printParameters(
}


public function printCanvas(): string
public function openCanvas(): void
{
echo '<script src="https://nette.github.io/resources/prism/prism.js"></script>';
echo '<link rel="stylesheet" href="https://nette.github.io/resources/prism/prism.css">';
echo "<div style='all:initial;position:fixed;overflow:auto;z-index:1000;left:0;right:0;top:0;bottom:0;color:black;background:white;padding:1em'>\n";
return "</div>\n";
}


public function closeCanvas(): void
{
echo "</div>\n";
}


Expand Down
8 changes: 5 additions & 3 deletions src/Latte/Essential/Nodes/TemplatePrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
use Latte\Compiler\Nodes;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\NodeTraverser;
use Latte\Compiler\PhpHelpers;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;
use Latte\Compiler\Token;


/**
* {templatePrint [ClassName]}
* {templatePrint [ParentClass]}
*/
class TemplatePrintNode extends StatementNode
{
Expand All @@ -37,7 +36,10 @@ public static function create(Tag $tag): static

public function print(PrintContext $context): string
{
return '(new Latte\Essential\Blueprint)->printClass($this, ' . PhpHelpers::dump($this->template) . '); exit;';
return $context->format(<<<'XX'
$ʟ_tmp = new Latte\Essential\Blueprint;
$ʟ_tmp->printClass($ʟ_tmp->generateTemplateClass($this->getParameters(), extends: %dump));
XX, $this->template);
}


Expand Down
5 changes: 3 additions & 2 deletions src/Latte/Essential/Nodes/VarPrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public static function create(Tag $tag): static

public function print(PrintContext $context): string
{
$vars = $this->all ? 'get_defined_vars()'
$vars = $this->all
? 'get_defined_vars()'
: 'array_diff_key(get_defined_vars(), $this->getParameters())';
return "(new Latte\\Essential\\Blueprint)->printVars($vars); exit;";
return "(new Latte\\Essential\\Blueprint)->printVars($vars);";
}


Expand Down
61 changes: 61 additions & 0 deletions tests/common/Blueprint.printClass.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

use Tester\Assert;

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


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
$latte->addFunction('Abc', function (stdClass $a, $b = 132) {});


class ParentTemplate
{
public $int;
}

$params = ['int' => 123, 'unknown' => null];

$blueprint = new Latte\Essential\Blueprint;
ob_start();
$blueprint->printClass(
$blueprint->generateTemplateClass($params),
exit: false,
);
$res = ob_get_clean();

Assert::match(
<<<'XX'
%A%class Template
{
public int $int;
public mixed $unknown;
}
%A%
XX,
$res,
);


ob_start();
$blueprint->printClass(
$blueprint->generateTemplateClass($params, name: Foo\Template::class, extends: ParentTemplate::class),
exit: false,
);
$res = ob_get_clean();

Assert::match(
<<<'XX'
%A%namespace Foo;
class Template extends \ParentTemplate
{
public mixed $unknown;
}
%A%
XX,
$res,
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


$printer = new Latte\Essential\Blueprint;
$blueprint = new Latte\Essential\Blueprint;
ob_start();
$printer->printVars(['int' => 123, 'unknown' => null]);
$blueprint->printVars(['int' => 123, 'unknown' => null], exit: false);
$res = ob_get_clean();

Assert::match(
Expand Down
35 changes: 0 additions & 35 deletions tests/tags/printClass.phpt

This file was deleted.

8 changes: 4 additions & 4 deletions tests/tags/templatePrint.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Assert::match(
{
extract($this->params);
(new Latte\Essential\Blueprint)->printClass($this, null);
exit;
$ʟ_tmp = new Latte\Essential\Blueprint;
$ʟ_tmp->printClass($ʟ_tmp->generateTemplateClass($this->getParameters(), extends: null));
%A%
XX,
$latte->compile('Foo {block}{/block} {templatePrint}'),
Expand All @@ -37,8 +37,8 @@ Assert::match(
{
extract($this->params);
(new Latte\Essential\Blueprint)->printClass($this, 'Foo');
exit;
$ʟ_tmp = new Latte\Essential\Blueprint;
$ʟ_tmp->printClass($ʟ_tmp->generateTemplateClass($this->getParameters(), extends: 'Foo'));
%A%
XX,
$latte->compile('{templatePrint Foo}'),
Expand Down

0 comments on commit 466863e

Please sign in to comment.