includeCache directive
This commit is contained in:
parent
153d3fe0b8
commit
0e92b78d33
2 changed files with 30 additions and 5 deletions
|
@ -1,8 +1,17 @@
|
|||
<?php
|
||||
class Cache {
|
||||
protected static $includeCache = [];
|
||||
protected static $templateCache = [];
|
||||
protected static $resourceCache = [];
|
||||
|
||||
public static function getIncludeCache(string $name) {
|
||||
return isset(self::$includeCache[$name]) ? self::$includeCache[$name] : null;
|
||||
}
|
||||
|
||||
public static function setIncludeCache(string $name, $data) {
|
||||
self::$includeCache[$name] = $data;
|
||||
}
|
||||
|
||||
public static function getTemplateFromCache(string $name) {
|
||||
if (isset(self::$templateCache[$name])) {
|
||||
return self::$templateCache[$name];
|
||||
|
|
|
@ -30,16 +30,32 @@ class Template {
|
|||
$class->pretransform();
|
||||
// Replace includes of templates
|
||||
$matches = array();
|
||||
preg_match_all("/{{include (.*\.html)(.*)}}/", $contents, $matches);
|
||||
preg_match_all("/{{(include|includeCached) (.*\.html)(.*)}}/", $contents, $matches);
|
||||
for ($i = 0; $i < count($matches[1]); $i++) {
|
||||
$matchString = $matches[0][$i];
|
||||
$match = $matches[1][$i];
|
||||
$options = trim($matches[2][$i]);
|
||||
$method = $matches[1][$i];
|
||||
$match = $matches[2][$i];
|
||||
$options = trim($matches[3][$i]);
|
||||
|
||||
if ($method === "includeCached") {
|
||||
$crc = hash("crc32b", $match . $options);
|
||||
$include = Cache::getIncludeCache($crc);
|
||||
if (!is_null($include)) {
|
||||
$contents = str_replace($matchString, $include, $contents);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
$include = new Template(dirname($this->template) . "/" . $match, $this->data);
|
||||
$contents = str_replace($matchString, $include->render(
|
||||
$rendered = $include->render(
|
||||
!empty($options) ? json_decode($options, false, 512, JSON_THROW_ON_ERROR) : null
|
||||
), $contents);
|
||||
);
|
||||
|
||||
if ($method === "includeCached") {
|
||||
Cache::setIncludeCache($crc, $rendered);
|
||||
}
|
||||
|
||||
$contents = str_replace($matchString, $rendered, $contents);
|
||||
}
|
||||
|
||||
$class->transform();
|
||||
|
|
Loading…
Reference in a new issue