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