protected $paths;
protected $cache;
- protected $errorCache;
/**
* Constructor.
public function addPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
- $this->cache = $this->errorCache = array();
+ $this->cache = array();
if (!is_dir($path)) {
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
- $this->cache = $this->errorCache = array();
+ $this->cache = array();
if (!is_dir($path)) {
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
*/
public function exists($name)
{
- return $this->doFindTemplate($this->normalizeName($name));
+ $name = (string) $name;
+ if (isset($this->cache[$name])) {
+ return true;
+ }
+
+ try {
+ $this->findTemplate($name);
+
+ return true;
+ } catch (Twig_Error_Loader $exception) {
+ return false;
+ }
}
/**
protected function findTemplate($name)
{
- $name = $this->normalizeName($name);
+ $name = (string) $name;
- if ($this->doFindTemplate($name)) {
- return $this->cache[$name];
- }
-
- throw new Twig_Error_Loader($this->errorCache[$name]);
- }
-
- protected function doFindTemplate($name)
- {
- $this->validateName($name);
+ // normalize name
+ $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
if (isset($this->cache[$name])) {
- return true;
+ return $this->cache[$name];
}
- if (isset($this->errorCache[$name])) {
- return false;
- }
+ $this->validateName($name);
$namespace = self::MAIN_NAMESPACE;
- $shortname = $name;
if (isset($name[0]) && '@' == $name[0]) {
if (false === $pos = strpos($name, '/')) {
- $this->errorCache[$name] = sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name);
-
- return false;
+ throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
}
$namespace = substr($name, 1, $pos - 1);
- $shortname = substr($name, $pos + 1);
+
+ $name = substr($name, $pos + 1);
}
if (!isset($this->paths[$namespace])) {
- $this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);
-
- return false;
+ throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
}
foreach ($this->paths[$namespace] as $path) {
- if (is_file($path.'/'.$shortname)) {
- $this->cache[$name] = $path.'/'.$shortname;
-
- return true;
+ if (is_file($path.'/'.$name)) {
+ return $this->cache[$name] = $path.'/'.$name;
}
}
- $this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]));
-
- return false;
- }
-
- protected function normalizeName($name)
- {
- return preg_replace('#/{2,}#', '/', strtr((string) $name, '\\', '/'));
+ throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
}
protected function validateName($name)
$loader->addPath(sys_get_temp_dir(), 'named');
$this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces());
}
-
- public function testFindTemplateExceptionNamespace()
- {
- $basePath = dirname(__FILE__).'/Fixtures';
-
- $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
- $loader->addPath($basePath.'/named', 'named');
-
- try {
- $loader->getSource('@named/nowhere.html');
- } catch (Exception $e) {
- $this->assertInstanceof('Twig_Error_Loader', $e);
- $this->assertContains('Unable to find template "@named/nowhere.html"', $e->getMessage());
- }
- }
-
- public function testFindTemplateWithCache()
- {
- $basePath = dirname(__FILE__).'/Fixtures';
-
- $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
- $loader->addPath($basePath.'/named', 'named');
-
- // prime the cache for index.html in the named namespace
- $namedSource = $loader->getSource('@named/index.html');
- $this->assertEquals("named path\n", $namedSource);
-
- // get index.html from the main namespace
- $this->assertEquals("path\n", $loader->getSource('index.html'));
- }
}