bug #26758 [WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format generation lazy (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format generation lazy

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26755, #26754
| License       | MIT
| Doc PR        | -

Commits
-------

e074c0550c [WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format generation lazy
This commit is contained in:
Fabien Potencier 2018-04-03 14:19:24 +02:00
commit 341682e079
3 changed files with 37 additions and 14 deletions

View File

@ -11,10 +11,13 @@
namespace Symfony\Bundle\WebProfilerBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
/**
@ -53,6 +56,11 @@ class WebProfilerExtension extends Extension
$container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']);
$container->setParameter('web_profiler.debug_toolbar.mode', $config['toolbar'] ? WebDebugToolbarListener::ENABLED : WebDebugToolbarListener::DISABLED);
}
if (Kernel::VERSION_ID >= 30408 || Kernel::VERSION_ID >= 40008) {
$container->getDefinition('debug.file_link_formatter')
->replaceArgument(3, new ServiceClosureArgument(new Reference('debug.file_link_formatter.url_format')));
}
}
/**

View File

@ -56,20 +56,14 @@
<argument>%debug.file_link_format%</argument>
<argument type="service" id="request_stack" on-invalid="ignore" />
<argument>%kernel.project_dir%</argument>
<argument type="service">
<service class="string">
<factory function="implode" />
<argument type="collection">
<argument type="service">
<service class="string">
<factory service="router" method="generate" />
<argument>_profiler_open_file</argument>
</service>
</argument>
<argument>?file=%%f&amp;line=%%l#line%%l</argument>
</argument>
</service>
</argument>
<argument>/_profiler/open?file=%%f&amp;line=%%l#line%%l</argument>
</service>
<service id="debug.file_link_formatter.url_format" class="string">
<factory class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter" method="generateUrlFormat" />
<argument type="service" id="router" />
<argument>_profiler_open_file</argument>
<argument>?file=%%f&amp;line=%%l#line%%l</argument>
</service>
</services>
</container>

View File

@ -13,6 +13,8 @@ namespace Symfony\Component\HttpKernel\Debug;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\ExceptionInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Formats debug file links.
@ -26,6 +28,9 @@ class FileLinkFormatter implements \Serializable
private $baseDir;
private $urlFormat;
/**
* @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand
*/
public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
@ -70,6 +75,18 @@ class FileLinkFormatter implements \Serializable
}
}
/**
* @internal
*/
public static function generateUrlFormat(UrlGeneratorInterface $router, $routeName, $queryString)
{
try {
return $router->generate($routeName).$queryString;
} catch (ExceptionInterface $e) {
return null;
}
}
private function getFileLinkFormat()
{
if ($this->fileLinkFormat) {
@ -78,6 +95,10 @@ class FileLinkFormatter implements \Serializable
if ($this->requestStack && $this->baseDir && $this->urlFormat) {
$request = $this->requestStack->getMasterRequest();
if ($request instanceof Request) {
if ($this->urlFormat instanceof \Closure && !$this->urlFormat = \call_user_func($this->urlFormat)) {
return;
}
return array(
$request->getSchemeAndHttpHost().$request->getBaseUrl().$this->urlFormat,
$this->baseDir.DIRECTORY_SEPARATOR, '',