Skip to content

Commit

Permalink
fix(admin): Fix Clear Cache template when view engine unavailable
Browse files Browse the repository at this point in the history
Fixed logic of the class that would cause a fatal error if Mustache or Twig were not installed.

Changed class to defer retrieval of engines from service container until needed and verify their engines' dependencies are available.
  • Loading branch information
mcaskill committed Jan 13, 2024
1 parent 12a4e34 commit 8f794e2
Showing 1 changed file with 80 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
// From 'charcoal-admin'
use Charcoal\Admin\AdminTemplate;
use Charcoal\View\EngineInterface;
use Charcoal\View\Mustache\MustacheEngine;
use Charcoal\View\Twig\TwigEngine;

/**
* Cache information.
*
* For the Mustache and Twig engines, this class defers their retrieval from
* the service container to avoid fetching an unused or unavailable engine.
*/
class ClearCacheTemplate extends AdminTemplate
{
Expand Down Expand Up @@ -62,16 +67,16 @@ class ClearCacheTemplate extends AdminTemplate
/**
* Mustache View Engine.
*
* @var \Charcoal\View\Mustache\MustacheEngine
* @var MustacheEngine|(callable():?MustacheEngine)
*/
private EngineInterface $mustacheEngine;
private $mustacheEngine;

/**
* Twig View Engine.
*
* @var \Charcoal\View\Twig\TwigEngine
* @var TwigEngine|(callable():?TwigEngine)
*/
private EngineInterface $twigEngine;
private $twigEngine;

/**
* Retrieve the title of the page.
Expand Down Expand Up @@ -193,6 +198,32 @@ private function getPagesCacheKey()
return '/::' . $this->getCacheNamespace() . '::request::|::' . $this->getCacheNamespace() . '::template::/';
}

public function getMustacheEngine(): ?MustacheEngine
{
if (is_callable($this->mustacheEngine)) {
$this->mustacheEngine = ($this->mustacheEngine)();
}

if ($this->mustacheEngine instanceof MustacheEngine) {
return $this->mustacheEngine;
}

return null;
}

public function getTwigEngine(): ?TwigEngine
{
if (is_callable($this->twigEngine)) {
$this->twigEngine = ($this->twigEngine)();
}

if ($this->twigEngine instanceof TwigEngine) {
return $this->twigEngine;
}

return null;
}

/**
* @return array
*/
Expand Down Expand Up @@ -257,10 +288,19 @@ private function objectsCacheInfo()
*/
private function twigCacheInfo(): array
{
$defaultCachePath = realpath($this->twigEngine->cache());
$engine = $this->getTwigEngine();
if (!$engine) {
return [
'num_entries' => 0,
'total_size' => 0,
'no_cache_folder' => true,
];
}

$defaultCachePath = realpath($engine->cache());
$cachePath = $defaultCachePath
? $defaultCachePath
: realpath($this->appConfig['publicPath'] . DIRECTORY_SEPARATOR . $this->twigEngine->cache());
: realpath($this->appConfig['publicPath'] . DIRECTORY_SEPARATOR . $engine->cache());

if (!is_dir($cachePath)) {
return [
Expand All @@ -282,8 +322,19 @@ private function twigCacheInfo(): array
*/
private function mustacheCacheInfo(): array
{
$defaultCachePath = realpath($this->mustacheEngine->cache());
$cachePath = $defaultCachePath ? $defaultCachePath : realpath($this->appConfig['publicPath'] . DIRECTORY_SEPARATOR . $this->mustacheEngine->cache());
$engine = $this->getMustacheEngine();
if (!$engine) {
return [
'num_entries' => 0,
'total_size' => 0,
'no_cache_folder' => true,
];
}

$defaultCachePath = realpath($engine->cache());
$cachePath = $defaultCachePath
? $defaultCachePath
: realpath($this->appConfig['publicPath'] . DIRECTORY_SEPARATOR . $engine->cache());
if (!is_dir($cachePath)) {
return [
'no_cache_folder' => true,
Expand Down Expand Up @@ -435,7 +486,12 @@ public function isMemory()

public function hasTwigCache(): bool
{
return $this->twigEngine->config()->useCache;
$engine = $this->getTwigEngine();
if ($engine) {
return (bool) $engine->config()['useCache'];
}

return false;
}

public function hasMustacheCache(): bool
Expand Down Expand Up @@ -586,7 +642,20 @@ protected function setDependencies(Container $container)
$this->availableCacheDrivers = $container['cache/available-drivers'];
$this->cache = $container['cache'];
$this->cacheConfig = $container['cache/config'];
$this->mustacheEngine = $container['view/engine/mustache'];
$this->twigEngine = $container['view/engine/twig'];

$this->mustacheEngine = function () use ($container) {
if (class_exists('\Mustache_Engine')) {
return $container['view/engine/mustache'];
}

return null;
};
$this->twigEngine = function () use ($container) {
if (class_exists('\Twig\Environment')) {
return $container['view/engine/twig'];
}

return null;
};
}
}

0 comments on commit 8f794e2

Please sign in to comment.