* @param array $arguments The parameters to be passed to the method
*
* @return Exception The previous exception or null
+ *
+ * @throws BadMethodCallException
*/
public function __call($method, $arguments)
{
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Interface all loaders must implement in order to provide extra functionality
+ * for the Twig core.
+ *
+ * @package twig
+ * @author Florin Patan <florinpatan@gmail.com>
+ */
+interface Twig_ExtendedLoaderInterface
+{
+
+ /**
+ * Check if we have the source code of a template, given its name.
+ *
+ * @param string $name The name of the template to check if we can load
+ *
+ * @return boolean If the template source code is handled by this loader or not
+ */
+ public function exists($name);
+
+}
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Array implements Twig_LoaderInterface
+class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
{
protected $templates;
}
/**
- * 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
+ * {@inheritdoc}
*/
public function getSource($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
+ * {@inheritdoc}
+ */
+ public function exists($name)
+ {
+ return isset($this->templates[(string) $name]);
+ }
+
+ /**
+ * {@inheritdoc}
*/
public function getCacheKey($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
+ * {@inheritdoc}
*/
public function isFresh($name, $time)
{
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Chain implements Twig_LoaderInterface
+class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
{
+ private $hasSourceCache = array();
protected $loaders;
/**
public function addLoader(Twig_LoaderInterface $loader)
{
$this->loaders[] = $loader;
+ $this->hasSourceCache = array();
}
/**
- * 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
+ * {@inheritdoc}
*/
public function getSource($name)
{
$exceptions = array();
foreach ($this->loaders as $loader) {
+ if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
+ continue;
+ }
+
try {
return $loader->getSource($name);
} catch (Twig_Error_Loader $e) {
}
/**
- * 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
+ * {@inheritdoc}
+ */
+ public function exists($name)
+ {
+ if (isset($this->hasSourceCache[$name])) {
+ return $this->hasSourceCache[$name];
+ }
+
+ foreach ($this->loaders as $loader) {
+ if ($loader instanceof Twig_ExtendedLoaderInterface) {
+ if ($loader->exists($name)) {
+ return $this->hasSourceCache[$name] = true;
+ }
+ } else {
+ try {
+ $loader->getSource($name);
+ return $this->hasSourceCache[$name] = true;
+ } catch (Twig_Error_Loader $e) {
+
+ }
+ }
+ }
+
+ return $this->hasSourceCache[$name] = false;
+ }
+
+ /**
+ * {@inheritdoc}
*/
public function getCacheKey($name)
{
$exceptions = array();
foreach ($this->loaders as $loader) {
+ if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
+ continue;
+ }
+
try {
return $loader->getCacheKey($name);
} catch (Twig_Error_Loader $e) {
}
/**
- * 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
+ * {@inheritdoc}
*/
public function isFresh($name, $time)
{
$exceptions = array();
foreach ($this->loaders as $loader) {
+ if ($loader instanceof Twig_ExtendedLoaderInterface && !$loader->exists($name)) {
+ continue;
+ }
+
try {
return $loader->isFresh($name, $time);
} catch (Twig_Error_Loader $e) {
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_Filesystem implements Twig_LoaderInterface
+class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
{
protected $paths;
protected $cache;
*
* @param string $path A path where to look for templates
* @param string $namespace A path name
+ *
+ * @throws Twig_Error_Loader
*/
public function addPath($path, $namespace = '__main__')
{
*
* @param string $path A path where to look for templates
* @param string $namespace A path name
+ *
+ * @throws Twig_Error_Loader
*/
public function prependPath($path, $namespace = '__main__')
{
}
/**
- * 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
+ * {@inheritdoc}
*/
public function getSource($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
+ * {@inheritdoc}
*/
public function getCacheKey($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
+ * {@inheritdoc}
+ */
+ public function exists($name)
+ {
+ if (isset($this->cache[$name])) {
+ return true;
+ }
+
+ try {
+ $this->findTemplate($name);
+
+ return true;
+ } catch (Twig_Error_Loader $exception) {
+ return false;
+ }
+ }
+
+ /**
+ * {@inheritdoc}
*/
public function isFresh($name, $time)
{
* @package twig
* @author Fabien Potencier <fabien@symfony.com>
*/
-class Twig_Loader_String implements Twig_LoaderInterface
+class Twig_Loader_String implements Twig_LoaderInterface, Twig_ExtendedLoaderInterface
{
/**
- * 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
+ * {@inheritdoc}
*/
public function getSource($name)
{
}
/**
+ * {@inheritdoc}
+ */
+ public function exists($name)
+ {
+ return true;
+ }
+
+ /**
* Gets the cache key to use for the cache for a given template name.
*
* @param string $name The name of the template to load
}
/**
- * 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
+ * {@inheritdoc}
*/
public function isFresh($name, $time)
{