Skip to content

Commit

Permalink
refactor: refacting the template file load logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 8, 2021
1 parent d6bec78 commit 98a3460
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 183 deletions.
123 changes: 123 additions & 0 deletions app/Lib/Template/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Inhere\Kite\Lib\Template;

use Inhere\Kite\Lib\Template\Contract\TemplateInterface;
use InvalidArgumentException;
use Toolkit\FsUtil\File;
use Toolkit\Stdlib\Obj;
use function is_file;
use function strpos;

/**
* Class AbstractTemplate
Expand All @@ -18,6 +22,25 @@ abstract class AbstractTemplate implements TemplateInterface
*/
protected array $globalVars = [];

/**
* allow template file ext list. should start with '.'
*
* @var string[]
*/
protected array $allowExt = ['.php', '.tpl'];

/**
* @var string
*/
protected string $tplDir = '';

/**
* manual set view files
*
* @var array
*/
protected array $tplFiles = [];

/**
* @return static
*/
Expand All @@ -36,6 +59,106 @@ public function __construct(array $config = [])
Obj::init($this, $config);
}

/**
* @param string $tplName
*
* @return string
*/
protected function findTplFile(string $tplName): string
{
if (is_file($tplName)) {
return $tplName;
}

if (isset($this->tplFiles[$tplName])) {
return $this->tplFiles[$tplName];
}

if (!$this->tplDir) {
throw new InvalidArgumentException("no such template file: $tplName");
}

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

if (strpos($tplName, '.') > 0) {
$suffix = File::getExtension($tplName);
}

// is an exists file
if ($suffix) {
if (is_file($tplFile)) {
return $tplFile;
}
} else {
foreach ($this->allowExt as $ext) {
$filename = $tplFile . $ext;
if (is_file($filename)) {
return $filename;
}
}
}

throw new InvalidArgumentException("no such template file: $tplName");
}

/**
* @param string $tplName
* @param string $filePath
*/
public function addTplFile(string $tplName, string $filePath): void
{
$this->tplFiles[$tplName] = $filePath;
}

/**
* @return array
*/
public function getTplFiles(): array
{
return $this->tplFiles;
}

/**
* @param array $tplFiles
*/
public function setTplFiles(array $tplFiles): void
{
$this->tplFiles = $tplFiles;
}

/**
* @return string
*/
public function getTplDir(): string
{
return $this->tplDir;
}

/**
* @param string $tplDir
*/
public function setTplDir(string $tplDir): void
{
$this->tplDir = $tplDir;
}

/**
* @return string[]
*/
public function getAllowExt(): array
{
return $this->allowExt;
}

/**
* @param string[] $allowExt
*/
public function setAllowExt(array $allowExt): void
{
$this->allowExt = $allowExt;
}

/**
* @return array
*/
Expand Down
19 changes: 17 additions & 2 deletions app/Lib/Template/Compiler/PregCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inhere\Kite\Lib\Template\Compiler;

use Toolkit\FsUtil\File;
use function addslashes;
use function array_keys;
use function explode;
Expand Down Expand Up @@ -48,6 +49,18 @@ public function setOpenCloseTag(string $open, string $close): self
return $this;
}

/**
* @param string $tplFile
*
* @return string
*/
public function compileFile(string $tplFile): string
{
$tplCode = File::readAll($tplFile);

return $this->compile($tplCode);
}

