migration symfony 5 4 (#300)

* symfony 5.4 (diff dev)

* symfony 5.4 (working)

* symfony 5.4 (update autoload)

* symfony 5.4 (remove swiftmailer mailer implementation)

* symfony 5.4 (php doc and split Global accessor class)


### Impacted packages:

composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies

composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
bdalsass
2022-06-16 09:13:24 +02:00
committed by GitHub
parent abb13b70b9
commit 79da71ecf8
2178 changed files with 87439 additions and 59451 deletions

View File

@@ -11,12 +11,15 @@
namespace Symfony\Bundle\WebProfilerBundle\Controller;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\DataCollector\ExceptionDataCollector;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -24,6 +27,8 @@ use Twig\Environment;
/**
* @author Fabien Potencier <fabien@symfony.com>
*
* @internal
*/
class ProfilerController
{
@@ -32,26 +37,15 @@ class ProfilerController
private $profiler;
private $twig;
private $templates;
private $toolbarPosition;
private $cspHandler;
private $baseDir;
/**
* @param UrlGeneratorInterface $generator The URL Generator
* @param Profiler $profiler The profiler
* @param Environment $twig The twig environment
* @param array $templates The templates
* @param string $toolbarPosition The toolbar position (top, bottom, normal, or null -- use the configuration)
* @param ContentSecurityPolicyHandler $cspHandler The Content-Security-Policy handler
* @param string $baseDir The project root directory
*/
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, Environment $twig, array $templates, $toolbarPosition = 'bottom', ContentSecurityPolicyHandler $cspHandler = null, $baseDir = null)
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, Environment $twig, array $templates, ContentSecurityPolicyHandler $cspHandler = null, string $baseDir = null)
{
$this->generator = $generator;
$this->profiler = $profiler;
$this->twig = $twig;
$this->templates = $templates;
$this->toolbarPosition = $toolbarPosition;
$this->cspHandler = $cspHandler;
$this->baseDir = $baseDir;
}
@@ -59,17 +53,11 @@ class ProfilerController
/**
* Redirects to the last profiles.
*
* @return RedirectResponse A RedirectResponse instance
*
* @throws NotFoundHttpException
*/
public function homeAction()
public function homeAction(): RedirectResponse
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
return new RedirectResponse($this->generator->generate('_profiler_search_results', ['token' => 'empty', 'limit' => 10]), 302, ['Content-Type' => 'text/html']);
}
@@ -77,26 +65,17 @@ class ProfilerController
/**
* Renders a profiler panel for the given token.
*
* @param Request $request The current HTTP request
* @param string $token The profiler token
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function panelAction(Request $request, $token)
public function panelAction(Request $request, string $token): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
if (null !== $this->cspHandler) {
$this->cspHandler->disableCsp();
}
$panel = $request->query->get('panel', 'request');
$panel = $request->query->get('panel');
$page = $request->query->get('page', 'home');
if ('latest' === $token && $latest = current($this->profiler->find(null, null, 1, null, null, null))) {
@@ -104,14 +83,30 @@ class ProfilerController
}
if (!$profile = $this->profiler->loadProfile($token)) {
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', ['about' => 'no_token', 'token' => $token, 'request' => $request]), 200, ['Content-Type' => 'text/html']);
return $this->renderWithCspNonces($request, '@WebProfiler/Profiler/info.html.twig', ['about' => 'no_token', 'token' => $token, 'request' => $request]);
}
if (null === $panel) {
$panel = 'request';
foreach ($profile->getCollectors() as $collector) {
if ($collector instanceof ExceptionDataCollector && $collector->hasException()) {
$panel = $collector->getName();
break;
}
if ($collector instanceof DumpDataCollector && $collector->getDumpsCount() > 0) {
$panel = $collector->getName();
}
}
}
if (!$profile->hasCollector($panel)) {
throw new NotFoundHttpException(sprintf('Panel "%s" is not available for token "%s".', $panel, $token));
}
return new Response($this->twig->render($this->getTemplateManager()->getName($profile, $panel), [
return $this->renderWithCspNonces($request, $this->getTemplateManager()->getName($profile, $panel), [
'token' => $token,
'profile' => $profile,
'collector' => $profile->getCollector($panel),
@@ -121,26 +116,21 @@ class ProfilerController
'templates' => $this->getTemplateManager()->getNames($profile),
'is_ajax' => $request->isXmlHttpRequest(),
'profiler_markup_version' => 2, // 1 = original profiler, 2 = Symfony 2.8+ profiler
]), 200, ['Content-Type' => 'text/html']);
]);
}
/**
* Renders the Web Debug Toolbar.
*
* @param Request $request The current HTTP Request
* @param string $token The profiler token
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function toolbarAction(Request $request, $token)
public function toolbarAction(Request $request, string $token = null): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
if ($request->hasSession() && ($session = $request->getSession()) && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
if ($request->hasSession() && ($session = $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
// keep current flashes for one more request if using AutoExpireFlashBag
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
}
@@ -155,11 +145,6 @@ class ProfilerController
return new Response('', 404, ['Content-Type' => 'text/html']);
}
// the toolbar position (top, bottom, normal, or null -- use the configuration)
if (null === $position = $request->query->get('position')) {
$position = $this->toolbarPosition;
}
$url = null;
try {
$url = $this->generator->generate('_profiler', ['token' => $token], UrlGeneratorInterface::ABSOLUTE_URL);
@@ -168,8 +153,8 @@ class ProfilerController
}
return $this->renderWithCspNonces($request, '@WebProfiler/Profiler/toolbar.html.twig', [
'full_stack' => class_exists(FullStack::class),
'request' => $request,
'position' => $position,
'profile' => $profile,
'templates' => $this->getTemplateManager()->getNames($profile),
'profiler_url' => $url,
@@ -181,17 +166,11 @@ class ProfilerController
/**
* Renders the profiler search bar.
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function searchBarAction(Request $request)
public function searchBarAction(Request $request): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
if (null !== $this->cspHandler) {
$this->cspHandler->disableCsp();
@@ -239,20 +218,11 @@ class ProfilerController
/**
* Renders the search results.
*
* @param Request $request The current HTTP Request
* @param string $token The token
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function searchResultsAction(Request $request, $token)
public function searchResultsAction(Request $request, string $token): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
if (null !== $this->cspHandler) {
$this->cspHandler->disableCsp();
@@ -268,7 +238,7 @@ class ProfilerController
$end = $request->query->get('end', null);
$limit = $request->query->get('limit');
return new Response($this->twig->render('@WebProfiler/Profiler/results.html.twig', [
return $this->renderWithCspNonces($request, '@WebProfiler/Profiler/results.html.twig', [
'request' => $request,
'token' => $token,
'profile' => $profile,
@@ -281,23 +251,17 @@ class ProfilerController
'end' => $end,
'limit' => $limit,
'panel' => null,
]), 200, ['Content-Type' => 'text/html']);
]);
}
/**
* Narrows the search bar.
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function searchAction(Request $request)
public function searchAction(Request $request): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
$ip = $request->query->get('ip');
$method = $request->query->get('method');
@@ -342,17 +306,11 @@ class ProfilerController
/**
* Displays the PHP info.
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function phpinfoAction()
public function phpinfoAction(): Response
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->denyAccessIfProfilerDisabled();
if (null !== $this->cspHandler) {
$this->cspHandler->disableCsp();
@@ -368,11 +326,9 @@ class ProfilerController
/**
* Displays the source of a file.
*
* @return Response A Response instance
*
* @throws NotFoundHttpException
*/
public function openAction(Request $request)
public function openAction(Request $request): Response
{
if (null === $this->baseDir) {
throw new NotFoundHttpException('The base dir should be set.');
@@ -391,19 +347,17 @@ class ProfilerController
throw new NotFoundHttpException(sprintf('The file "%s" cannot be opened.', $file));
}
return new Response($this->twig->render('@WebProfiler/Profiler/open.html.twig', [
return $this->renderWithCspNonces($request, '@WebProfiler/Profiler/open.html.twig', [
'filename' => $filename,
'file' => $file,
'line' => $line,
]), 200, ['Content-Type' => 'text/html']);
]);
}
/**
* Gets the Template Manager.
*
* @return TemplateManager The Template Manager
*/
protected function getTemplateManager()
protected function getTemplateManager(): TemplateManager
{
if (null === $this->templateManager) {
$this->templateManager = new TemplateManager($this->profiler, $this->twig, $this->templates);
@@ -412,14 +366,23 @@ class ProfilerController
return $this->templateManager;
}
private function renderWithCspNonces(Request $request, $template, $variables, $code = 200, $headers = ['Content-Type' => 'text/html'])
private function denyAccessIfProfilerDisabled()
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
}
private function renderWithCspNonces(Request $request, string $template, array $variables, int $code = 200, array $headers = ['Content-Type' => 'text/html']): Response
{
$response = new Response('', $code, $headers);
$nonces = $this->cspHandler ? $this->cspHandler->getNonces($request, $response) : [];
$variables['csp_script_nonce'] = isset($nonces['csp_script_nonce']) ? $nonces['csp_script_nonce'] : null;
$variables['csp_style_nonce'] = isset($nonces['csp_style_nonce']) ? $nonces['csp_style_nonce'] : null;
$variables['csp_script_nonce'] = $nonces['csp_script_nonce'] ?? null;
$variables['csp_style_nonce'] = $nonces['csp_style_nonce'] ?? null;
$response->setContent($this->twig->render($template, $variables));