-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WebProfilerBundle] display profiler url in logs #60505
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
Open
jeremyFreeAgent
wants to merge
4
commits into
symfony:7.4
Choose a base branch
from
jeremyFreeAgent:feat/web-profiler-bundle-profiler-url-log
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9e0b5ef
[WebProfilerBundle] display profiler url in logs
jeremyFreeAgent 22559d0
add changelog entry
jeremyFreeAgent bd46580
use final and private attributes
jeremyFreeAgent 6143514
Update src/Symfony/Bundle/WebProfilerBundle/EventListener/ProfilerLin…
jeremyFreeAgent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/Symfony/Bundle/WebProfilerBundle/EventListener/ProfilerLinkLogListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,60 @@ | ||||||
<?php | ||||||
|
||||||
/* | ||||||
* This file is part of the Symfony package. | ||||||
* | ||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||
* | ||||||
* For the full copyright and license information, please view the LICENSE | ||||||
* file that was distributed with this source code. | ||||||
*/ | ||||||
|
||||||
namespace Symfony\Bundle\WebProfilerBundle\EventListener; | ||||||
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||||||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||||||
use Symfony\Component\HttpKernel\KernelEvents; | ||||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||||||
use Psr\Log\LoggerInterface; | ||||||
|
||||||
/** | ||||||
* @author Jérémy Romey jeremyFreeAgent <jeremy@free-agent.fr> | ||||||
*/ | ||||||
final class ProfilerLinkLogListener implements EventSubscriberInterface | ||||||
{ | ||||||
public function __construct( | ||||||
private ?LoggerInterface $logger = null, | ||||||
private ?UrlGeneratorInterface $urlGenerator = null, | ||||||
) { | ||||||
} | ||||||
|
||||||
public function onKernelResponse(ResponseEvent $event): void | ||||||
{ | ||||||
if (null === $this->logger) { | ||||||
return; | ||||||
} | ||||||
if (null === $this->urlGenerator) { | ||||||
return; | ||||||
} | ||||||
|
||||||
$response = $event->getResponse(); | ||||||
$request = $event->getRequest(); | ||||||
|
||||||
if (!$event->isMainRequest()) { | ||||||
return; | ||||||
} | ||||||
|
||||||
if (!$response->headers->has('X-Debug-Token')) { | ||||||
return; | ||||||
} | ||||||
|
||||||
$this->logger->debug(\sprintf('See profiler at %s', $this->urlGenerator->generate('_profiler', ['token' => $response->headers->get('X-Debug-Token')], UrlGeneratorInterface::ABSOLUTE_URL))); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logger should do the interpolation. Does it work like this?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use directly |
||||||
} | ||||||
|
||||||
public static function getSubscribedEvents(): array | ||||||
{ | ||||||
return [ | ||||||
KernelEvents::RESPONSE => ['onKernelResponse', -2048], | ||||||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
]; | ||||||
} | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/ProfilerLinkLogListenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\WebProfilerBundle\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\WebProfilerBundle\EventListener\ProfilerLinkLogListener; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Kernel; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* @author Jérémy Romey jeremyFreeAgent <jeremy@free-agent.fr> | ||
*/ | ||
final class ProfilerLinkLogListenerTest extends TestCase | ||
{ | ||
public function testProfilerLinkLog() | ||
{ | ||
$response = new Response('I love Symfony', 200); | ||
$response->headers->set('Location', 'https://example.com/'); | ||
$response->headers->set('X-Debug-Token', '04bb3f'); | ||
|
||
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
->expects($this->once()) | ||
->method('debug') | ||
->with('See profiler at http://mydomain.com/_profiler/04bb3f') | ||
; | ||
|
||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class); | ||
$urlGenerator | ||
->expects($this->once()) | ||
->method('generate') | ||
->with('_profiler', ['token' => '04bb3f'], UrlGeneratorInterface::ABSOLUTE_URL) | ||
->willReturn('http://mydomain.com/_profiler/04bb3f') | ||
; | ||
|
||
$listener = new ProfilerLinkLogListener($logger, $urlGenerator); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testProfilerLinkLogShouldNotLogWhenNoLogger() | ||
{ | ||
$response = new Response('I love Symfony', 200); | ||
$response->headers->set('Location', 'https://example.com/'); | ||
$response->headers->set('X-Debug-Token', '04bb3f'); | ||
|
||
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response); | ||
|
||
$logger = null; | ||
|
||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class); | ||
$urlGenerator | ||
->expects($this->never()) | ||
->method('generate') | ||
; | ||
|
||
$listener = new ProfilerLinkLogListener($logger, $urlGenerator); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testProfilerLinkLogShouldNotLogWhenNoUrlGenerator() | ||
{ | ||
$response = new Response('I love Symfony', 200); | ||
$response->headers->set('Location', 'https://example.com/'); | ||
$response->headers->set('X-Debug-Token', '04bb3f'); | ||
|
||
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
->expects($this->never()) | ||
->method('debug') | ||
; | ||
|
||
$urlGenerator = null; | ||
|
||
$listener = new ProfilerLinkLogListener($logger, $urlGenerator); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testProfilerLinkLogShouldNotLogWhenNotMainRequest() | ||
{ | ||
$response = new Response('I love Symfony', 200); | ||
$response->headers->set('Location', 'https://example.com/'); | ||
$response->headers->set('X-Debug-Token', '04bb3f'); | ||
|
||
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::SUB_REQUEST, $response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
->expects($this->never()) | ||
->method('debug') | ||
; | ||
|
||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class); | ||
$urlGenerator | ||
->expects($this->never()) | ||
->method('generate') | ||
; | ||
|
||
$listener = new ProfilerLinkLogListener($logger, $urlGenerator); | ||
$listener->onKernelResponse($event); | ||
} | ||
|
||
public function testProfilerLinkLogShouldNotLogWhenNoToken() | ||
{ | ||
$response = new Response('I love Symfony', 200); | ||
$response->headers->set('Location', 'https://example.com/'); | ||
|
||
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$logger | ||
->expects($this->never()) | ||
->method('debug') | ||
; | ||
|
||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class); | ||
$urlGenerator | ||
->expects($this->never()) | ||
->method('generate') | ||
; | ||
|
||
$listener = new ProfilerLinkLogListener($logger, $urlGenerator); | ||
$listener->onKernelResponse($event); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are feature freeze for 7.3, hopefully this feature will be for 7.4. You can add the 7.4 title.