Skip to content

Commit

Permalink
add caching back in
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Aug 10, 2024
1 parent 06f94e1 commit d49f31d
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
/coverage
/.phpunit.cache
/composer.lock
/composer.lock
/tests/cache
31 changes: 30 additions & 1 deletion src/TagEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace LordSimal\CustomHtmlElements;

use Exception;
use LordSimal\CustomHtmlElements\Error\TagNotFoundException;
use Spatie\StructureDiscoverer\Discover;

Expand All @@ -16,6 +17,8 @@ class TagEngine
protected array $options = [
'component_prefix' => 'c', // Prefix for custom tags
'tag_directories' => [], // Location for tag extensions
'enable_cache' => false, // Enable caching
'cache_dir' => '', // Location for cache files
];

/**
Expand Down Expand Up @@ -46,6 +49,18 @@ public function __construct(array $options = [])
$prefix = $this->options['component_prefix'] ?? 'c';
$this->regex = sprintf($this->regex, $prefix, $prefix, $prefix);
$this->registerTags();

if ($this->options['enable_cache']) {
if (empty($this->options['cache_dir'])) {
throw new Exception('Please set a `cache_dir` config');
}
if (!is_dir($this->options['cache_dir'])) {
throw new Exception('Cache directory does not exist');
}
if (!is_writable($this->options['cache_dir'])) {
throw new Exception('Cache directory is not writable');
}
}
}

/**
Expand Down Expand Up @@ -148,6 +163,14 @@ protected function parseAttributes(string $attributesString): array
*/
protected function renderComponent(string $componentName, array $attributes, string $innerContent = ''): string
{
if ($this->options['enable_cache']) {
$cacheKey = md5($componentName . serialize($attributes) . $innerContent);
$cacheFile = $this->options['cache_dir'] . DIRECTORY_SEPARATOR . $cacheKey . '.html';
if (file_exists($cacheFile)) {
return file_get_contents($cacheFile);
}
}

try {
$class = TagRegistry::getTag(sprintf('%s-%s', $this->options['component_prefix'], $componentName));
$tag = new $class($attributes, $innerContent);
Expand All @@ -160,6 +183,12 @@ protected function renderComponent(string $componentName, array $attributes, str
$tag::$tag = $componentName;
}

return $tag->render();
$html = $tag->render();

if ($this->options['enable_cache']) {
file_put_contents($cacheFile, $html);
}

return $html;
}
}
79 changes: 79 additions & 0 deletions tests/TagEngine/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

namespace LordSimal\CustomHtmlElements\Test\TagEngine;

use FilesystemIterator;
use LordSimal\CustomHtmlElements\TagEngine;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;

/**
* @see \LordSimal\CustomHtmlElements\TagEngine
*/
class CacheTest extends TestCase
{
protected string $cacheDir = '';

protected TagEngine $tagEngine;

protected function setUp(): void
{
$this->cacheDir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
if (!is_dir($this->cacheDir)) {
mkdir($this->cacheDir);
}
$this->tagEngine = new TagEngine([
'tag_directories' => [
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Tags' . DIRECTORY_SEPARATOR,
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR,
],
'enable_cache' => true,
'cache_dir' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR,
]);
}

protected function tearDown(): void
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->cacheDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($files as $fileInfo) {
$todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileInfo->getRealPath());
}

rmdir($this->cacheDir);
}

/**
* Test a tag with a simple attribute (non-self-closing)
*
* @return void
*/
public function testCacheWillBeFilledAndRead(): void
{
$element = '<c-youtube src="RLdsCL4RDf8"></c-youtube>';
$result = $this->tagEngine->parse($element);
$expected = <<<HTML
<iframe width="560" height="315"
src="https://www.youtube.com/embed/RLdsCL4RDf8"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen>
</iframe>
HTML;
$this->assertSame($expected, $result);

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($this->cacheDir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
$this->assertGreaterThan(0, iterator_count($files));

$result = $this->tagEngine->parse($element);
$this->assertSame($expected, $result);
}
}

0 comments on commit d49f31d

Please sign in to comment.