Skip to content

Commit

Permalink
feat: cache Templator when using file_get_contents() (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
SonyPradana committed Sep 14, 2024
1 parent 37bb6cf commit 439fcb3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/System/View/InteractWithCacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace System\View;

trait InteractWithCacheTrait
{
/**
* Get contents using cache first.
*/
private function getContents(string $file_name): string
{
if (false === array_key_exists($file_name, self::$cache)) {
self::$cache[$file_name] = file_get_contents($file_name);
}

return self::$cache[$file_name];
}
}
14 changes: 13 additions & 1 deletion src/System/View/Templator/IncludeTemplator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
namespace System\View\Templator;

use System\View\AbstractTemplatorParse;
use System\View\InteractWithCacheTrait;

class IncludeTemplator extends AbstractTemplatorParse
{
use InteractWithCacheTrait;

private int $maks_dept = 5;

/**
* File get content cached.
*
* @var array<string, string>
*/
private static array $cache = [];

public function maksDept(int $maks_dept): self
{
$this->maks_dept = $maks_dept;
Expand All @@ -19,6 +29,8 @@ public function maksDept(int $maks_dept): self

public function parse(string $template): string
{
self::$cache = [];

return preg_replace_callback(
'/{%\s*include\s*\(\s*[\'"]([^\'"]+)[\'"]\s*\)\s*%}/',
function ($matches) {
Expand All @@ -27,7 +39,7 @@ function ($matches) {
}

$templatePath = $this->finder->find($matches[1]);
$includedTemplate = file_get_contents($templatePath);
$includedTemplate = $this->getContents($templatePath);

if ($this->maks_dept === 0) {
return $includedTemplate;
Expand Down
14 changes: 13 additions & 1 deletion src/System/View/Templator/SectionTemplator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@

use System\Text\Str;
use System\View\AbstractTemplatorParse;
use System\View\InteractWithCacheTrait;

class SectionTemplator extends AbstractTemplatorParse
{
use InteractWithCacheTrait;

/** @var array<string, mixed> */
private $sections = [];

/**
* File get content cached.
*
* @var array<string, string>
*/
private static array $cache = [];

public function parse(string $template): string
{
self::$cache = [];

preg_match('/{%\s*extend\s*\(\s*[\'"]([^\'"]+)[\'"]\s*\)\s*%}/', $template, $matches_layout);
if (!array_key_exists(1, $matches_layout)) {
return $template;
Expand All @@ -24,7 +36,7 @@ public function parse(string $template): string
}

$templatePath = $this->finder->find($matches_layout[1]);
$layout = file_get_contents($templatePath);
$layout = $this->getContents($templatePath);

$template = preg_replace_callback(
'/{%\s*section\s*\(\s*[\'"]([^\'"]+)[\'"]\s*,\s*[\'"]([^\'"]+)[\'"]\s*\)\s*%}/s',
Expand Down

0 comments on commit 439fcb3

Please sign in to comment.