Restore logging for cache operations
`AbstractCache` exposes the ability to set a logger. Default behavior automatically passes the logger reference down to the cache, unless a specific cache instance was provided.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
abstract class Mustache_Cache_AbstractCache implements Mustache_Cache
|
||||
{
|
||||
private $logger = null;
|
||||
|
||||
public function getLogger()
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function setLogger($logger = null)
|
||||
{
|
||||
if ($logger !== null && !($logger instanceof Mustache_Logger || is_a($logger, 'Psr\\Log\\LoggerInterface'))) {
|
||||
throw new Mustache_Exception_InvalidArgumentException('Expected an instance of Mustache_Logger or Psr\\Log\\LoggerInterface.');
|
||||
}
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
protected function log($level, $message, array $context = array())
|
||||
{
|
||||
if (isset($this->logger)) {
|
||||
$this->logger->log($level, $message, $context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Mustache_Cache_FilesystemCache implements Mustache_Cache
|
||||
class Mustache_Cache_FilesystemCache extends Mustache_Cache_AbstractCache
|
||||
{
|
||||
private $directory;
|
||||
private $fileMode;
|
||||
@@ -26,6 +26,13 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
|
||||
public function cache($key, $value)
|
||||
{
|
||||
$fileName = $this->getCacheFilename($key);
|
||||
|
||||
$this->log(
|
||||
Mustache_Logger::DEBUG,
|
||||
'Writing to template cache: "{fileName}"',
|
||||
array('fileName' => $fileName)
|
||||
);
|
||||
|
||||
$this->writeFile($fileName, $value);
|
||||
$this->load($key);
|
||||
}
|
||||
@@ -39,6 +46,12 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
|
||||
{
|
||||
$dirName = dirname($fileName);
|
||||
if (!is_dir($dirName)) {
|
||||
$this->log(
|
||||
Mustache_Logger::INFO,
|
||||
'Creating Mustache template cache directory: "{dirName}"',
|
||||
array('dirName' => $dirName)
|
||||
);
|
||||
|
||||
@mkdir($dirName, 0777, true);
|
||||
if (!is_dir($dirName)) {
|
||||
throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
|
||||
@@ -50,6 +63,13 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
|
||||
private function writeFile($fileName, $value)
|
||||
{
|
||||
$dirName = $this->buildDirectoryForFilename($fileName);
|
||||
|
||||
$this->log(
|
||||
Mustache_Logger::DEBUG,
|
||||
'Caching compiled template to "{fileName}"',
|
||||
array('fileName' => $fileName)
|
||||
);
|
||||
|
||||
$tempFile = tempnam($dirName, basename($fileName));
|
||||
if (false !== @file_put_contents($tempFile, $value)) {
|
||||
if (@rename($tempFile, $fileName)) {
|
||||
@@ -58,6 +78,12 @@ class Mustache_Cache_FilesystemCache implements Mustache_Cache
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
$this->log(
|
||||
Mustache_Logger::ERROR,
|
||||
'Unable to rename Mustache temp cache file: "{tempName}" -> "{fileName}"',
|
||||
array('tempName' => $tempFile, 'fileName' => $fileName)
|
||||
);
|
||||
}
|
||||
|
||||
throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Mustache_Cache_NoopCache implements Mustache_Cache
|
||||
class Mustache_Cache_NoopCache extends Mustache_Cache_AbstractCache
|
||||
{
|
||||
public function load($key)
|
||||
{
|
||||
@@ -9,6 +9,11 @@ class Mustache_Cache_NoopCache implements Mustache_Cache
|
||||
|
||||
public function cache($key, $compiled)
|
||||
{
|
||||
$this->log(
|
||||
Mustache_Logger::WARNING,
|
||||
'Template cache disabled, evaluating class at runtime',
|
||||
array()
|
||||
);
|
||||
eval("?>".$compiled);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +164,10 @@ class Mustache_Engine
|
||||
if (isset($options['strict_callables'])) {
|
||||
$this->strictCallables = $options['strict_callables'];
|
||||
}
|
||||
|
||||
if (!isset($options['cache']) || is_string($options['cache'])) {
|
||||
$this->getCache()->setLogger($this->getLogger());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -506,12 +510,6 @@ class Mustache_Engine
|
||||
{
|
||||
if (!isset($this->cache)) {
|
||||
$this->cache = new Mustache_Cache_NoopCache();
|
||||
|
||||
$this->log(
|
||||
Mustache_Logger::WARNING,
|
||||
'Template cache disabled',
|
||||
array()
|
||||
);
|
||||
}
|
||||
|
||||
return $this->cache;
|
||||
|
||||
@@ -294,7 +294,7 @@ class Mustache_Test_EngineTest extends PHPUnit_Framework_TestCase
|
||||
$result = $mustache->render('{{ foo }}', array('foo' => 'FOO'));
|
||||
$this->assertEquals('FOO', $result);
|
||||
|
||||
$this->assertContains('WARNING: Template cache disabled', file_get_contents($name));
|
||||
$this->assertContains('WARNING: Template cache disabled, evaluating', file_get_contents($name));
|
||||
}
|
||||
|
||||
public function testLoggingIsNotTooAnnoying()
|
||||
|
||||
Reference in New Issue
Block a user