| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Mustache.php. |
| 5 |
* |
| 6 |
* (c) 2010-2026 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 |
namespace Mustache\Source; |
| 13 |
|
| 14 |
use Mustache\Exception\RuntimeException; |
| 15 |
use Mustache\Source; |
| 16 |
|
| 17 |
/** |
| 18 |
* Mustache template Filesystem Source. |
| 19 |
* |
| 20 |
* This template Source uses stat() to generate the Source key, so that using |
| 21 |
* pre-compiled templates doesn't require hitting the disk to read the source. |
| 22 |
* It is more suitable for production use, and is used by default in the |
| 23 |
* ProductionFilesystemLoader. |
| 24 |
*/ |
| 25 |
class FilesystemSource implements Source |
| 26 |
{ |
| 27 |
private $fileName; |
| 28 |
private $statProps; |
| 29 |
private $stat; |
| 30 |
|
| 31 |
/** |
| 32 |
* Filesystem Source constructor. |
| 33 |
* |
| 34 |
* @param string $fileName |
| 35 |
*/ |
| 36 |
public function __construct($fileName, array $statProps) |
| 37 |
{ |
| 38 |
$this->fileName = $fileName; |
| 39 |
$this->statProps = $statProps; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get the Source key (used to generate the compiled class name). |
| 44 |
* |
| 45 |
* @throws RuntimeException when a source file cannot be read |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getKey() |
| 50 |
{ |
| 51 |
$chunks = [ |
| 52 |
'fileName' => $this->fileName, |
| 53 |
]; |
| 54 |
|
| 55 |
if (!empty($this->statProps)) { |
| 56 |
if (!isset($this->stat)) { |
| 57 |
$this->stat = @stat($this->fileName); |
| 58 |
} |
| 59 |
|
| 60 |
if ($this->stat === false) { |
| 61 |
throw new RuntimeException(sprintf('Failed to read source file "%s".', $this->fileName)); |
| 62 |
} |
| 63 |
|
| 64 |
foreach ($this->statProps as $prop) { |
| 65 |
$chunks[$prop] = $this->stat[$prop]; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return json_encode($chunks); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get the template Source. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function getSource() |
| 78 |
{ |
| 79 |
return file_get_contents($this->fileName); |
| 80 |
} |
| 81 |
} |
| 82 |
|