8000 [WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format generation lazy by nicolas-grekas · Pull Request #26758 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 1 commit into from
Apr 3, 2018
Merged
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
[WebProfilerBundle][HttpKernel] Make FileLinkFormatter URL format gen…
…eration lazy
  • Loading branch information
nicolas-grekas committed Apr 3, 2018
commit e074c0550cc3e8a3d59b4174a785d365a28733e2
Original file line number Diff line number Diff line change
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) {
Copy link
Member

Choose a reason for hiding this comment

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

this needs to be Kernel::VERSION_ID >= 30408 && Kernel::VERSION_ID < 40000 || Kernel::VERSION_ID >= 40008)

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed in 946eefa

$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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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