From 1198731955ae27792e1caecd257c861d77d918b0 Mon Sep 17 00:00:00 2001 From: Michal Lulco Date: Fri, 25 Jun 2021 23:50:37 +0200 Subject: [PATCH] POC write output for varType and templateType macros --- src/Latte/Macros/CoreMacros.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Latte/Macros/CoreMacros.php b/src/Latte/Macros/CoreMacros.php index b0e588319..873afb0ee 100644 --- a/src/Latte/Macros/CoreMacros.php +++ b/src/Latte/Macros/CoreMacros.php @@ -16,6 +16,8 @@ use Latte\MacroNode; use Latte\PhpHelpers; use Latte\PhpWriter; +use ReflectionClass; +use ReflectionException; /** @@ -838,7 +840,7 @@ public function macroParameters(MacroNode $node, PhpWriter $writer): void /** * {varType type $var} */ - public function macroVarType(MacroNode $node): void + public function macroVarType(MacroNode $node, PhpWriter $writer): string { if ($node->modifiers) { $node->setArgs($node->args . $node->modifiers); @@ -851,6 +853,10 @@ public function macroVarType(MacroNode $node): void if (!$type || !$variable) { throw new CompileException('Unexpected content, expecting {varType type $var}.'); } + + $varName = $variable[0]; + $paramName = ltrim($varName, '$'); + return $writer->write("/** @var $type */\n$varName = \$this->params['$paramName']"); } @@ -869,12 +875,32 @@ public function macroVarPrint(MacroNode $node): string /** * {templateType ClassName} */ - public function macroTemplateType(MacroNode $node): void + public function macroTemplateType(MacroNode $node, PhpWriter $writer): string { if (!$this->getCompiler()->isInHead()) { throw new CompileException($node->getNotation() . ' is allowed only in template header.'); } $node->validate('class name'); + + $templateTypes = []; + try { + $reflectionClass = new ReflectionClass($node->args); + foreach ($reflectionClass->getProperties() as $property) { + $propertyName = $property->getName(); + $type = $property->getType(); + + $templateType = ''; + if ($type) { + $templateType .= "/** @var {$type->getName()} */\n"; + } + $templateType .= "\$$propertyName = \$this->params['$propertyName'];"; + $templateTypes[] = $templateType; + } + } catch (ReflectionException $e) { + + } + + return $writer->write(implode("\n", $templateTypes)); }