Skip to content

Commit

Permalink
Blueprint::printClass() rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 16, 2024
1 parent 16de2dd commit ce29aad
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 66 deletions.
63 changes: 43 additions & 20 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,75 @@
*/
final class Blueprint
{
public function printClass(Template $template, ?string $name = null): void
{
public function __construct(
) {
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 use blueprints, install package `nette/php-generator`.');
}
}

$name = $name ?: 'Template';

public function generateClass(
array $params,
?string $name = 'Template',
?string $extends = null,
): Php\ClassType
{
$namespace = new Php\PhpNamespace(Php\Helpers::extractNamespace($name));
$class = $namespace->addClass(Php\Helpers::extractShortName($name));
if ($extends) {
$class->setExtends($extends);
if (class_exists($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);

public function printClass(
Php\ClassType $class,
bool $exit = true,
): void
{
$end = $this->printCanvas();
$this->printHeader('Native types');
$this->printCode((string) $namespace);
if (($extends = $class->getExtends()) && !class_exists($extends)) {
$this->printHeader("Blueprint error: Class '$extends' doesn't exist.");
} else {
$this->printCode((string) $class->getNamespace());
}
echo $end;
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`.');
}

$blueprint = new self;
$res = '';
foreach ($vars as $name => $value) {
if (str_starts_with($name, 'ʟ_')) {
continue;
}

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

$end = $this->printCanvas();
$this->printHeader('varPrint');
$this->printCode($res ?: 'No variables', 'latte');
$end = $blueprint->printCanvas();
$blueprint->printHeader('varPrint');
$blueprint->printCode($res ?: 'No variables', 'latte');
echo $end;

if ($exit) {
exit;
}
}


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->generateClass($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->generateClass($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->generateClass($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->generateClass($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->generateClass($this->getParameters(), extends: 'Foo'));
%A%
XX,
$latte->compile('{templatePrint Foo}'),
Expand Down

0 comments on commit ce29aad

Please sign in to comment.