parent
32567ea58e
commit
bd1c35d830
@ -0,0 +1,35 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
namespace CodeHydrater; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
class php_file_cache { |
||||||
|
protected $cache_path; |
||||||
|
|
||||||
|
public function __construct($path) { |
||||||
|
$this->cache_path = rtrim($path, '/') . '/'; |
||||||
|
if (!is_dir($this->cache_path)) { |
||||||
|
mkdir($this->cache_path, 0775, true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function get($key) { |
||||||
|
$file = $this->cache_path . md5($key) . '.cache.php'; |
||||||
|
if (file_exists($file)) { |
||||||
|
return unserialize(file_get_contents($file)); |
||||||
|
} |
||||||
|
return null; |
||||||
|
} |
||||||
|
|
||||||
|
public function set($key, $value) { |
||||||
|
$file = $this->cache_path . md5($key) . '.cache.php'; |
||||||
|
file_put_contents($file, serialize($value)); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,78 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace CodeHydrater\services; |
||||||
|
|
||||||
|
use CodeHydrater\bootstrap\registry as Reg; |
||||||
|
use CodeHydrater\php_file_cache as FileCache; |
||||||
|
use Liquid\{Liquid, Template, Context}; |
||||||
|
use Liquid\Cache\Local; |
||||||
|
|
||||||
|
final class liquid_templates { |
||||||
|
private $use_local_cache = false; |
||||||
|
private $liquid; |
||||||
|
private $dir; |
||||||
|
private $extension = 'tpl'; |
||||||
|
|
||||||
|
public function __construct(string $template_extension = 'tpl') { |
||||||
|
$this->extension = $template_extension; |
||||||
|
if (! Reg::get('loader')->is_loaded('Liquid')) { |
||||||
|
Reg::get('loader')->add_namespace('Liquid', CodeHydrater_PROJECT . 'vendor/liquid/liquid/src/Liquid'); |
||||||
|
} |
||||||
|
|
||||||
|
Liquid::set('INCLUDE_SUFFIX', $template_extension); |
||||||
|
Liquid::set('INCLUDE_PREFIX', ''); |
||||||
|
|
||||||
|
$this->dir = CodeHydrater_PROJECT . 'views/liquid'; |
||||||
|
$this->liquid = new Template($this->dir); |
||||||
|
} |
||||||
|
|
||||||
|
public function whitespace_control() { |
||||||
|
/* Force whitespace control to be used by switching tags from {%- to {% |
||||||
|
* Also, will error out if you try {%- |
||||||
|
* Need to figure out how to turn back on whitespaces with {%@ errors! not important... |
||||||
|
*/ |
||||||
|
Liquid::set('TAG_START', '(?:{%@)|\s*{%'); |
||||||
|
Liquid::set('TAG_END', '(?:@%}|%}\s*)'); |
||||||
|
} |
||||||
|
|
||||||
|
public function parse(string $source) { |
||||||
|
return $this->liquid->parse($source); |
||||||
|
} |
||||||
|
|
||||||
|
public function parse_file(string $file) { |
||||||
|
$safe_file = \CodeHydrater\security::filter_uri($file); |
||||||
|
if ($this->use_local_cache) { |
||||||
|
$cache = new FileCache(BaseDir . "/protected/runtime/liquid_cache"); |
||||||
|
|
||||||
|
$templateName = $safe_file . "." . $this->extension; |
||||||
|
$templatePath = $this->dir . "/". $safe_file . "." . $this->extension; |
||||||
|
|
||||||
|
$templateSource = file_get_contents($templatePath); |
||||||
|
$cached = $cache->get($templateName); |
||||||
|
if (!$cached) { |
||||||
|
$this->liquid->parseFile($safe_file); |
||||||
|
$cache->set($templateName, $this->liquid); |
||||||
|
} else { |
||||||
|
$this->liquid = $cached; |
||||||
|
} |
||||||
|
} else { |
||||||
|
$this->liquid->parseFile($safe_file); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function render(array $assigns = array(), $filters = null, array $registers = array()): string { |
||||||
|
return $this->liquid->render($assigns, $filters, $registers); |
||||||
|
} |
||||||
|
|
||||||
|
public function get_engine() { |
||||||
|
return $this->liquid; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types = 1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
namespace CodeHydrater\services; |
||||||
|
|
||||||
|
class twig { |
||||||
|
public static function init() { |
||||||
|
\CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Twig", CodeHydrater_PROJECT. "/vendor/twig/twig/src"); |
||||||
|
\CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Symfony\Polyfill\Mbstring", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-mbstring"); |
||||||
|
\CodeHydrater\bootstrap\registry::get('loader')->add_namespace("Symfony\Polyfill\Ctype", CodeHydrater_PROJECT. "/vendor/symfony/polyfill-ctype"); |
||||||
|
$loader = new \Twig\Loader\FilesystemLoader(CodeHydrater_PROJECT. "views/twig"); |
||||||
|
$twig = new \Twig\Environment($loader, [ |
||||||
|
'cache' => BaseDir . "/protected/runtime/compilation_cache", |
||||||
|
]); |
||||||
|
return $twig; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue