From 059113e11fcbbe6ac5150c430264499d3472984d Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 9 Jul 2019 11:32:42 -0400 Subject: [PATCH] Removed templateExists method --- .../Bundle/WebProfilerBundle/CHANGELOG.md | 6 ++++ .../Controller/ExceptionController.php | 22 +------------- .../Profiler/TemplateManager.php | 29 ++----------------- .../Tests/Profiler/TemplateManagerTest.php | 21 +++++--------- 4 files changed, 16 insertions(+), 62 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md index d339b4762d..3c50252453 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md @@ -1,6 +1,12 @@ CHANGELOG ========= +5.0.0 +----- + + * removed the `ExceptionController::templateExists()` method + * removed the `TemplateManager::templateExists()` method + 4.3.0 ----- diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php index f9ccf63397..8e62ea7572 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php @@ -17,8 +17,6 @@ use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Profiler\Profiler; use Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Loader\ExistsLoaderInterface; /** * ExceptionController. @@ -97,7 +95,7 @@ class ExceptionController $template = $this->getTemplate(); - if (!$this->templateExists($template)) { + if (!$this->twig->getLoader()->exists($template)) { return new Response($this->errorRenderer->getStylesheet(), 200, ['Content-Type' => 'text/css']); } @@ -108,22 +106,4 @@ class ExceptionController { 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; - } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php index 5b75e795df..4d960c6b17 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php @@ -15,10 +15,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 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. @@ -66,6 +62,7 @@ class TemplateManager */ public function getNames(Profile $profile) { + $loader = $this->twig->getLoader(); $templates = []; foreach ($this->templates as $arguments) { @@ -83,7 +80,7 @@ class TemplateManager $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)); } @@ -92,26 +89,4 @@ class TemplateManager 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; - } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php index 33142dbf06..b3e40bfb8e 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/Profiler/TemplateManagerTest.php @@ -45,9 +45,9 @@ class TemplateManagerTest extends TestCase $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); @@ -71,7 +71,7 @@ class TemplateManagerTest extends TestCase ->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) @@ -103,17 +103,10 @@ class TemplateManagerTest extends TestCase protected function mockTwigEnvironment() { + $loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(); + $loader->method('exists')->willReturn(true); + $this->twigEnvironment = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); - - $this->twigEnvironment->expects($this->any()) - ->method('loadTemplate') - ->willReturn('loadedTemplate'); - - if (interface_exists('Twig\Loader\SourceContextLoaderInterface')) { - $loader = $this->getMockBuilder('Twig\Loader\SourceContextLoaderInterface')->getMock(); - } else { - $loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock(); - } $this->twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader); return $this->twigEnvironment;