From: Fabien Potencier Date: Wed, 24 Aug 2011 10:56:57 +0000 (+0200) Subject: added Twig_Loader_Chain X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=517c233679e2d0b19860609c0654847cb86c9602;p=web%2Fkonrad%2Ftwig.git added Twig_Loader_Chain --- diff --git a/CHANGELOG b/CHANGELOG index 72b120f..f8f26f8 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.2.0 + * added Twig_Loader_Chain * added Twig_Loader_Array::setTemplate() * added an optimization for the set tag when used to capture a large chunk of static text * changed name regex to match PHP one "[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*" (works for blocks, tags, functions, filters, and macros) diff --git a/lib/Twig/Loader/Chain.php b/lib/Twig/Loader/Chain.php new file mode 100644 index 0000000..48dd8b8 --- /dev/null +++ b/lib/Twig/Loader/Chain.php @@ -0,0 +1,100 @@ + + */ +class Twig_Loader_Chain implements Twig_LoaderInterface +{ + protected $loaders; + + /** + * Constructor. + * + * @param Twig_LoaderInterface[] $loaders An array of loader instances + */ + public function __construct(array $loaders = array()) + { + $this->loaders = array(); + foreach ($loaders as $loader) { + $this->addLoader($loader); + } + } + + /** + * Adds a loader instance. + * + * @param Twig_LoaderInterface $loader A Loader instance + */ + public function addLoader(Twig_LoaderInterface $loader) + { + $this->loaders[] = $loader; + } + + /** + * Gets the source code of a template, given its name. + * + * @param string $name The name of the template to load + * + * @return string The template source code + */ + public function getSource($name) + { + foreach ($this->loaders as $loader) { + try { + return $loader->getSource($name); + } catch (Twig_Error_Loader $e) { + } + } + + throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); + } + + /** + * Gets the cache key to use for the cache for a given template name. + * + * @param string $name The name of the template to load + * + * @return string The cache key + */ + public function getCacheKey($name) + { + foreach ($this->loaders as $loader) { + try { + return $loader->getCacheKey($name); + } catch (Twig_Error_Loader $e) { + } + } + + throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); + } + + /** + * Returns true if the template is still fresh. + * + * @param string $name The template name + * @param timestamp $time The last modification time of the cached template + */ + public function isFresh($name, $time) + { + foreach ($this->loaders as $loader) { + try { + return $loader->isFresh($name, $time); + } catch (Twig_Error_Loader $e) { + } + } + + throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); + } +} diff --git a/test/Twig/Tests/Loader/ChainTest.php b/test/Twig/Tests/Loader/ChainTest.php new file mode 100644 index 0000000..580ae10 --- /dev/null +++ b/test/Twig/Tests/Loader/ChainTest.php @@ -0,0 +1,63 @@ + 'bar')), + new Twig_Loader_Array(array('foo' => 'foobar', 'bar' => 'foo')), + )); + + $this->assertEquals('bar', $loader->getSource('foo')); + $this->assertEquals('foo', $loader->getSource('bar')); + } + + /** + * @expectedException Twig_Error_Loader + */ + public function testGetSourceWhenTemplateDoesNotExist() + { + $loader = new Twig_Loader_Chain(array()); + + $loader->getSource('foo'); + } + + public function testGetCacheKey() + { + $loader = new Twig_Loader_Chain(array( + new Twig_Loader_Array(array('foo' => 'bar')), + new Twig_Loader_Array(array('foo' => 'foobar', 'bar' => 'foo')), + )); + + $this->assertEquals('bar', $loader->getCacheKey('foo')); + $this->assertEquals('foo', $loader->getCacheKey('bar')); + } + + /** + * @expectedException Twig_Error_Loader + */ + public function testGetCacheKeyWhenTemplateDoesNotExist() + { + $loader = new Twig_Loader_Chain(array()); + + $loader->getCacheKey('foo'); + } + + public function testAddLoader() + { + $loader = new Twig_Loader_Chain(); + $loader->addLoader(new Twig_Loader_Array(array('foo' => 'bar'))); + + $this->assertEquals('bar', $loader->getSource('foo')); + } +}