From da70490ab43b63a179f349c1a9726504df076f36 Mon Sep 17 00:00:00 2001 From: Inhere Date: Wed, 10 Nov 2021 14:56:16 +0800 Subject: [PATCH] update: update easy template gen tmp file logic --- app/Helper/KiteUtil.php | 8 +- app/Lib/Template/AbstractTemplate.php | 1 - app/Lib/Template/EasyTemplate.php | 13 +- app/Lib/Template/HtmlTemplate.php | 16 --- app/Lib/Template/README.md | 5 - app/Lib/Template/TextTemplate.php | 122 ++---------------- app/Lib/Template/easy-template.md | 42 ------ .../Lib/Template/TextTemplateTest.php | 4 +- 8 files changed, 26 insertions(+), 185 deletions(-) delete mode 100644 app/Lib/Template/HtmlTemplate.php delete mode 100644 app/Lib/Template/README.md diff --git a/app/Helper/KiteUtil.php b/app/Helper/KiteUtil.php index f359216..e00ad86 100644 --- a/app/Helper/KiteUtil.php +++ b/app/Helper/KiteUtil.php @@ -4,6 +4,7 @@ use Inhere\Kite\Kite; use Inhere\Kite\Lib\Template\EasyTemplate; +use Inhere\Kite\Lib\Template\TextTemplate; use Toolkit\FsUtil\FS; use Toolkit\Stdlib\OS; use Toolkit\Stdlib\Str; @@ -18,10 +19,10 @@ class KiteUtil { - public const NL_CHAR = 'NL'; + public const NL_CHAR = 'NL'; public const SPACE_CHAR = 'SPACE'; - public const STDIN_ALIAS = [ + public const STDIN_ALIAS = [ '@i', '@stdin', 'stdin', @@ -98,8 +99,7 @@ public static function userConfigDir(string $path = ''): string */ public static function newTplEngine(array $config = []): EasyTemplate { - return EasyTemplate::new($config) - ->disableEchoFilter() + return TextTemplate::new($config) ->setPathResolver([Kite::class, 'resolve']) ->configThis(function (EasyTemplate $tpl) { $tpl->tmpDir = Kite::getTmpPath('tplCache'); diff --git a/app/Lib/Template/AbstractTemplate.php b/app/Lib/Template/AbstractTemplate.php index a93571d..99410ba 100644 --- a/app/Lib/Template/AbstractTemplate.php +++ b/app/Lib/Template/AbstractTemplate.php @@ -112,7 +112,6 @@ protected function findTplFile(string $tplName): string $suffix = ''; $tplFile = $this->tplDir . '/' . $tplName; - if (strpos($tplName, '.') > 0) { $suffix = File::getExtension($tplName); } diff --git a/app/Lib/Template/EasyTemplate.php b/app/Lib/Template/EasyTemplate.php index dfc986b..b1d09ba 100644 --- a/app/Lib/Template/EasyTemplate.php +++ b/app/Lib/Template/EasyTemplate.php @@ -6,6 +6,8 @@ use Inhere\Kite\Lib\Template\Contract\CompilerInterface; use Inhere\Kite\Lib\Template\Contract\EasyTemplateInterface; use InvalidArgumentException; +use Toolkit\FsUtil\File; +use function basename; use function is_string; /** @@ -13,8 +15,13 @@ * * @author inhere */ -class EasyTemplate extends TextTemplate implements EasyTemplateInterface +class EasyTemplate extends PhpTemplate implements EasyTemplateInterface { + /** + * @var string[] + */ + protected array $allowExt = ['.html', '.phtml', '.php', '.tpl']; + /** * @var CompilerInterface */ @@ -115,10 +122,10 @@ public function compileFile(string $tplFile): string $tplFile = $this->findTplFile($tplFile); // compile contents - $tplCode = $this->compiler->compileFile($tplFile); + $phpCode = $this->compiler->compileFile($tplFile); // generate temp php file - return $this->genTempPhpFile($tplCode); + return $this->genTmpPhpFile($phpCode, File::getName($tplFile, true)); } /** diff --git a/app/Lib/Template/HtmlTemplate.php b/app/Lib/Template/HtmlTemplate.php deleted file mode 100644 index 69e3bc0..0000000 --- a/app/Lib/Template/HtmlTemplate.php +++ /dev/null @@ -1,16 +0,0 @@ -genTempPhpFile($tplCode); - - return $this->doRenderFile($tempFile, $tplVars); - } - - /** - * @param string $tplFile - * @param array $tplVars - * - * @return string - */ - public function renderFile(string $tplFile, array $tplVars): string - { - return $this->doRenderFile($tplFile, $tplVars); - } - - /** - * @param string $tplFile - * @param array $tplVars - * - * @return string + * @var string[] */ - protected function doRenderFile(string $tplFile, array $tplVars): string - { - if (!file_exists($tplFile)) { - throw new InvalidArgumentException('no such template file:' . $tplFile); - } - - if ($this->globalVars) { - $tplVars = array_merge($this->globalVars, $tplVars); - } - - ob_start(); - extract($tplVars, EXTR_OVERWRITE); - try { - // require \BASE_PATH . '/runtime/go-snippets-0709.tpl.php'; - require $tplFile; - return ob_get_clean(); - } catch (Throwable $e) { - ob_clean(); - throw new RuntimeException($e->getMessage(), $e->getCode(), $e); - } - } + protected array $allowExt = ['.php', '.tpl']; /** - * generate temp php file + * Class constructor. * - * @param string $tplCode - * - * @return string + * @param array $config */ - protected function genTempPhpFile(string $tplCode): string + public function __construct(array $config = []) { - $tmpDir = $this->tmpDir ?: Sys::getTempDir() . '/php-tpl-gen'; - $tmpFile = sprintf('%s/%s-%s.php', $tmpDir, date('ymd_his'), md5($tplCode)); - - if (!file_exists($tmpFile)) { - Dir::create($tmpDir); - - // write contents - $num = file_put_contents($tmpFile, $tplCode . PHP_EOL); - if ($num < 1) { - throw new RuntimeException('write template contents to temp file error'); - } - } + parent::__construct($config); - $this->tmpPhpFile = $tmpFile; - return $tmpFile; + // use raw echo for text template + $this->getCompiler()->disableEchoFilter(); } - - /** - * @return string - */ - public function getTmpPhpFile(): string - { - return $this->tmpPhpFile; - } - } diff --git a/app/Lib/Template/easy-template.md b/app/Lib/Template/easy-template.md index 8af3a98..e69de29 100644 --- a/app/Lib/Template/easy-template.md +++ b/app/Lib/Template/easy-template.md @@ -1,42 +0,0 @@ -# EasyTemplate - -## Using the filters - -You can use the filters in any of your blade templates. - -#### Regular usage: - -```php -{{ 'john' | ucfirst }} // John -``` - -#### Chained usage: - -```php -{{ 'john' | ucfirst | substr:0,1 }} // J -{{ '1999-12-31' | date:'Y/m/d' }} // 1999/12/31 -``` - -#### Passing non-static values: - -```php -{{ $name | ucfirst | substr:0,1 }} -{{ $user['name'] | ucfirst | substr:0,1 }} -{{ $currentUser->name | ucfirst | substr:0,1 }} -{{ getName() | ucfirst | substr:0,1 }} -``` - -#### Passing variables as filter parameters: - -```php -$currency = 'HUF' -{{ '12.75' | currency:$currency }} // HUF 12.75 -``` - -#### Built-in Laravel functionality: - -```php -{{ 'This is a title' | slug }} // this-is-a-title -{{ 'This is a title' | title }} // This Is A Title -{{ 'foo_bar' | studly }} // FooBar -``` \ No newline at end of file diff --git a/test/unittest/Lib/Template/TextTemplateTest.php b/test/unittest/Lib/Template/TextTemplateTest.php index b3aad1b..c332897 100644 --- a/test/unittest/Lib/Template/TextTemplateTest.php +++ b/test/unittest/Lib/Template/TextTemplateTest.php @@ -3,7 +3,7 @@ namespace Inhere\KiteTest\Lib\Template; use Inhere\Kite\Kite; -use Inhere\Kite\Lib\Template\TextTemplate; +use Inhere\Kite\Lib\Template\PhpTemplate; use Inhere\KiteTest\BaseKiteTestCase; use Toolkit\FsUtil\File; use function vdump; @@ -15,7 +15,7 @@ class TextTemplateTest extends BaseKiteTestCase { public function testRenderFile():void { - $t = new TextTemplate(); + $t = new PhpTemplate(); $tplFile = Kite::alias('@resource-tpl/gen-by-parse/gen-go-funcs.tpl'); $tplVars = ['vars' => ['Info', 'Error', 'Warn']];