Skip to content

[WebProfilerBundle] Removed templateExists method #32461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

5.0.0
-----

* removed the `ExceptionController::templateExists()` method
* removed the `TemplateManager::templateExists()` method

4.3.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;

/**
* ExceptionController.
Expand Down Expand Up @@ -97,7 +95,7 @@ public function cssAction(string $token)

$template = $this->getTemplate();

if (!$this->templateExists($template)) {
if (!$this->twig->getLoader()->exists($template)) {
return new Response($this->errorRenderer->getStylesheet(), 200, ['Content-Type' => 'text/css']);
}

Expand All @@ -108,22 +106,4 @@ protected function getTemplate()
{
return '@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig';
}

// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists(string $template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof ExistsLoaderInterface) {
return $loader->exists($template);
}

try {
$loader->getSource($template);

return true;
} catch (LoaderError $e) {
}

return false;
}
}
29 changes: 2 additions & 27 deletions src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\Template;

/**
* Profiler Templates Manager.
Expand Down Expand Up @@ -66,6 +62,7 @@ public function getName(Profile $profile, string $panel)
*/
public function getNames(Profile $profile)
{
$loader = $this->twig->getLoader();
$templates = [];

foreach ($this->templates as $arguments) {
Expand All @@ -83,7 +80,7 @@ public function getNames(Profile $profile)
$template = substr($template, 0, -10);
}

if (!$this->templateExists($template.'.html.twig')) {
if (!$loader->exists($template.'.html.twig')) {
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
}

Expand All @@ -92,26 +89,4 @@ public function getNames(Profile $profile)

return $templates;
}

// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists(string $template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof ExistsLoaderInterface) {
return $loader->exists($template);
}

try {
if ($loader instanceof SourceContextLoaderInterface || method_exists($loader, 'getSourceContext')) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}

return true;
} catch (LoaderError $e) {
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ protected function setUp()
$profiler = $this->mockProfiler();
$twigEnvironment = $this->mockTwigEnvironment();
$templates = [
'data_collector.foo' => ['foo', 'FooBundle:Collector:foo'],
'data_collector.bar' => ['bar', 'FooBundle:Collector:bar'],
'data_collector.baz' => ['baz', 'FooBundle:Collector:baz'],
'data_collector.foo' => ['foo', '@Foo/Collector/foo.html.twig'],
'data_collector.bar' => ['bar', '@Foo/Collector/bar.html.twig'],
'data_collector.baz' => ['baz', '@Foo/Collector/baz.html.twig'],
];

$this->templateManager = new TemplateManager($profiler, $twigEnvironment, $templates);
Expand All @@ -71,7 +71,7 @@ public function testGetNameValidTemplate()
->withAnyParameters()
->willReturnCallback([$this, 'profilerHasCallback']);

$this->assertEquals('FooBundle:Collector:foo.html.twig', $this->templateManager->getName(new ProfileDummy(), 'foo'));
$this->assertEquals('@Foo/Collector/foo.html.twig', $this->templateManager->getName(new ProfileDummy(), 'foo'));
}

public function profilerHasCallback($panel)
Expand Down Expand Up @@ -103,17 +103,10 @@ protected function mockProfile()

protected function mockTwigEnvironment()
{
$this->twigEnvironment = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();

$this->twigEnvironment->expects($this->any())
->method('loadTemplate')
->willReturn('loadedTemplate');
$loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock();
$loader->method('exists')->willReturn(true);

if (interface_exists('Twig\Loader\SourceContextLoaderInterface')) {
$loader = $this->getMockBuilder('Twig\Loader\SourceContextLoaderInterface')->getMock();
} else {
$loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock();
}
$this->twigEnvironment = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
$this->twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader);

return $this->twigEnvironment;
Expand Down