WIP on creating new CodeFrames using nette

This commit is contained in:
Simon Wisselink
2023-01-25 13:03:38 +01:00
parent 46dfed3837
commit d586f7555f
4 changed files with 69 additions and 4 deletions

View File

@@ -31,7 +31,8 @@
},
"require": {
"php": "^7.2 || ^8.0",
"symfony/polyfill-mbstring": "^1.27"
"symfony/polyfill-mbstring": "^1.27",
"nette/php-generator": "^3.6"
},
"autoload": {
"psr-4" : {

25
src/CodeFrame/Base.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace Smarty\CodeFrame;
abstract class Base {
/**
* @var \Smarty\Smarty
*/
private $smarty;
public function __construct(\Smarty\Smarty $smarty) {
$this->smarty = $smarty;
}
public function isFresh(\Smarty\Template $template): bool {
return $template->isFresh($this->getProperties(), $this->isCache());
}
protected function isCache(): bool {
return false;
}
abstract protected function getProperties(): array;
}

10
src/CodeFrame/Cached.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
namespace Smarty\CodeFrame;
abstract class Cached extends Base {
protected function isCache(): bool {
return true;
}
}

View File

@@ -43,8 +43,29 @@ class CodeFrame
$cache = false,
\Smarty\Compiler\Template $compiler = null
) {
$className = ($cache ? 'Cached' : 'Compiled') . str_replace(array('.', ','), '_', uniqid('', true));
$properties = [];
$properties['version'] = \Smarty\Smarty::SMARTY_VERSION;
$file = new \Nette\PhpGenerator\PhpFile;
$file->addComment('This file is auto-generated.');
$class = $file->addClass($className);
$class
->setFinal()
->setExtends($cache ? \Smarty\CodeFrame\Cached::class : \Smarty\CodeFrame\Base::class)
->addComment(sprintf(
"Created on %s from '%s'",
$properties[ 'version' ],
date("Y-m-d H:i:s"),
str_replace('*/', '* /', $this->_template->getSource()->filepath)
));
$dumper = new \Nette\PhpGenerator\Dumper;
// build property code
$properties[ 'version' ] = \Smarty\Smarty::SMARTY_VERSION;
$properties[ 'unifunc' ] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
if (!$cache) {
$properties[ 'has_nocache_code' ] = $this->_template->getCompiled()->getNocacheCode();
@@ -55,8 +76,16 @@ class CodeFrame
$properties[ 'file_dependency' ] = $this->_template->getCached()->file_dependency;
$properties[ 'cache_lifetime' ] = $this->_template->cache_lifetime;
}
$output = sprintf(
"<?php\n/* Smarty version %s, created on %s\n from '%s' */\n\n",
$class->addMethod('getProperties')
->setProtected()
->setReturnType('array') // method return type
->setBody('return ' . $dumper->dump($properties) . ';');
$output = (string) $file;
$output .= sprintf(
"\n/* Created on %s\n from '%s' */\n\n",
$properties[ 'version' ],
date("Y-m-d H:i:s"),
str_replace('*/', '* /', $this->_template->getSource()->filepath)