[Debug] add some file link format handling

This commit is contained in:
Nicolas Grekas 2014-09-25 19:46:28 +02:00
parent 15dfb0614e
commit c6923afcd0
5 changed files with 52 additions and 28 deletions

View File

@ -57,27 +57,6 @@ class FrameworkExtension extends Extension
// Property access is used by both the Form and the Validator component
$loader->load('property_access.xml');
$loader->load('debug_prod.xml');
if ($container->getParameter('kernel.debug')) {
$loader->load('debug.xml');
$definition = $container->findDefinition('debug.debug_handlers_listener');
$definition->replaceArgument(0, array(new Reference('http_kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'terminateWithException'));
$definition = $container->findDefinition('http_kernel');
$definition->replaceArgument(2, new Reference('debug.controller_resolver'));
// replace the regular event_dispatcher service with the debug one
$definition = $container->findDefinition('event_dispatcher');
$definition->setPublic(false);
$container->setDefinition('debug.event_dispatcher.parent', $definition);
$container->setAlias('event_dispatcher', 'debug.event_dispatcher');
} else {
$definition = $container->findDefinition('debug.debug_handlers_listener');
$definition->replaceArgument(2, E_COMPILE_ERROR | E_PARSE | E_ERROR | E_CORE_ERROR);
}
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
@ -144,6 +123,30 @@ class FrameworkExtension extends Extension
$loader->load('serializer.xml');
}
$loader->load('debug_prod.xml');
$definition = $container->findDefinition('debug.debug_handlers_listener');
if ($container->hasParameter('templating.helper.code.file_link_format')) {
$definition->replaceArgument(4, '%templating.helper.code.file_link_format%');
}
if ($container->getParameter('kernel.debug')) {
$loader->load('debug.xml');
$definition->replaceArgument(0, array(new Reference('http_kernel', ContainerInterface::NULL_ON_INVALID_REFERENCE), 'terminateWithException'));
$definition = $container->findDefinition('http_kernel');
$definition->replaceArgument(2, new Reference('debug.controller_resolver'));
// replace the regular event_dispatcher service with the debug one
$definition = $container->findDefinition('event_dispatcher');
$definition->setPublic(false);
$container->setDefinition('debug.event_dispatcher.parent', $definition);
$container->setAlias('event_dispatcher', 'debug.event_dispatcher');
} else {
$definition->replaceArgument(2, E_COMPILE_ERROR | E_PARSE | E_ERROR | E_CORE_ERROR);
}
$this->addClassesToCompile(array(
'Symfony\\Component\\Config\\FileLocator',

View File

@ -17,6 +17,7 @@
<argument type="service" id="logger" on-invalid="null" />
<argument /><!-- Log levels map for enabled error levels -->
<argument>%kernel.debug%</argument>
<argument>null</argument><!-- %templating.helper.code.file_link_format% -->
</service>
<service id="debug.stopwatch" class="%debug.stopwatch.class%" />

View File

@ -37,7 +37,7 @@ class CodeHelper extends Helper
*/
public function __construct($fileLinkFormat, $rootDir, $charset)
{
$this->fileLinkFormat = empty($fileLinkFormat) ? ini_get('xdebug.file_link_format') : $fileLinkFormat;
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->rootDir = str_replace('\\', '/', $rootDir).'/';
$this->charset = $charset;
}

View File

@ -37,10 +37,12 @@ class ExceptionHandler
private $handler;
private $caughtBuffer;
private $caughtLength;
private $fileLinkFormat;
public function __construct($debug = true)
public function __construct($debug = true, $fileLinkFormat = null)
{
$this->debug = $debug;
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
}
/**
@ -50,9 +52,9 @@ class ExceptionHandler
*
* @return ExceptionHandler The registered exception handler
*/
public static function register($debug = true)
public static function register($debug = true, $fileLinkFormat = null)
{
$handler = new static($debug);
$handler = new static($debug, $fileLinkFormat = null);
$prev = set_exception_handler(array($handler, 'handle'));
if (is_array($prev) && $prev[0] instanceof ErrorHandler) {
@ -81,6 +83,21 @@ class ExceptionHandler
return $old;
}
/**
* Sets the format for links to source files.
*
* @param string $format The format for links to source files
*
* @return string The previous file link format.
*/
public function setFileLinkFormat($format)
{
$old = $this->fileLinkFormat;
$this->fileLinkFormat = $format;
return $old;
}
/**
* Sends a response for the given Exception.
*
@ -353,7 +370,7 @@ EOF;
$path = self::utf8Htmlize($path);
$file = preg_match('#[^/\\\\]*$#', $path, $file) ? $file[0] : $path;
if ($linkFormat = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format')) {
if ($linkFormat = $this->fileLinkFormat) {
$link = str_replace(array('%f', '%l'), array($path, $line), $linkFormat);
return sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $link, $file, $line);

View File

@ -34,13 +34,15 @@ class DebugHandlersListener implements EventSubscriberInterface
* @param LoggerInterface|null $logger A PSR-3 logger
* @param array|int $levels An array map of E_* to LogLevel::* or an integer bit field of E_* constants
* @param bool $debug Enables/disables debug mode
* @param string $fileLinkFormat The format for links to source files
*/
public function __construct($exceptionHandler, LoggerInterface $logger = null, $levels = null, $debug = true)
public function __construct($exceptionHandler, LoggerInterface $logger = null, $levels = null, $debug = true, $fileLinkFormat = null)
{
$this->exceptionHandler = $exceptionHandler;
$this->logger = $logger;
$this->levels = $levels;
$this->debug = $debug;
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
}
public function configure()
@ -76,6 +78,7 @@ class DebugHandlersListener implements EventSubscriberInterface
}
if ($handler instanceof ExceptionHandler) {
$handler->setHandler($this->exceptionHandler);
$handler->setFileLinkFormat($this->fileLinkFormat);
}
$this->exceptionHandler = null;
}