[Templating] removed CompilableLoaderInterface and fixed unit tests

This commit is contained in:
Fabien Potencier 2010-05-20 20:57:25 +02:00
parent 6ea878242b
commit 46a8a1752f
5 changed files with 9 additions and 77 deletions

View File

@ -61,10 +61,6 @@ class CacheLoader extends Loader
$file = substr($tmp, 2);
$path = $dir.DIRECTORY_SEPARATOR.$file;
if ($this->loader instanceof CompilableLoaderInterface) {
$options['renderer'] = 'php';
}
if (file_exists($path)) {
if (null !== $this->debugger) {
$this->debugger->log(sprintf('Fetching template "%s" from cache', $template));
@ -79,10 +75,6 @@ class CacheLoader extends Loader
$content = $storage->getContent();
if ($this->loader instanceof CompilableLoaderInterface) {
$content = $this->loader->compile($content);
}
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}

View File

@ -1,34 +0,0 @@
<?php
namespace Symfony\Components\Templating\Loader;
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* CompilableLoaderInterface is the interface a template loader must implement
* if the templates are compilable.
*
* @package Symfony
* @subpackage Components_Templating
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
interface CompilableLoaderInterface
{
/**
* Compiles a template.
*
* @param string $template The template to compile
*
* @return string The compiled template
*
* @throws \Exception if the template is not compilable
*/
public function compile($template);
}

View File

@ -15,7 +15,6 @@ require_once __DIR__.'/../../../../lib/SymfonyTests/Components/Templating/Simple
use Symfony\Components\Templating\Engine;
use Symfony\Components\Templating\Loader\Loader;
use Symfony\Components\Templating\Loader\CompilableLoaderInterface;
use Symfony\Components\Templating\Renderer\Renderer;
use Symfony\Components\Templating\Renderer\PhpRenderer;
use Symfony\Components\Templating\Storage\Storage;
@ -115,10 +114,6 @@ class EngineTest extends \PHPUnit_Framework_TestCase
self::$loader->setTemplate('foo.php', '<?php $view->extend("layout"); echo $foo ?>');
self::$loader->setTemplate('layout.php', '<?php echo $view->render("bar") ?>-<?php echo $view->slots->get("_content") ?>-');
$this->assertEquals('bar-foo-', $engine->render('foo', array('foo' => 'foo', 'bar' => 'bar')), '->render() supports render() calls in templates');
// compilable templates
$engine = new ProjectTemplateEngine(new CompilableTemplateLoader(), array('foo' => new FooTemplateRenderer()));
$this->assertEquals('foo', $engine->render('index'), '->load() takes into account the renderer embedded in the Storage instance if not null');
}
public function testEscape()
@ -176,18 +171,10 @@ class ProjectTemplateLoader extends Loader
return false;
}
}
class CompilableTemplateLoader extends Loader implements CompilableLoaderInterface
{
public function load($template, array $options = array())
public function isFresh($template, array $options = array(), $time)
{
return new StringStorage($template, 'foo');
}
public function compile($template)
{
return 'COMPILED';
return false;
}
}

View File

@ -15,7 +15,6 @@ require_once __DIR__.'/../../../../../lib/SymfonyTests/Components/Templating/Pro
use Symfony\Components\Templating\Loader\Loader;
use Symfony\Components\Templating\Loader\CacheLoader;
use Symfony\Components\Templating\Loader\CompilableLoaderInterface;
use Symfony\Components\Templating\Storage\StringStorage;
class CacheLoaderTest extends \PHPUnit_Framework_TestCase
@ -39,20 +38,6 @@ class CacheLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
$loader->load('index');
$this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
// load() template compilation
$dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.rand(111111, 999999);
mkdir($dir, 0777, true);
$loader = new ProjectTemplateLoader(new CompilableTemplateLoader(), $dir);
$loader->setDebugger($debugger = new \ProjectTemplateDebugger());
$template = $loader->load('special', array('renderer' => 'comp'));
$this->assertTrue($debugger->hasMessage('Storing template'), '->load() logs a "Storing template" message if the template is found');
$this->assertEquals('php', $template->getRenderer(), '->load() changes the renderer to php if the template is compilable');
$template = $loader->load('special', array('renderer' => 'comp'));
$this->assertTrue($debugger->hasMessage('Fetching template'), '->load() logs a "Storing template" message if the template is fetched from cache');
$this->assertEquals('php', $template->getRenderer(), '->load() changes the renderer to php if the template is compilable');
}
}
@ -89,12 +74,9 @@ class ProjectTemplateLoaderVar extends Loader
return false;
}
}
class CompilableTemplateLoader extends ProjectTemplateLoaderVar implements CompilableLoaderInterface
{
public function compile($template)
public function isFresh($template, array $options = array(), $time)
{
return preg_replace('/{{\s*([a-zA-Z0-9_]+)\s*}}/', '<?php echo $$1 ?>', $template);
return false;
}
}

View File

@ -35,4 +35,9 @@ class ProjectTemplateLoader4 extends Loader
{
return $this->debugger;
}
public function isFresh($template, array $options = array(), $time)
{
return false;
}
}