+16
-6
@@ -605,12 +605,18 @@ class Mustache_Engine
|
||||
* This method must be updated any time options are added which make it so
|
||||
* the same template could be parsed and compiled multiple different ways.
|
||||
*
|
||||
* @param string $source
|
||||
* @param string|Mustache_Source $source
|
||||
*
|
||||
* @return string Mustache Template class name
|
||||
*/
|
||||
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
|
||||
@@ -629,7 +635,7 @@ class Mustache_Engine
|
||||
'version' => self::VERSION,
|
||||
);
|
||||
|
||||
return $this->templateClassPrefix . md5(json_encode($options) . "\n" . $source);
|
||||
return $this->templateClassPrefix . md5(json_encode($options) . "\n" . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -706,8 +712,8 @@ class Mustache_Engine
|
||||
* @see Mustache_Engine::loadPartial
|
||||
* @see Mustache_Engine::loadLambda
|
||||
*
|
||||
* @param string $source
|
||||
* @param Mustache_Cache $cache (default: null)
|
||||
* @param string|Mustache_Source $source
|
||||
* @param Mustache_Cache $cache (default: null)
|
||||
*
|
||||
* @return Mustache_Template
|
||||
*/
|
||||
@@ -775,13 +781,12 @@ class Mustache_Engine
|
||||
*
|
||||
* @see Mustache_Compiler::compile
|
||||
*
|
||||
* @param string $source
|
||||
* @param string|Mustache_Source $source
|
||||
*
|
||||
* @return string generated Mustache template class code
|
||||
*/
|
||||
private function compile($source)
|
||||
{
|
||||
$tree = $this->parse($source);
|
||||
$name = $this->getTemplateClassName($source);
|
||||
|
||||
$this->log(
|
||||
@@ -790,6 +795,11 @@ class Mustache_Engine
|
||||
array('className' => $name)
|
||||
);
|
||||
|
||||
if ($source instanceof Mustache_Source) {
|
||||
$source = $source->getSource();
|
||||
}
|
||||
$tree = $this->parse($source);
|
||||
|
||||
$compiler = $this->getCompiler();
|
||||
$compiler->setPragmas($this->getPragmas());
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ interface Mustache_Loader
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return string Mustache Template source
|
||||
* @return string|Mustache_Source Mustache Template source
|
||||
*/
|
||||
public function load($name);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2010-2015 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mustache Template production filesystem Loader implementation.
|
||||
*
|
||||
* A production-ready FilesystemLoader, which doesn't require reading a file if it already exists in the template cache.
|
||||
*
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
class Mustache_Loader_ProductionFilesystemLoader extends Mustache_Loader_FilesystemLoader
|
||||
{
|
||||
/**
|
||||
* Helper function for loading a Mustache file by name.
|
||||
*
|
||||
* @throws Mustache_Exception_UnknownTemplateException If a template file is not found.
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Mustache_Source Mustache Template source
|
||||
*/
|
||||
protected function loadFile($name)
|
||||
{
|
||||
$fileName = $this->getFileName($name);
|
||||
|
||||
if (!file_exists($fileName)) {
|
||||
throw new Mustache_Exception_UnknownTemplateException($name);
|
||||
}
|
||||
|
||||
return new Mustache_Source_FilesystemSource($fileName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2010-2015 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mustache template Source interface.
|
||||
*/
|
||||
interface Mustache_Source
|
||||
{
|
||||
/**
|
||||
* Get the Source key (used to generate the compiled class name).
|
||||
*
|
||||
* @throws RuntimeException when a source file cannot be read
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getKey();
|
||||
|
||||
/**
|
||||
* Get the template Source.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSource();
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2010-2015 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mustache template Filesystem Source.
|
||||
*
|
||||
* This template Source uses stat() to generate the Source key, so that using
|
||||
* pre-compiled templates doesn't require hitting the disk to read the source.
|
||||
* It is more suitable for production use, and is used by default in the
|
||||
* ProductionFilesystemLoader.
|
||||
*/
|
||||
class Mustache_Source_FilesystemSource implements Mustache_Source
|
||||
{
|
||||
private $stat;
|
||||
private $filename;
|
||||
|
||||
/**
|
||||
* Filesystem Source constructor.
|
||||
*
|
||||
* @param string $filename
|
||||
*/
|
||||
public function __construct($filename)
|
||||
{
|
||||
$this->filename = $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Source key (used to generate the compiled class name).
|
||||
*
|
||||
* @throws RuntimeException when a source file cannot be read
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getKey()
|
||||
{
|
||||
if (!isset($this->stat)) {
|
||||
$this->stat = stat($this->filename);
|
||||
}
|
||||
|
||||
if ($this->stat === false) {
|
||||
throw new RuntimeException(sprintf('Failed to read source file "%s".', $this->filename));
|
||||
}
|
||||
|
||||
return sprintf('filename:%s,size:%s,mtime:%s', $this->filename, $this->stat['size'], $this->stat['mtime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template Source.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSource()
|
||||
{
|
||||
return file_get_contents($this->filename);
|
||||
}
|
||||
}
|
||||
@@ -331,6 +331,15 @@ class Mustache_Test_EngineTest extends Mustache_Test_FunctionalTestCase
|
||||
));
|
||||
}
|
||||
|
||||
public function testCompileFromMustacheSourceInstance()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../fixtures/templates');
|
||||
$mustache = new Mustache_Engine(array(
|
||||
'loader' => new Mustache_Loader_ProductionFilesystemLoader($baseDir),
|
||||
));
|
||||
$this->assertEquals('one contents', $mustache->render('one'));
|
||||
}
|
||||
|
||||
private function getLoggedMustache($level = Mustache_Logger::ERROR)
|
||||
{
|
||||
$name = tempnam(sys_get_temp_dir(), 'mustache-test');
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2010-2015 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group unit
|
||||
*/
|
||||
class Mustache_Test_Loader_ProductionFilesystemLoaderTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir, array('extension' => '.ms'));
|
||||
$this->assertInstanceOf('Mustache_Source', $loader->load('alpha'));
|
||||
$this->assertEquals('alpha contents', $loader->load('alpha')->getSource());
|
||||
$this->assertInstanceOf('Mustache_Source', $loader->load('beta.ms'));
|
||||
$this->assertEquals('beta contents', $loader->load('beta.ms')->getSource());
|
||||
}
|
||||
|
||||
public function testTrailingSlashes()
|
||||
{
|
||||
$baseDir = dirname(__FILE__) . '/../../../fixtures/templates/';
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir);
|
||||
$this->assertEquals('one contents', $loader->load('one')->getSource());
|
||||
}
|
||||
|
||||
public function testConstructorWithProtocol()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
|
||||
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader('file://' . $baseDir, array('extension' => '.ms'));
|
||||
$this->assertEquals('alpha contents', $loader->load('alpha')->getSource());
|
||||
$this->assertEquals('beta contents', $loader->load('beta.ms')->getSource());
|
||||
}
|
||||
|
||||
public function testLoadTemplates()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir);
|
||||
$this->assertEquals('one contents', $loader->load('one')->getSource());
|
||||
$this->assertEquals('two contents', $loader->load('two.mustache')->getSource());
|
||||
}
|
||||
|
||||
public function testEmptyExtensionString()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
|
||||
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir, array('extension' => ''));
|
||||
$this->assertEquals('one contents', $loader->load('one.mustache')->getSource());
|
||||
$this->assertEquals('alpha contents', $loader->load('alpha.ms')->getSource());
|
||||
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir, array('extension' => null));
|
||||
$this->assertEquals('two contents', $loader->load('two.mustache')->getSource());
|
||||
$this->assertEquals('beta contents', $loader->load('beta.ms')->getSource());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Mustache_Exception_RuntimeException
|
||||
*/
|
||||
public function testMissingBaseDirThrowsException()
|
||||
{
|
||||
new Mustache_Loader_ProductionFilesystemLoader(dirname(__FILE__) . '/not_a_directory');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Mustache_Exception_UnknownTemplateException
|
||||
*/
|
||||
public function testMissingTemplateThrowsException()
|
||||
{
|
||||
$baseDir = realpath(dirname(__FILE__) . '/../../../fixtures/templates');
|
||||
$loader = new Mustache_Loader_ProductionFilesystemLoader($baseDir);
|
||||
|
||||
$loader->load('fake');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user