diff --git a/src/Mustache/Engine.php b/src/Mustache/Engine.php index 38c05c9..41cb920 100644 --- a/src/Mustache/Engine.php +++ b/src/Mustache/Engine.php @@ -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); } /** diff --git a/src/Mustache/Source.php b/src/Mustache/Source.php index 3eded91..e6dfdf4 100644 --- a/src/Mustache/Source.php +++ b/src/Mustache/Source.php @@ -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(); diff --git a/src/Mustache/Source/FilesystemSource.php b/src/Mustache/Source/FilesystemSource.php index 7928ab2..1201af8 100644 --- a/src/Mustache/Source/FilesystemSource.php +++ b/src/Mustache/Source/FilesystemSource.php @@ -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); } }