Clean up Source implementation a bit.
Make it work with new template class name logic.
This commit is contained in:
+11
-8
@@ -611,12 +611,6 @@ class Mustache_Engine
|
|||||||
*/
|
*/
|
||||||
public function getTemplateClassName($source)
|
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.
|
// 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
|
// Pick a value here which is unique for each possible way the template
|
||||||
@@ -625,17 +619,26 @@ class Mustache_Engine
|
|||||||
// 'default' escapes.
|
// 'default' escapes.
|
||||||
//
|
//
|
||||||
// Keep this list in alphabetical order :)
|
// Keep this list in alphabetical order :)
|
||||||
$options = array(
|
$chunks = array(
|
||||||
'charset' => $this->charset,
|
'charset' => $this->charset,
|
||||||
'delimiters' => $this->delimiters,
|
'delimiters' => $this->delimiters,
|
||||||
'entityFlags' => $this->entityFlags,
|
'entityFlags' => $this->entityFlags,
|
||||||
'escape' => isset($this->escape) ? 'custom' : 'default',
|
'escape' => isset($this->escape) ? 'custom' : 'default',
|
||||||
|
'key' => ($source instanceof Mustache_Source) ? $source->getKey() : 'source',
|
||||||
'pragmas' => $this->getPragmas(),
|
'pragmas' => $this->getPragmas(),
|
||||||
'strictCallables' => $this->strictCallables,
|
'strictCallables' => $this->strictCallables,
|
||||||
'version' => self::VERSION,
|
'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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ interface Mustache_Source
|
|||||||
/**
|
/**
|
||||||
* Get the Source key (used to generate the compiled class name).
|
* 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
|
* @throws RuntimeException when a source file cannot be read
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
@@ -26,6 +32,8 @@ interface Mustache_Source
|
|||||||
/**
|
/**
|
||||||
* Get the template Source.
|
* Get the template Source.
|
||||||
*
|
*
|
||||||
|
* @throws RuntimeException when a source file cannot be read
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSource();
|
public function getSource();
|
||||||
|
|||||||
@@ -19,19 +19,19 @@
|
|||||||
*/
|
*/
|
||||||
class Mustache_Source_FilesystemSource implements Mustache_Source
|
class Mustache_Source_FilesystemSource implements Mustache_Source
|
||||||
{
|
{
|
||||||
private $filename;
|
private $fileName;
|
||||||
private $statProps;
|
private $statProps;
|
||||||
private $stat;
|
private $stat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filesystem Source constructor.
|
* Filesystem Source constructor.
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $fileName
|
||||||
* @param array $statProps
|
* @param array $statProps
|
||||||
*/
|
*/
|
||||||
public function __construct($filename, array $statProps)
|
public function __construct($fileName, array $statProps)
|
||||||
{
|
{
|
||||||
$this->filename = $filename;
|
$this->fileName = $fileName;
|
||||||
$this->statProps = $statProps;
|
$this->statProps = $statProps;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,24 +45,24 @@ class Mustache_Source_FilesystemSource implements Mustache_Source
|
|||||||
public function getKey()
|
public function getKey()
|
||||||
{
|
{
|
||||||
$chunks = array(
|
$chunks = array(
|
||||||
sprintf('filename:%s', $this->filename),
|
'fileName' => $this->fileName,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!empty($this->statProps)) {
|
if (!empty($this->statProps)) {
|
||||||
if (!isset($this->stat)) {
|
if (!isset($this->stat)) {
|
||||||
$this->stat = stat($this->filename);
|
$this->stat = stat($this->fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->stat === false) {
|
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) {
|
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()
|
public function getSource()
|
||||||
{
|
{
|
||||||
return file_get_contents($this->filename);
|
return file_get_contents($this->fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user