Skip to content

Use the router to resolve the file links in the profiler #24153

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@

<service id="debug.file_link_formatter" class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter">
<argument>%debug.file_link_format%</argument>
<argument type="service" id="request_stack" on-invalid="ignore" />
<argument type="service" id="router" on-invalid="null" />
<argument>null</argument>
<argument>/_profiler/open?file=%%f&amp;line=%%l#line%%l</argument>
<argument>?file=%%f&amp;line=%%l#line%%l</argument>
</service>
</services>
</container>
32 changes: 26 additions & 6 deletions src/Symfony/Component/HttpKernel/Debug/FileLinkFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

namespace Symfony\Component\HttpKernel\Debug;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Formats debug file links.
Expand All @@ -23,10 +23,14 @@ class FileLinkFormatter implements \Serializable
{
private $fileLinkFormat;
private $requestStack;
private $urlGenerator;
private $baseDir;
private $urlFormat;
private $queryString;

public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
/**
* @param $urlGenerator UrlGeneratorInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param UrlGeneratorInterface $urlGenerator

*/
public function __construct($fileLinkFormat = null, $urlGenerator = null, $baseDir = null, $queryString = null)
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
Expand All @@ -35,9 +39,17 @@ public function __construct($fileLinkFormat = null, RequestStack $requestStack =
}

$this->fileLinkFormat = $fileLinkFormat;
$this->requestStack = $requestStack;
$this->baseDir = $baseDir;
$this->urlFormat = $urlFormat;
$this->queryString = $queryString;

if ($urlGenerator instanceof RequestStack) {
@trigger_error(sprintf('Passing a RequestStack to %s() as a second argument is deprecated since version 3.4 and will be unsupported in 4.0. Pass a UrlGeneratorInterface instead.', __METHOD__), E_USER_DEPRECATED);
$this->requestStack = $urlGenerator;
} elseif ($urlGenerator instanceof UrlGeneratorInterface) {
$this->urlGenerator = $urlGenerator;
} elseif (null !== $urlGenerator) {
throw new \InvalidArgumentException('The second argument of %s() must either implement UrlGeneratorInterface or RequestStack.');
}
}

public function format($file, $line)
Expand Down Expand Up @@ -75,7 +87,15 @@ private function getFileLinkFormat()
if ($this->fileLinkFormat) {
return $this->fileLinkFormat;
}
if ($this->requestStack && $this->baseDir && $this->urlFormat) {

if (null !== $this->urlGenerator) {
return array(
$this->urlGenerator->generate('_profiler_open_file').$this->queryString,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That ties it to the definition of WebProfilerBundle. I suggest that we make it configurable instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we please do that in the future in case enough people ask for it?

$this->baseDir.DIRECTORY_SEPARATOR, '',
);
}

if (null !== $this->requestStack) {
$request = $this->requestStack->getMasterRequest();
if ($request instanceof Request) {
return array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,33 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class FileLinkFormatterTest extends TestCase
{
public function testWhenNoFileLinkFormatAndNoRequest()
public function testWhenNoFileLinkFormat()
{
$sut = new FileLinkFormatter();

$this->assertFalse($sut->format('/kernel/root/src/my/very/best/file.php', 3));
}

public function testWhenFileLinkFormatAndNoRequest()
public function testWhenFileLinkFormat()
{
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';

$sut = new FileLinkFormatter('debug://open?url=file://%f&line=%l', new RequestStack());
$sut = new FileLinkFormatter('debug://open?url=file://%f&line=%l');

$this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
}

public function testWhenFileLinkFormatAndRequest()
/**
* @group legacy
*/
public function testWhenFileLinkFormatAndRequestStack()
{
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
$baseDir = __DIR__;
Expand All @@ -47,21 +54,31 @@ public function testWhenFileLinkFormatAndRequest()
$this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
}

public function testWhenNoFileLinkFormatAndRequest()
public function testWhenFileLinkFormatAndRouter()
{
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
$requestStack = new RequestStack();
$request = new Request();
$requestStack->push($request);
$baseDir = __DIR__;

$request->server->set('SERVER_NAME', 'www.example.org');
$request->server->set('SERVER_PORT', 80);
$request->server->set('SCRIPT_NAME', '/app.php');
$request->server->set('SCRIPT_FILENAME', '/web/app.php');
$request->server->set('REQUEST_URI', '/app.php/example');
$sut = new FileLinkFormatter('debug://open?url=file://%f&line=%l', $this->getUrlGenerator(), $baseDir, '/_profiler/open?file=%f&line=%l#line%l');

$sut = new FileLinkFormatter(null, $requestStack, __DIR__, '/_profiler/open?file=%f&line=%l#line%l');
$this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
}

public function testWhenNoFileLinkFormatAndRouter()
{
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
$baseDir = __DIR__;

$sut = new FileLinkFormatter(null, $this->getUrlGenerator(), $baseDir, '?file=%f&line=%l#line%l');

$this->assertSame('/_profiler_customized?file=file.php&line=3#line3', $sut->format($file, 3));
}

private function getUrlGenerator()
{
$routes = new RouteCollection();
$routes->add('_profiler_open_file', new Route('/_profiler_customized'));

$this->assertSame('http://www.example.org/app.php/_profiler/open?file=file.php&line=3#line3', $sut->format($file, 3));
return new UrlGenerator($routes, new RequestContext());
}
}