Skip to content

Commit

Permalink
minor #20101 Simplified link-to-source mapping definitions in debug.f…
Browse files Browse the repository at this point in the history
…ile_link_format (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

Simplified link-to-source mapping definitions in debug.file_link_format

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19950
| License       | MIT
| Doc PR        | symfony/symfony-docs#7019

Having to json_encode here (or any other kind of encoding) is really tedious to deal with: it makes it hard to have things working quickly. `%f` and `%l` aren't encoded anyway, so let's use very unlikely chars as separators here also instead.

Commits
-------

27df38e Simplified link-to-source mapping definitions in debug.file_link_format
  • Loading branch information
fabpot committed Oct 5, 2016
2 parents 3ad5b29 + 27df38e commit 63308cd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 41 deletions.
12 changes: 5 additions & 7 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Expand Up @@ -33,10 +33,8 @@ public function __construct($fileLinkFormat, $rootDir, $charset)
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}
$this->fileLinkFormat = $fileLinkFormat;
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
Expand Down Expand Up @@ -198,9 +196,9 @@ public function formatFile($file, $line, $text = null)
public function getFileLink($file, $line)
{
if ($this->fileLinkFormat && file_exists($file)) {
foreach ($this->fileLinkFormat[1] as $k => $v) {
if (0 === strpos($file, $k)) {
$file = substr_replace($file, $v, 0, strlen($k));
for ($i = 1; isset($this->fileLinkFormat[$i]); ++$i) {
if (0 === strpos($file, $k = $this->fileLinkFormat[$i++])) {
$file = substr_replace($file, $this->fileLinkFormat[$i], 0, strlen($k));
break;
}
}
Expand Down
Expand Up @@ -64,6 +64,6 @@ public function testGetName()

protected function getExtension()
{
return new CodeExtension('proto://%f#&line=%l#'.json_encode(substr(__FILE__, 0, 5)).':"foobar"', '/root', 'UTF-8');
return new CodeExtension('proto://%f#&line=%l&'.substr(__FILE__, 0, 5).'>foobar', '/root', 'UTF-8');
}
}
Expand Up @@ -35,10 +35,8 @@ public function __construct($fileLinkFormat, $rootDir, $charset)
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}
$this->fileLinkFormat = $fileLinkFormat;
$this->rootDir = str_replace('\\', '/', $rootDir).'/';
Expand Down Expand Up @@ -193,9 +191,9 @@ public function formatFile($file, $line, $text = null)
public function getFileLink($file, $line)
{
if ($this->fileLinkFormat && is_file($file)) {
foreach ($this->fileLinkFormat[1] as $k => $v) {
if (0 === strpos($file, $k)) {
$file = substr_replace($file, $v, 0, strlen($k));
for ($i = 1; isset($this->fileLinkFormat[$i]); ++$i) {
if (0 === strpos($file, $k = $this->fileLinkFormat[$i++])) {
$file = substr_replace($path, $this->fileLinkFormat[$i], 0, strlen($k));
break;
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Expand Up @@ -39,10 +39,8 @@ public function __construct($debug = true, $charset = null, $fileLinkFormat = nu
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}
$this->debug = $debug;
$this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8';
Expand Down Expand Up @@ -97,9 +95,8 @@ public function setFileLinkFormat($fileLinkFormat)
{
$old = $this->fileLinkFormat;
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = strpos($fileLinkFormat, '#"') ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}
$this->fileLinkFormat = $fileLinkFormat;
if ($old) {
Expand Down Expand Up @@ -368,9 +365,9 @@ private function formatPath($path, $line)
$file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path);

if ($fileLinkFormat = $this->fileLinkFormat) {
foreach ($fileLinkFormat[1] as $k => $v) {
if (0 === strpos($path, $k)) {
$path = substr_replace($path, $v, 0, strlen($k));
for ($i = 1; isset($fileLinkFormat[$i]); ++$i) {
if (0 === strpos($path, $k = $fileLinkFormat[$i++])) {
$path = substr_replace($path, $fileLinkFormat[$i], 0, strlen($k));
break;
}
}
Expand Down
Expand Up @@ -42,10 +42,8 @@ public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null,
{
$fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
if ($fileLinkFormat && !is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}
$this->stopwatch = $stopwatch;
$this->fileLinkFormat = $fileLinkFormat;
Expand Down Expand Up @@ -268,9 +266,9 @@ private function doDump($data, $name, $file, $line)
$name = strip_tags($this->style('', $name));
$file = strip_tags($this->style('', $file));
if ($fileLinkFormat) {
foreach ($fileLinkFormat[1] as $k => $v) {
if (0 === strpos($file, $k)) {
$file = substr_replace($file, $v, 0, strlen($k));
for ($i = 1; isset($fileLinkFormat[$i]); ++$i) {
if (0 === strpos($file, $k = $fileLinkFormat[$i++])) {
$file = substr_replace($file, $fileLinkFormat[$i], 0, strlen($k));
break;
}
}
Expand Down
14 changes: 5 additions & 9 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Expand Up @@ -542,16 +542,12 @@ private function getSourceLink($file, $line)
return false;
}
if (!is_array($fileLinkFormat)) {
$i = max(strpos($fileLinkFormat, '%f'), strpos($fileLinkFormat, '%l'));
$i = strpos($fileLinkFormat, '#"', $i) ?: strlen($fileLinkFormat);
$fileLinkFormat = array(substr($fileLinkFormat, 0, $i), substr($fileLinkFormat, $i + 1));
$fileLinkFormat[1] = @json_decode('{'.$fileLinkFormat[1].'}', true) ?: array();
$this->extraDisplayOptions['fileLinkFormat'] = $fileLinkFormat;
$i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
$fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
}

foreach ($fileLinkFormat[1] as $k => $v) {
if (0 === strpos($file, $k)) {
$file = substr_replace($file, $v, 0, strlen($k));
for ($i = 1; isset($fileLinkFormat[$i]); ++$i) {
if (0 === strpos($file, $k = $fileLinkFormat[$i++])) {
$file = substr_replace($file, $fileLinkFormat[$i], 0, strlen($k));
break;
}
}
Expand Down

0 comments on commit 63308cd

Please sign in to comment.