Skip to content

Commit

Permalink
prof: only compile on cached tmp file not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2021
1 parent 2966299 commit e631751
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
29 changes: 12 additions & 17 deletions src/EasyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use PhpPkg\EasyTpl\Contract\EasyTemplateInterface;
use InvalidArgumentException;
use Toolkit\FsUtil\File;
use function file_exists;
use function file_get_contents;
use function is_string;
use function md5;

/**
* Class EasyTemplate
Expand All @@ -27,7 +30,7 @@ class EasyTemplate extends PhpTemplate implements EasyTemplateInterface
private CompilerInterface $compiler;

/**
* custom filter for handle result(only for echo body).
* Custom filter for handle result(only for echo body).
*
* ```php
* $p->addFilter('upper', 'strtoupper');
Expand Down Expand Up @@ -150,12 +153,16 @@ protected function renderInclude(string $tplFile, array $tplVars): string
public function compileFile(string $tplFile): string
{
$tplFile = $this->findTplFile($tplFile);
$tplCode = file_get_contents($tplFile);
$tmpFile = $this->tmpFilepath(md5($tplCode), File::getName($tplFile, true));

// compile contents
$phpCode = $this->compiler->compileFile($tplFile);
// only compile on cached tmp file not exists
if (!file_exists($tmpFile)) {
$phpCode = $this->compiler->compile($tplCode);
$tmpFile = $this->genTmpPhpFile($phpCode, $tmpFile);
}

// generate temp php file
return $this->genTmpPhpFile($phpCode, File::getName($tplFile, true));
return $tmpFile;
}

/**
Expand Down Expand Up @@ -199,7 +206,6 @@ public function addFilters(array $filters): self
foreach ($filters as $name => $filterFn) {
$this->addFilter($name, $filterFn);
}

return $this;
}

Expand Down Expand Up @@ -275,17 +281,6 @@ public function setOpenCloseTag(string $open, string $close): self
return $this;
}

/**
* @param CompilerInterface $compiler
*
* @return EasyTemplate
*/
public function setCompiler(CompilerInterface $compiler): self
{
$this->compiler = $compiler;
return $this;
}

/**
* @return CompilerInterface
*/
Expand Down
39 changes: 27 additions & 12 deletions src/PhpTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Toolkit\FsUtil\Dir;
use Toolkit\Stdlib\OS;
use function array_merge;
use function date;
use function dirname;
use function extract;
use function file_exists;
use function file_put_contents;
Expand All @@ -17,7 +19,6 @@
use function ob_start;
use function sprintf;
use const EXTR_OVERWRITE;
use const PHP_EOL;

/**
* Class PhpTemplate
Expand Down Expand Up @@ -83,7 +84,6 @@ protected function doRenderFile(string $tplFile, array $tplVars): string
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) {
Expand All @@ -92,31 +92,46 @@ protected function doRenderFile(string $tplFile, array $tplVars): string
}
}

/**
* @param string $hashId template contents hashID
* @param string $prefix
*
* @return string
*/
protected function tmpFilepath(string $hashId, string $prefix = ''): string
{
$prefix = $prefix ?: 'phpTpl';
$tmpDir = $this->tmpDir ?: OS::getTempDir() . '/php-tpl-code';
$tmpFile = sprintf('%s/%s_%s.php', $tmpDir, $prefix, $hashId);

$this->tmpPhpFile = $tmpFile;
return $tmpFile;
}

/**
* Generate tmp php template file
*
* @param string $phpCode
* @param string $prefix
* @param string $tmpFile
*
* @return string
*/
protected function genTmpPhpFile(string $phpCode, string $prefix = ''): string
protected function genTmpPhpFile(string $phpCode, string $tmpFile = ''): string
{
$tmpDir = $this->tmpDir ?: OS::getTempDir() . '/php-tpl-code';
$prefix = $prefix ?: 'phpTpl';
$tmpFile = sprintf('%s/%s_%s.php', $tmpDir, $prefix, md5($phpCode));
if (!$tmpFile) {
$tmpFile = $this->tmpFilepath(md5($phpCode));
}

if (!file_exists($tmpFile)) {
Dir::create($tmpDir);
Dir::create(dirname($tmpFile));

// write contents
$num = file_put_contents($tmpFile, $phpCode . PHP_EOL);
$date = date('Y/m/d H:i:s');
$num = file_put_contents($tmpFile, $phpCode . "\n<?php // generated on $date ?>");
if ($num < 1) {
throw new RuntimeException('write template contents to temp file error');
throw new RuntimeException('write contents to tmp file failed');
}
}

$this->tmpPhpFile = $tmpFile;
return $tmpFile;
}

Expand Down

0 comments on commit e631751

Please sign in to comment.