From 28377a26e9508e43cf59ab8a736a9ba8c7bde61a Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Wed, 25 Jan 2023 13:03:38 +0100 Subject: [PATCH] WIP on creating new CodeFrames using nette --- composer.json | 3 ++- src/CodeFrame/Base.php | 25 +++++++++++++++++++++++++ src/CodeFrame/Cached.php | 10 ++++++++++ src/Compiler/CodeFrame.php | 35 ++++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/CodeFrame/Base.php create mode 100644 src/CodeFrame/Cached.php diff --git a/composer.json b/composer.json index b62c0559..3d8bde76 100644 --- a/composer.json +++ b/composer.json @@ -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" : { diff --git a/src/CodeFrame/Base.php b/src/CodeFrame/Base.php new file mode 100644 index 00000000..061244e5 --- /dev/null +++ b/src/CodeFrame/Base.php @@ -0,0 +1,25 @@ +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; +} \ No newline at end of file diff --git a/src/CodeFrame/Cached.php b/src/CodeFrame/Cached.php new file mode 100644 index 00000000..d08b1001 --- /dev/null +++ b/src/CodeFrame/Cached.php @@ -0,0 +1,10 @@ +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( - "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)