From 653dddd1f962e5cf6818f11e8757c82becd35a4f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 16 Mar 2019 11:55:08 +0100 Subject: [PATCH] simplified code --- src/Environment.php | 16 ++++------------ src/Template.php | 6 +++--- test/Twig/Tests/EnvironmentTest.php | 2 +- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 99a9e3a4a..6232f4961 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -356,7 +356,7 @@ class Environment return new TemplateWrapper($this, $name); } - return new TemplateWrapper($this, $this->loadTemplate($name)); + return new TemplateWrapper($this, $this->loadTemplate($this->getTemplateClass($name), $name)); } /** @@ -376,15 +376,7 @@ class Environment * * @internal */ - public function loadTemplate($name, $index = null) - { - return $this->loadClass($this->getTemplateClass($name), $name, $index); - } - - /** - * @internal - */ - public function loadClass($cls, $name, $index = null) + public function loadTemplate(string $cls, string $name, int $index = null): Template { $mainCls = $cls; if (null !== $index) { @@ -459,7 +451,7 @@ class Environment $this->setLoader($loader); try { - return new TemplateWrapper($this, $this->loadTemplate($name)); + return new TemplateWrapper($this, $this->loadTemplate($this->getTemplateClass($name), $name)); } finally { $this->setLoader($current); } @@ -510,7 +502,7 @@ class Environment } try { - return $this->loadTemplate($name); + return $this->load($name); } catch (LoaderError $e) { if (1 === \count($names)) { throw $e; diff --git a/src/Template.php b/src/Template.php index ed49b01c3..c8067186a 100644 --- a/src/Template.php +++ b/src/Template.php @@ -320,11 +320,11 @@ abstract class Template if (false !== $pos = strrpos($class, '___', -1)) { $class = substr($class, 0, $pos); } - - return $this->env->loadClass($class, $template, $index); + } else { + $class = $this->env->getTemplateClass($template); } - return $this->env->loadTemplate($template, $index); + return $this->env->loadTemplate($class, $template, $index); } catch (Error $e) { if (!$e->getSourceContext()) { $e->setSourceContext($templateName ? new Source('', $templateName) : $this->getSourceContext()); diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 03bdb6823..ad7ef591e 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -367,7 +367,7 @@ class Twig_Tests_EnvironmentTest extends \PHPUnit\Framework\TestCase { $template = 'testFailLoadTemplate.twig'; $twig = new Environment(new ArrayLoader([$template => false])); - $twig->loadTemplate($template, 112233); + $twig->loadTemplate($twig->getTemplateClass($template), $template, 112233); } protected function getMockLoader($templateName, $templateContent)