Skip to content

Commit 9f55b34

Browse files
committed
Sync Twig templateExists behaviors
1 parent 395a0b8 commit 9f55b34

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

TwigEngine.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Twig\Error\Error;
2020
use Twig\Error\LoaderError;
2121
use Twig\Loader\ExistsLoaderInterface;
22+
use Twig\Loader\SourceContextLoaderInterface;
2223
use Twig\Template;
2324

2425
/**
@@ -74,19 +75,24 @@ public function exists($name)
7475

7576
$loader = $this->environment->getLoader();
7677

77-
if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
78-
return $loader->exists((string) $name);
79-
}
78+
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
79+
try {
80+
// cast possible TemplateReferenceInterface to string because the
81+
// EngineInterface supports them but LoaderInterface does not
82+
if ($loader instanceof SourceContextLoaderInterface) {
83+
$loader->getSourceContext((string) $name);
84+
} else {
85+
$loader->getSource((string) $name);
86+
}
87+
88+
return true;
89+
} catch (LoaderError $e) {
90+
}
8091

81-
try {
82-
// cast possible TemplateReferenceInterface to string because the
83-
// EngineInterface supports them but LoaderInterface does not
84-
$loader->getSourceContext((string) $name)->getCode();
85-
} catch (LoaderError $e) {
8692
return false;
8793
}
8894

89-
return true;
95+
return $loader->exists((string) $name);
9096
}
9197

9298
/**

0 commit comments

Comments
 (0)