From 846d3e0e58821013327618a9489bce0c2404d5a5 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 23 Jul 2019 19:59:27 -0400 Subject: [PATCH] Decoupling TwigBundle and using the new ErrorRenderer mechanism --- UPGRADE-4.4.md | 2 +- UPGRADE-5.0.md | 5 ++ .../Bundle/WebProfilerBundle/CHANGELOG.md | 1 + .../Controller/ExceptionController.php | 4 ++ .../Controller/ExceptionErrorController.php | 59 +++++++++++++++++++ .../Resources/config/profiler.xml | 8 ++- .../Resources/config/routing/profiler.xml | 4 +- .../views/Collector/exception.css.twig | 2 - .../views/Collector/exception.html.twig | 1 + .../views/Collector/messenger.html.twig | 4 +- .../Resources/views/Profiler/base.html.twig | 2 +- .../views/Profiler/toolbar_redirect.html.twig | 2 +- .../views/images/icon-minus-square.svg | 1 + .../views/images/icon-plus-square.svg | 1 + .../WebProfilerExtensionTest.php | 8 ++- .../Bundle/WebProfilerBundle/composer.json | 1 + 16 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionErrorController.php create mode 100644 src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-minus-square.svg create mode 100644 src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-plus-square.svg diff --git a/UPGRADE-4.4.md b/UPGRADE-4.4.md index 492591a5d4..ad0bb2420c 100644 --- a/UPGRADE-4.4.md +++ b/UPGRADE-4.4.md @@ -169,7 +169,7 @@ Validator WebProfilerBundle ----------------- - * Deprecated the `ExceptionController::templateExists()` method + * Deprecated the `ExceptionController` class in favor of `ExceptionErrorController` * Deprecated the `TemplateManager::templateExists()` method WebServerBundle diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index ce0ad3e656..678416c586 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -577,6 +577,11 @@ Yaml * The parser is now stricter and will throw a `ParseException` when a mapping is found inside a multi-line string. +WebProfilerBundle +----------------- + + * Removed the `ExceptionController` class, use `ExceptionErrorController` instead. + WebServerBundle --------------- diff --git a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md index 821a86b756..f44dd269b2 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Added button to clear the ajax request tab * Deprecated the `ExceptionController::templateExists()` method * Deprecated the `TemplateManager::templateExists()` method + * Deprecated the `ExceptionController` in favor of `ExceptionErrorController` 4.3.0 ----- diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php index f9298965bf..15c54e0c63 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php @@ -20,10 +20,14 @@ use Twig\Environment; use Twig\Error\LoaderError; use Twig\Loader\ExistsLoaderInterface; +@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.', ExceptionController::class, ExceptionErrorController::class), E_USER_DEPRECATED); + /** * ExceptionController. * * @author Fabien Potencier + * + * @deprecated since Symfony 4.4, use the ExceptionErrorController instead. */ class ExceptionController { diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionErrorController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionErrorController.php new file mode 100644 index 0000000000..c0833067cf --- /dev/null +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionErrorController.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\WebProfilerBundle\Controller; + +use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; +use Symfony\Component\HttpKernel\Profiler\Profiler; + +/** + * Renders the exception panel. + * + * @author Yonel Ceruto + */ +class ExceptionErrorController +{ + private $htmlErrorRenderer; + private $profiler; + + public function __construct(HtmlErrorRenderer $htmlErrorRenderer, ?Profiler $profiler) + { + $this->htmlErrorRenderer = $htmlErrorRenderer; + $this->profiler = $profiler; + } + + /** + * Renders the exception panel stacktrace for the given token. + */ + public function body(string $token): Response + { + if (null === $this->profiler) { + throw new NotFoundHttpException('The profiler must be enabled.'); + } + + $exception = $this->profiler->loadProfile($token) + ->getCollector('exception') + ->getException() + ; + + return new Response($this->htmlErrorRenderer->getBody($exception)); + } + + /** + * Renders the exception panel stylesheet. + */ + public function stylesheet(): Response + { + return new Response($this->htmlErrorRenderer->getStylesheet()); + } +} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml index dcacc51032..962e1418bd 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml @@ -27,7 +27,13 @@ %kernel.debug% - + + The "%service_id%" service is deprecated since Symfony 4.4, use the "web_profiler.controller.exception_error" service instead. + + + + + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/profiler.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/profiler.xml index 0bc9a9ec4f..0eb4ea72ff 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/profiler.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/profiler.xml @@ -37,11 +37,11 @@ - web_profiler.controller.exception::showAction + web_profiler.controller.exception_error::body - web_profiler.controller.exception::cssAction + web_profiler.controller.exception_error::stylesheet diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.css.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.css.twig index 78752853b9..ea028e026f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.css.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.css.twig @@ -1,5 +1,3 @@ -{{ include('@Twig/exception.css.twig') }} - .container { max-width: auto; margin: 0; diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig index 94dfbb6aca..261d5cc2b1 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig @@ -4,6 +4,7 @@ {% if collector.hasexception %} {% endif %} {{ parent() }} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/messenger.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/messenger.html.twig index 779f1259ed..6f2af8dd45 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/messenger.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/messenger.html.twig @@ -119,8 +119,8 @@ exception {% endif %} - {{ include('@Twig/images/icon-minus-square.svg') }} - {{ include('@Twig/images/icon-plus-square.svg') }} + {{ include('@WebProfiler/images/icon-minus-square.svg') }} + {{ include('@WebProfiler/images/icon-plus-square.svg') }} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base.html.twig index 580b3b5b0e..0b13f57509 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/base.html.twig @@ -4,7 +4,7 @@ - Symfony Profiler + {% block title %}Symfony Profiler{% endblock %} {% block head %} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig index 35b6e90eb5..18d43b2253 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig @@ -1,4 +1,4 @@ -{% extends '@Twig/layout.html.twig' %} +{% extends '@WebProfiler/Profiler/base.html.twig' %} {% block title 'Redirection Intercepted' %} diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-minus-square.svg b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-minus-square.svg new file mode 100644 index 0000000000..471c2741c7 --- /dev/null +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-minus-square.svg @@ -0,0 +1 @@ + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-plus-square.svg b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-plus-square.svg new file mode 100644 index 0000000000..2f5c3b3583 --- /dev/null +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/images/icon-plus-square.svg @@ -0,0 +1 @@ + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php index cfbee00bd0..c2ac0a75ef 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php @@ -54,7 +54,7 @@ class WebProfilerExtensionTest extends TestCase $this->kernel = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\KernelInterface')->getMock(); $this->container = new ContainerBuilder(); - $this->container->register('error_renderer.renderer.html', HtmlErrorRenderer::class); + $this->container->register('error_renderer.renderer.html', HtmlErrorRenderer::class)->setPublic(true); $this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true); $this->container->register('router', $this->getMockClass('Symfony\\Component\\Routing\\RouterInterface'))->setPublic(true); $this->container->register('twig', 'Twig\Environment')->setPublic(true); @@ -92,10 +92,11 @@ class WebProfilerExtensionTest extends TestCase $extension = new WebProfilerExtension(); $extension->load([[]], $this->container); + $this->container->removeDefinition('web_profiler.controller.exception'); $this->assertFalse($this->container->has('web_profiler.debug_toolbar')); - $this->assertSaneContainer($this->getCompiledContainer()); + self::assertSaneContainer($this->getCompiledContainer()); } /** @@ -105,10 +106,11 @@ class WebProfilerExtensionTest extends TestCase { $extension = new WebProfilerExtension(); $extension->load([['toolbar' => $toolbarEnabled, 'intercept_redirects' => $interceptRedirects]], $this->container); + $this->container->removeDefinition('web_profiler.controller.exception'); $this->assertSame($listenerInjected, $this->container->has('web_profiler.debug_toolbar')); - $this->assertSaneContainer($this->getCompiledContainer(), '', ['web_profiler.csp.handler']); + self::assertSaneContainer($this->getCompiledContainer(), '', ['web_profiler.csp.handler']); if ($listenerInjected) { $this->assertSame($listenerEnabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled()); diff --git a/src/Symfony/Bundle/WebProfilerBundle/composer.json b/src/Symfony/Bundle/WebProfilerBundle/composer.json index 190fc6d117..d0cf3153cd 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/composer.json +++ b/src/Symfony/Bundle/WebProfilerBundle/composer.json @@ -18,6 +18,7 @@ "require": { "php": "^7.1.3", "symfony/config": "^4.2|^5.0", + "symfony/error-renderer": "^4.4|^5.0", "symfony/http-kernel": "^4.4", "symfony/routing": "^3.4|^4.0|^5.0", "symfony/twig-bundle": "^4.2|^5.0",