/**
* @param string $tplCode
*
Expand Down Expand Up @@ -117,10 +130,12 @@ public function parseCodeBlock(string $block): string
$open = self::PHP_TAG_OPEN . ($isInline ? ' ' : "\n");
$close = ($isInline ? ' ' : "\n") . self::PHP_TAG_CLOSE;

// echo statement
if ($trimmed[0] === '=') {
$type = Token::T_ECHO;
$open = self::PHP_TAG_ECHO;
} elseif (str_starts_with($trimmed, 'echo ')) { // echo statement
} elseif (str_starts_with($trimmed, 'echo ')) {
// echo statement
$type = Token::T_ECHO;
$open = self::PHP_TAG_OPEN . ' ';
} elseif ($isInline && ($tryType = Token::tryAloneToken($trimmed))) {
Expand All @@ -142,7 +157,7 @@ public function parseCodeBlock(string $block): string
$endChar = $trimmed[strlen($trimmed) - 1];

// not in raw php code AND end char != :
if ($endChar !== '}' && $endChar !== '{' && $endChar !== ':') {
if ($endChar !== '}' && $endChar !== '{' && $endChar !== ';' && $endChar !== ':') {
$close = ': ' . self::PHP_TAG_CLOSE;
}
}
Expand Down
10 changes: 9 additions & 1 deletion app/Lib/Template/Contract/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
interface CompilerInterface
{

/**
* @param string $open
* @param string $close
Expand All @@ -26,4 +25,13 @@ public function setOpenCloseTag(string $open, string $close): self;
* @return string
*/
public function compile(string $tplCode): string;

/**
* compile template file contents to raw PHP template codes
*
* @param string $tplFile
*
* @return string
*/
public function compileFile(string $tplFile): string;
}
40 changes: 20 additions & 20 deletions app/Lib/Template/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
use Inhere\Kite\Lib\Template\Compiler\PregCompiler;
use Inhere\Kite\Lib\Template\Contract\CompilerInterface;
use Inhere\Kite\Lib\Template\Contract\EasyTemplateInterface;
use InvalidArgumentException;
use Toolkit\FsUtil\File;
use function file_exists;

/**
* Class EasyTemplate
Expand All @@ -16,11 +13,6 @@
*/
class EasyTemplate extends TextTemplate implements EasyTemplateInterface
{
/**
* @var string[]
*/
protected array $allowExt = ['.php', '.tpl'];

/**
* @var CompilerInterface
*/
Expand All @@ -39,11 +31,24 @@ public function __construct(array $config = [])
$this->compiler->addDirective(
'include',
function (string $body, string $name) {
return 'echo $this->' . $name . $body;
return '$this->' . $name . $body;
}
);
}

/**
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
public function renderFile(string $tplFile, array $tplVars): string
{
$phpFile = $this->compileFile($tplFile);

return $this->doRenderFile($phpFile, $tplVars);
}

/**
* @param string $tplCode
* @param array $tplVars
Expand All @@ -52,22 +57,20 @@ function (string $body, string $name) {
*/
public function renderString(string $tplCode, array $tplVars): string
{
$tplCode = $this->compileCode($tplCode);
$tplCode = $this->compiler->compile($tplCode);

return parent::renderString($tplCode, $tplVars);
}

/**
* @param string $tplFile
* @param array $tplVars
*
* @return string
*/
public function renderFile(string $tplFile, array $tplVars): string
protected function include(string $tplFile, array $tplVars): void
{
$phpFile = $this->compileFile($tplFile);

return $this->doRenderFile($phpFile, $tplVars);
echo $this->doRenderFile($phpFile, $tplVars);
}

/**
Expand All @@ -76,7 +79,7 @@ public function renderFile(string $tplFile, array $tplVars): string
*
* @return string
*/
protected function include(string $tplFile, array $tplVars): string
protected function renderInclude(string $tplFile, array $tplVars): string
{
$phpFile = $this->compileFile($tplFile);

Expand All @@ -90,13 +93,10 @@ protected function include(string $tplFile, array $tplVars): string
*/
public function compileFile(string $tplFile): string
{
if (!file_exists($tplFile)) {
throw new InvalidArgumentException('no such template file:' . $tplFile);
}
$tplFile = $this->findTplFile($tplFile);

$tplCode = File::readAll($tplFile);
// compile contents
$tplCode = $this->compileCode($tplCode);
$tplCode = $this->compiler->compileFile($tplFile);

// generate temp php file
return $this->genTempPhpFile($tplCode);
Expand Down

0 comments on commit 98a3460

Please sign in to comment.