Skip to content
Permalink
Browse files

added macro {templatePrint}

  • Loading branch information
dg committed Jan 8, 2020
1 parent 32cc5a3 commit a0ade8863ce555d4f0d22bea42ad42ef96a525ba
Showing with 106 additions and 2 deletions.
  1. +2 −2 src/Latte/Engine.php
  2. +11 −0 src/Latte/Macros/CoreMacros.php
  3. +93 −0 src/Latte/Runtime/TemplatePrinter.php
@@ -72,7 +72,7 @@ public function __construct()
*/
public function render(string $name, array $params = [], string $block = null): void
{
$this->createTemplate($name, $params + ['_renderblock' => $block])
$this->createTemplate($name, $params + ($block ? ['_renderblock' => $block] : []))
->render();
}

@@ -82,7 +82,7 @@ public function render(string $name, array $params = [], string $block = null):
*/
public function renderToString(string $name, array $params = [], string $block = null): string
{
$template = $this->createTemplate($name, $params + ['_renderblock' => $block]);
$template = $this->createTemplate($name, $params + ($block ? ['_renderblock' => $block] : []));
return $template->capture([$template, 'render']);
}

@@ -70,6 +70,7 @@ public static function install(Latte\Compiler $compiler): void

$me->addMacro('varType', [$me, 'macroVarType'], null, null, self::ALLOWED_IN_HEAD);
$me->addMacro('templateType', [$me, 'macroTemplateType'], null, null, self::ALLOWED_IN_HEAD);
$me->addMacro('templatePrint', [$me, 'macroTemplatePrint'], null, null, self::ALLOWED_IN_HEAD);
}


@@ -545,4 +546,14 @@ public function macroTemplateType(MacroNode $node, PhpWriter $writer)
throw new CompileException('Missing class name in {templateType} macro.');
}
}


/**
* {templatePrint [ClassName]}
*/
public function macroTemplatePrint(MacroNode $node, PhpWriter $writer)
{
$class = $node->tokenizer->fetchWord() ?: null;
return $writer->write('ob_end_clean(); header("Content-Type: text/plain"); echo (new Latte\Runtime\TemplatePrinter)->print($this, %var); exit;', $class);
}
}
@@ -0,0 +1,93 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Latte\Runtime;

use Latte;


/**
* Generates blueprint of template class.
*/
class TemplatePrinter
{
use Latte\Strict;

public function print(Template $template, string $class = null): string
{
$class = $class ?: 'Template';
$types = array_map([self::class, 'getType'], $template->getParameters());
return
"class $class\n"
. "{\n" . self::printProperties($types, true) . "\n}\n"
. "\n\n"
. "/**\n" . self::printProperties($types, false) . "\n */\n"
. "class $class\n"
. "{\n}\n";
}


public static function printProperties(array $types, bool $asProps): string
{
$keys = array_keys($types);
if ($asProps) {
$items = PHP_VERSION_ID >= 70400
? array_map(function ($key, $type) { return "\tpublic $type $$key;"; }, $keys, $types)
: array_map(function ($key, $type) { return "\t/** @var $type */\n\tpublic $$key;\n"; }, $keys, $types);
} else {
$items = array_map(function ($key, $type) { return " * @property $type $$key"; }, $keys, $types);
}
return implode("\n", $items);
}


public static function printFunction(string $name, callable $callback, bool $asProps): string
{
if (is_string($callback) && strpos($callback, '::')) {
$rf = new \ReflectionMethod($callback);
} elseif (is_array($callback)) {
$rf = new \ReflectionMethod($callback[0], $callback[1]);
} elseif (is_object($callback) && !$callback instanceof \Closure) {
$rf = new \ReflectionMethod($callback, '__invoke');
} else {
$rf = new \ReflectionFunction($callback);
}

$params = [];
foreach ($rf->getParameters() as $param) {
$params[] = ($param->getType() ? $param->getType()->getName() . ' ' : '')
. '$' . $param->getName()
. ($param->isDefaultValueAvailable() ? ' = ' . preg_replace('#\s#', '', var_export($param->getDefaultValue(), true)) : '');
}
$type = $rf->getReturnType() ? $rf->getReturnType()->getName() : '';
return $asProps
? ' * @method ' . ($type ?: 'mixed') . " $name(" . implode(', ', $params) . ')'
: "$name(" . implode(', ', $params) . ')' . ($type ? ": $type" : '');
}


public static function getType($value): string
{
if (is_object($value)) {
return '\\' . get_class($value);
} elseif (is_int($value)) {
return 'int';
} elseif (is_float($value)) {
return 'float';
} elseif (is_string($value)) {
return 'string';
} elseif (is_bool($value)) {
return 'bool';
} elseif (is_array($value)) {
return 'array';
} else {
return '';
}
}
}

0 comments on commit a0ade88

Please sign in to comment.
You can’t perform that action at this time.