From e5be4e97ffc41ca3b0386651a626b781a3637a85 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 6 Oct 2023 18:26:22 +0200 Subject: Update for PHP 8.2 --- Mustache/Cache/AbstractCache.php | 60 ++++++++++++++ Mustache/Cache/FilesystemCache.php | 161 +++++++++++++++++++++++++++++++++++++ Mustache/Cache/NoopCache.php | 47 +++++++++++ 3 files changed, 268 insertions(+) create mode 100644 Mustache/Cache/AbstractCache.php create mode 100644 Mustache/Cache/FilesystemCache.php create mode 100644 Mustache/Cache/NoopCache.php (limited to 'Mustache/Cache') diff --git a/Mustache/Cache/AbstractCache.php b/Mustache/Cache/AbstractCache.php new file mode 100644 index 0000000..281038f --- /dev/null +++ b/Mustache/Cache/AbstractCache.php @@ -0,0 +1,60 @@ +logger; + } + + /** + * Set a logger instance. + * + * @param Mustache_Logger|Psr\Log\LoggerInterface $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; + } + + /** + * Add a log record if logging is enabled. + * + * @param string $level The logging level + * @param string $message The log message + * @param array $context The log context + */ + protected function log($level, $message, array $context = array()) + { + if (isset($this->logger)) { + $this->logger->log($level, $message, $context); + } + } +} diff --git a/Mustache/Cache/FilesystemCache.php b/Mustache/Cache/FilesystemCache.php new file mode 100644 index 0000000..3e742b7 --- /dev/null +++ b/Mustache/Cache/FilesystemCache.php @@ -0,0 +1,161 @@ +cache($className, $compiledSource); + * + * The FilesystemCache benefits from any opcode caching that may be setup in your environment. So do that, k? + */ +class Mustache_Cache_FilesystemCache extends Mustache_Cache_AbstractCache +{ + private $baseDir; + private $fileMode; + + /** + * Filesystem cache constructor. + * + * @param string $baseDir Directory for compiled templates + * @param int $fileMode Override default permissions for cache files. Defaults to using the system-defined umask + */ + public function __construct($baseDir, $fileMode = null) + { + $this->baseDir = $baseDir; + $this->fileMode = $fileMode; + } + + /** + * Load the class from cache using `require_once`. + * + * @param string $key + * + * @return bool + */ + public function load($key) + { + $fileName = $this->getCacheFilename($key); + if (!is_file($fileName)) { + return false; + } + + require_once $fileName; + + return true; + } + + /** + * Cache and load the compiled class. + * + * @param string $key + * @param string $value + */ + 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); + } + + /** + * Build the cache filename. + * Subclasses should override for custom cache directory structures. + * + * @param string $name + * + * @return string + */ + protected function getCacheFilename($name) + { + return sprintf('%s/%s.php', $this->baseDir, $name); + } + + /** + * Create cache directory. + * + * @throws Mustache_Exception_RuntimeException If unable to create directory + * + * @param string $fileName + * + * @return string + */ + private function buildDirectoryForFilename($fileName) + { + $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); + // @codeCoverageIgnoreStart + if (!is_dir($dirName)) { + throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName)); + } + // @codeCoverageIgnoreEnd + } + + return $dirName; + } + + /** + * Write cache file. + * + * @throws Mustache_Exception_RuntimeException If unable to write file + * + * @param string $fileName + * @param string $value + */ + 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)) { + $mode = isset($this->fileMode) ? $this->fileMode : (0666 & ~umask()); + @chmod($fileName, $mode); + + return; + } + + // @codeCoverageIgnoreStart + $this->log( + Mustache_Logger::ERROR, + 'Unable to rename Mustache temp cache file: "{tempName}" -> "{fileName}"', + array('tempName' => $tempFile, 'fileName' => $fileName) + ); + // @codeCoverageIgnoreEnd + } + + // @codeCoverageIgnoreStart + throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName)); + // @codeCoverageIgnoreEnd + } +} diff --git a/Mustache/Cache/NoopCache.php b/Mustache/Cache/NoopCache.php new file mode 100644 index 0000000..ed9eec9 --- /dev/null +++ b/Mustache/Cache/NoopCache.php @@ -0,0 +1,47 @@ +log( + Mustache_Logger::WARNING, + 'Template cache disabled, evaluating "{className}" class at runtime', + array('className' => $key) + ); + eval('?>' . $value); + } +} -- cgit v1.2.3-55-g7522