/lib/mustache/src/Mustache/Cache/FilesystemCache.php

https://github.com/jfilip/moodle · PHP · 159 lines · 74 code · 19 blank · 66 comment · 5 complexity · 129270d62dbcba2bf9beb3118e37dfa5 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Mustache.php.
  4. *
  5. * (c) 2010-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Mustache Cache filesystem implementation.
  12. *
  13. * A FilesystemCache instance caches Mustache Template classes from the filesystem by name:
  14. *
  15. * $cache = new Mustache_Cache_FilesystemCache(dirname(__FILE__).'/cache');
  16. * $cache->cache($className, $compiledSource);
  17. *
  18. * The FilesystemCache benefits from any opcode caching that may be setup in your environment. So do that, k?
  19. */
  20. class Mustache_Cache_FilesystemCache extends Mustache_Cache_AbstractCache
  21. {
  22. private $baseDir;
  23. private $fileMode;
  24. /**
  25. * Filesystem cache constructor.
  26. *
  27. * @param string $baseDir Directory for compiled templates.
  28. * @param int $fileMode Override default permissions for cache files. Defaults to using the system-defined umask.
  29. */
  30. public function __construct($baseDir, $fileMode = null)
  31. {
  32. $this->baseDir = $baseDir;
  33. $this->fileMode = $fileMode;
  34. }
  35. /**
  36. * Load the class from cache using `require_once`.
  37. *
  38. * @param string $key
  39. *
  40. * @return boolean
  41. */
  42. public function load($key)
  43. {
  44. $fileName = $this->getCacheFilename($key);
  45. if (!is_file($fileName)) {
  46. return false;
  47. }
  48. require_once $fileName;
  49. return true;
  50. }
  51. /**
  52. * Cache and load the compiled class
  53. *
  54. * @param string $key
  55. * @param string $value
  56. *
  57. * @return void
  58. */
  59. public function cache($key, $value)
  60. {
  61. $fileName = $this->getCacheFilename($key);
  62. $this->log(
  63. Mustache_Logger::DEBUG,
  64. 'Writing to template cache: "{fileName}"',
  65. array('fileName' => $fileName)
  66. );
  67. $this->writeFile($fileName, $value);
  68. $this->load($key);
  69. }
  70. /**
  71. * Build the cache filename.
  72. * Subclasses should override for custom cache directory structures.
  73. *
  74. * @param string $name
  75. *
  76. * @return string
  77. */
  78. protected function getCacheFilename($name)
  79. {
  80. return sprintf('%s/%s.php', $this->baseDir, $name);
  81. }
  82. /**
  83. * Create cache directory
  84. *
  85. * @throws Mustache_Exception_RuntimeException If unable to create directory
  86. *
  87. * @param string $fileName
  88. *
  89. * @return string
  90. */
  91. private function buildDirectoryForFilename($fileName)
  92. {
  93. $dirName = dirname($fileName);
  94. if (!is_dir($dirName)) {
  95. $this->log(
  96. Mustache_Logger::INFO,
  97. 'Creating Mustache template cache directory: "{dirName}"',
  98. array('dirName' => $dirName)
  99. );
  100. @mkdir($dirName, 0777, true);
  101. if (!is_dir($dirName)) {
  102. throw new Mustache_Exception_RuntimeException(sprintf('Failed to create cache directory "%s".', $dirName));
  103. }
  104. }
  105. return $dirName;
  106. }
  107. /**
  108. * Write cache file
  109. *
  110. * @throws Mustache_Exception_RuntimeException If unable to write file
  111. *
  112. * @param string $fileName
  113. * @param string $value
  114. *
  115. * @return void
  116. */
  117. private function writeFile($fileName, $value)
  118. {
  119. $dirName = $this->buildDirectoryForFilename($fileName);
  120. $this->log(
  121. Mustache_Logger::DEBUG,
  122. 'Caching compiled template to "{fileName}"',
  123. array('fileName' => $fileName)
  124. );
  125. $tempFile = tempnam($dirName, basename($fileName));
  126. if (false !== @file_put_contents($tempFile, $value)) {
  127. if (@rename($tempFile, $fileName)) {
  128. $mode = isset($this->fileMode) ? $this->fileMode : (0666 & ~umask());
  129. @chmod($fileName, $mode);
  130. return;
  131. }
  132. $this->log(
  133. Mustache_Logger::ERROR,
  134. 'Unable to rename Mustache temp cache file: "{tempName}" -> "{fileName}"',
  135. array('tempName' => $tempFile, 'fileName' => $fileName)
  136. );
  137. }
  138. throw new Mustache_Exception_RuntimeException(sprintf('Failed to write cache file "%s".', $fileName));
  139. }
  140. }