Optimize Environment::resolveTemplate() to be much faster when template overrides do not exist

This commit is contained in:
Matias Griese
2021-12-10 13:18:08 +02:00
committed by Fabien Potencier
parent a41a6cce62
commit 695423e14e
+7 -6
View File
@@ -501,6 +501,7 @@ class Environment
$names = [$names];
}
$count = \count($names);
foreach ($names as $name) {
if ($name instanceof Template) {
return $name;
@@ -509,13 +510,13 @@ class Environment
return $name;
}
try {
return $this->loadTemplate($name);
} catch (LoaderError $e) {
if (1 === \count($names)) {
throw $e;
}
// Optimization: Avoid throwing an exception when it would be ignored anyway.
if (1 !== $count && !$this->getLoader()->exists($name)) {
continue;
}
// Throws LoaderError: Unable to find template "%s".
return $this->loadTemplate($name);
}
throw new LoaderError(sprintf('Unable to find one of the following templates: "%s".', implode('", "', $names)));