Skip to content

Commit

Permalink
Latte: compatible with latest blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 17, 2024
1 parent 0ce1d9a commit ec91641
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
53 changes: 23 additions & 30 deletions src/Bridges/ApplicationLatte/Nodes/TemplatePrintNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
use Latte;
use Latte\Compiler\PhpHelpers;
use Latte\Compiler\PrintContext;
use Nette\Application\UI\Presenter;
use Nette\Application\UI;
use Nette\Bridges\ApplicationLatte\Template;
use Nette\PhpGenerator as Php;


/**
* {templatePrint [ClassName]}
Expand All @@ -24,42 +22,37 @@ class TemplatePrintNode extends Latte\Essential\Nodes\TemplatePrintNode
{
public function print(PrintContext $context): string
{
return self::class . '::printClass($this, ' . PhpHelpers::dump($this->template) . '); exit;';
return self::class . '::printClass($this->getParameters(), ' . PhpHelpers::dump($this->template ?? Template::class) . '); exit;';
}


public static function printClass(Latte\Runtime\Template $template, ?string $parent = null): void
public static function printClass(array $params, string $parentClass): void
{
$blueprint = new Latte\Essential\Blueprint;
$name = 'Template';
$params = $template->getParameters();
$bp = new Latte\Essential\Blueprint;
$control = $params['control'] ?? $params['presenter'] ?? null;
if ($control) {
$name = 'Template';
if ($control instanceof UI\Control) {
$name = preg_replace('#(Control|Presenter)$#', '', $control::class) . 'Template';
unset($params[$control instanceof Presenter ? 'control' : 'presenter']);
unset($params[$control instanceof UI\Presenter ? 'control' : 'presenter']);
}

if ($parent) {
if (!class_exists($parent)) {
$blueprint->printHeader("{templatePrint}: Class '$parent' doesn't exist.");
return;
$class = $bp->generateTemplateClass($params, $name, $parentClass);
$code = (string) $class->getNamespace();

$bp->printBegin();
$bp->printCode($code);

if ($control instanceof UI\Control) {
$file = dirname((new \ReflectionClass($control))->getFileName()) . '/' . $class->getName() . '.php';
$_file = '<span style="user-select: all">' . htmlspecialchars($file) . '</span>';
if (file_exists($file)) {
echo "unsaved, file $_file already exists";
} else {
echo "saved to file $_file";
file_put_contents($file, "<?php\n\ndeclare(strict_types=1);\n\n$code");
}

$params = array_diff_key($params, get_class_vars($parent));
}

$funcs = array_diff_key((array) $template->global->fn, (new Latte\Essential\CoreExtension)->getFunctions());
unset($funcs['isLinkCurrent'], $funcs['isModuleCurrent']);

$namespace = new Php\PhpNamespace(Php\Helpers::extractNamespace($name));
$class = $namespace->addClass(Php\Helpers::extractShortName($name));
$class->setExtends($parent ?: Template::class);

$blueprint->addProperties($class, $params);
$blueprint->addFunctions($class, $funcs);

$end = $blueprint->printCanvas();
$blueprint->printCode((string) $namespace);
echo $end;
$bp->printEnd();
exit;
}
}
10 changes: 10 additions & 0 deletions src/Bridges/ApplicationLatte/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class Template implements Nette\Application\UI\Template
{
private ?string $file = null;
private ?string $blueprint;


public function __construct(
Expand All @@ -39,6 +40,9 @@ final public function getLatte(): Latte\Engine
public function render(?string $file = null, array $params = []): void
{
Nette\Utils\Arrays::toObject($params, $this);
if (isset($this->blueprint)) {
Nodes\TemplatePrintNode::printClass($this->getParameters(), $this->blueprint);
}
$this->latte->render($file ?: $this->file, $this);
}

Expand Down Expand Up @@ -139,6 +143,12 @@ final public function getParameters(): array
}


public function blueprint(?string $parentClass = null): void
{
$this->blueprint = $parentClass ?? self::class;
}


/**
* Prevents unserialization.
*/
Expand Down

0 comments on commit ec91641

Please sign in to comment.