Clean up Source implementation a bit.

Make it work with new template class name logic.
This commit is contained in:
Justin Hileman
2017-07-02 21:50:12 -07:00
parent 1514e0c2cd
commit 556b8e67b8
3 changed files with 29 additions and 18 deletions
+11 -8
View File
@@ -611,12 +611,6 @@ class Mustache_Engine
*/
public function getTemplateClassName($source)
{
if ($source instanceof Mustache_Source) {
$key = $source->getKey();
} else {
$key = sprintf('source:%s', $source);
}
// For the most part, adding a new option here should do the trick.
//
// Pick a value here which is unique for each possible way the template
@@ -625,17 +619,26 @@ class Mustache_Engine
// 'default' escapes.
//
// Keep this list in alphabetical order :)
$options = array(
$chunks = array(
'charset' => $this->charset,
'delimiters' => $this->delimiters,
'entityFlags' => $this->entityFlags,
'escape' => isset($this->escape) ? 'custom' : 'default',
'key' => ($source instanceof Mustache_Source) ? $source->getKey() : 'source',
'pragmas' => $this->getPragmas(),
'strictCallables' => $this->strictCallables,
'version' => self::VERSION,
);
return $this->templateClassPrefix . md5(json_encode($options) . "\n" . $key);
$key = json_encode($chunks);
// Template Source instances have already provided their own source key. For strings, just include the whole
// source string in the md5 hash.
if (!$source instanceof Mustache_Source) {
$key .= "\n" . $source;
}
return $this->templateClassPrefix . md5($key);
}
/**
+8
View File
@@ -17,6 +17,12 @@ interface Mustache_Source
/**
* Get the Source key (used to generate the compiled class name).
*
* This must return a distinct key for each template source. For example, an
* MD5 hash of the template contents would probably do the trick. The
* ProductionFilesystemLoader uses mtime and file path. If your production
* source directory is under version control, you could use the current Git
* rev and the file path...
*
* @throws RuntimeException when a source file cannot be read
*
* @return string
@@ -26,6 +32,8 @@ interface Mustache_Source
/**
* Get the template Source.
*
* @throws RuntimeException when a source file cannot be read
*
* @return string
*/
public function getSource();
+10 -10
View File
@@ -19,19 +19,19 @@
*/
class Mustache_Source_FilesystemSource implements Mustache_Source
{
private $filename;
private $fileName;
private $statProps;
private $stat;
/**
* Filesystem Source constructor.
*
* @param string $filename
* @param string $fileName
* @param array $statProps
*/
public function __construct($filename, array $statProps)
public function __construct($fileName, array $statProps)
{
$this->filename = $filename;
$this->fileName = $fileName;
$this->statProps = $statProps;
}
@@ -45,24 +45,24 @@ class Mustache_Source_FilesystemSource implements Mustache_Source
public function getKey()
{
$chunks = array(
sprintf('filename:%s', $this->filename),
'fileName' => $this->fileName,
);
if (!empty($this->statProps)) {
if (!isset($this->stat)) {
$this->stat = stat($this->filename);
$this->stat = stat($this->fileName);
}
if ($this->stat === false) {
throw new RuntimeException(sprintf('Failed to read source file "%s".', $this->filename));
throw new RuntimeException(sprintf('Failed to read source file "%s".', $this->fileName));
}
foreach ($this->statProps as $prop) {
$chunks[] = sprintf('%s:%s', $prop, $this->stat[$prop]);
$chunks[$prop] = $this->stat[$prop];
}
}
return implode(',', $chunks);
return json_encode($chunks);
}
/**
@@ -72,6 +72,6 @@ class Mustache_Source_FilesystemSource implements Mustache_Source
*/
public function getSource()
{
return file_get_contents($this->filename);
return file_get_contents($this->fileName);
}
}