8000 Use the router to resolve the file links in the profiler · symfony/symfony@46cb297 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46cb297

Browse files
javiereguiluzfabpot
authored andcommitted
Use the router to resolve the file links in the profiler
1 parent bfae454 commit 46cb297

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
<argument>%debug.file_link_format%</argument>
5757
<argument type="service" id="request_stack" on-invalid="ignore" />
5858
<argument>null</argument>
59-
<argument>/_profiler/open?file=%%f&amp;line=%%l#line%%l</argument>
59+
<argument>?file=%%f&amp;line=%%l#line%%l</argument>
60+
<argument type="service" id="router" on-invalid="null" />
6061
</service>
6162
</services>
6263
</container>

src/Symfony/Component/HttpKernel/Debug/FileLinkFormatter.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Component\HttpKernel\Debug;
1313

14-
use Symfony\Component\HttpFoundation\Request;
1514
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1616

1717
/**
1818
* Formats debug file links.
@@ -23,10 +23,11 @@ class FileLinkFormatter implements \Serializable
2323
{
2424
private $fileLinkFormat;
2525
private $requestStack;
26+
private $router;
2627
private $baseDir;
27-
private $urlFormat;
28+
private $queryString;
2829

29-
public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
30+
public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $queryString = null, UrlGeneratorInterface $router = null)
3031
{
3132
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
3233
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
@@ -36,8 +37,9 @@ public function __construct($fileLinkFormat = null, RequestStack $requestStack =
3637

3738
$this->fileLinkFormat = $fileLinkFormat;
3839
$this->requestStack = $requestStack;
40+
$this->router = $router;
3941
$this->baseDir = $baseDir;
40-
$this->urlFormat = $urlFormat;
42+
$this->queryString = $queryString;
4143
}
4244

4345
public function format($file, $line)
@@ -75,7 +77,15 @@ private function getFileLinkFormat()
7577
if ($this->fileLinkFormat) {
7678
return $this->fileLinkFormat;
7779
}
78-
if ($this->requestStack && $this->baseDir && $this->urlFormat) {
80+
81+
if (null !== $this->router) {
82+
return array(
83+
$this->router->generate('_profiler_open_file').$this->queryString,
84+
$this->baseDir.DIRECTORY_SEPARATOR, '',
85+
);
86+
}
87+
88+
if (null !== $this->requestStack) {
7989
$request = $this->requestStack->getMasterRequest();
8090
if ($request instanceof Request) {
8191
return array(

src/Symfony/Component/HttpKernel/Tests/Debug/FileLinkFormatterTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,30 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\RequestStack;
1717
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
18+
use Symfony\Component\Routing\Generator\UrlGenerator;
19+
use Symfony\Component\Routing\RequestContext;
20+
use Symfony\Component\Routing\Route;
21+
use Symfony\Component\Routing\RouteCollection;
1822

1923
class FileLinkFormatterTest extends TestCase
2024
{
21-
public function testWhenNoFileLinkFormatAndNoRequest()
25+
public function testWhenNoFileLinkFormat()
2226
{
2327
$sut = new FileLinkFormatter();
2428

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

28-
public function testWhenFileLinkFormatAndNoRequest()
32+
public function testWhenFileLinkFormat()
2933
{
3034
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
3135

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

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

37-
public function testWhenFileLinkFormatAndRequest()
41+
public function testWhenFileLinkFormatAndRequestStack()
3842
{
3943
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
4044
$baseDir = __DIR__;
@@ -47,21 +51,33 @@ public function testWhenFileLinkFormatAndRequest()
4751
$this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
4852
}
4953

50-
public function testWhenNoFileLinkFormatAndRequest()
54+
public function testWhenFileLinkFormatAndRouter()
5155
{
5256
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
53-
$requestStack = new RequestStack();
54-
$request = new Request();
55-
$requestStack->push($request);
57+
$baseDir = __DIR__;
58+
$router = $this->getRouter();
5659

57-
$request->server->set('SERVER_NAME', 'www.example.org');
58-
$request->server->set('SERVER_PORT', 80);
59-
$request->server->set('SCRIPT_NAME', '/app.php');
60-
$request->server->set('SCRIPT_FILENAME', '/web/app.php');
61-
$request->server->set('REQUEST_URI', '/app.php/example');
60+
$sut = new FileLinkFormatter('debug://open?url=file://%f&line=%l', null, $baseDir, '/_profiler/open?file=%f&line=%l#line%l', $router);
6261

63-
$sut = new FileLinkFormatter(null, $requestStack, __DIR__, '/_profiler/open?file=%f&line=%l#line%l');
62+
$this->assertSame("debug://open?url=file://$file&line=3", $sut->format($file, 3));
63+
}
64+
65+
public function testWhenNoFileLinkFormatAndRouter()
66+
{
67+
$file = __DIR__.DIRECTORY_SEPARATOR.'file.php';
68+
$baseDir = __DIR__;
69+
$router = $this->getRouter();
70+
71+
$sut = new FileLinkFormatter(null, null, $baseDir, '?file=%f&line=%l#line%l', $router);
72+
73+
$this->assertSame('/_profiler_customized?file=file.php&line=3#line3', $sut->format($file, 3));
74+
}
75+
76+
private function getRouter()
77+
{
78+
$routes = new RouteCollection();
79+
$routes->add('_profiler_open_file', new Route('/_profiler_customized'));
6480

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

0 commit comments

Comments
 (0)
0