Skip to content

Commit

Permalink
update: update easy template gen tmp file logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 10, 2021
1 parent 7634022 commit da70490
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 185 deletions.
8 changes: 4 additions & 4 deletions app/Helper/KiteUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand Down Expand Up @@ -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');
Expand Down
1 change: 0 additions & 1 deletion app/Lib/Template/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ protected function findTplFile(string $tplName): string

$suffix = '';
$tplFile = $this->tplDir . '/' . $tplName;

if (strpos($tplName, '.') > 0) {
$suffix = File::getExtension($tplName);
}
Expand Down
13 changes: 10 additions & 3 deletions app/Lib/Template/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
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;

/**
* Class EasyTemplate
*
* @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
*/
Expand Down Expand Up @@ -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));
}

/**
Expand Down
16 changes: 0 additions & 16 deletions app/Lib/Template/HtmlTemplate.php

This file was deleted.

5 changes: 0 additions & 5 deletions app/Lib/Template/README.md

This file was deleted.

122 changes: 10 additions & 112 deletions app/Lib/Template/TextTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,28 @@

namespace Inhere\Kite\Lib\Template;

use InvalidArgumentException;
use RuntimeException;
use Throwable;
use Toolkit\FsUtil\Dir;
use Toolkit\Sys\Sys;
use function array_merge;
use function date;
use function extract;
use function file_exists;
use function file_put_contents;
use function md5;
use function ob_clean;
use function ob_get_clean;
use function ob_start;
use function sprintf;
use const EXTR_OVERWRITE;
use const PHP_EOL;

/**
* Class TextTemplate
* class TextTemplate
*
* @author inhere
* @package Inhere\Kite\Model\Logic
*/
class TextTemplate extends AbstractTemplate
class TextTemplate extends EasyTemplate
{
/**
* The dir for auto generated temp php file
*
* @var string
*/
public string $tmpDir = '';

/**
* The auto generated temp php file
*
* @var string
*/
private string $tmpPhpFile = '';

/**
* @param string $tplCode
* @param array $tplVars
*
* @return string
*/
public function renderString(string $tplCode, array $tplVars): string
{
$tempFile = $this->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;
}

}
42 changes: 0 additions & 42 deletions app/Lib/Template/easy-template.md
Original file line number Diff line number Diff line change
@@ -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
```
4 changes: 2 additions & 2 deletions test/unittest/Lib/Template/TextTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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']];
Expand Down

0 comments on commit da70490

Please sign in to comment.