Skip to content

Commit

Permalink
simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 23, 2019
1 parent 8613aad commit 653dddd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 16 deletions.
16 changes: 4 additions & 12 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function load($name)
return new TemplateWrapper($this, $name);
}

return new TemplateWrapper($this, $this->loadTemplate($name));
return new TemplateWrapper($this, $this->loadTemplate($this->getTemplateClass($name), $name));
}

/**
Expand All @@ -376,15 +376,7 @@ public function load($name)
*
* @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) {
Expand Down Expand Up @@ -459,7 +451,7 @@ public function createTemplate($template, string $name = null)

$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);
}
Expand Down Expand Up @@ -510,7 +502,7 @@ public function resolveTemplate($names)
}

try {
return $this->loadTemplate($name);
return $this->load($name);
} catch (LoaderError $e) {
if (1 === \count($names)) {
throw $e;
Expand Down
6 changes: 3 additions & 3 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
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());
Expand Down
2 changes: 1 addition & 1 deletion test/Twig/Tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function testFailLoadTemplate()
{
$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)
Expand Down

0 comments on commit 653dddd

Please sign in to comment.