Skip to content

Commit

Permalink
bug #26758 [WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL…
Browse files Browse the repository at this point in the history
… 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
-------

e074c05 [WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format generation lazy
  • Loading branch information
fabpot committed Apr 3, 2018
2 parents ad30087 + e074c05 commit 341682e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -53,6 +56,11 @@ public function load(array $configs, ContainerBuilder $container)
$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')));
}
}

/**
Expand Down
22 changes: 8 additions & 14 deletions src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml
Expand Up @@ -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>
21 changes: 21 additions & 0 deletions src/Symfony/Component/HttpKernel/Debug/FileLinkFormatter.php
Expand Up @@ -13,6 +13,8 @@

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.
Expand All @@ -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');
Expand Down Expand Up @@ -70,6 +75,18 @@ public function unserialize($serialized)
}
}

/**
* @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) {
Expand All @@ -78,6 +95,10 @@ private function getFileLinkFormat()
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, '',
Expand Down

0 comments on commit 341682e

Please sign in to comment.