Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] Output the location of calls to dump() #31446

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
Sep 28, 2019
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
13 changes: 13 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
<argument>0</argument> <!-- flags -->
</service>

<service id="var_dumper.contextualized_cli_dumper" class="Symfony\Component\VarDumper\Dumper\ContextualizedDumper" decorates="var_dumper.cli_dumper">
<argument type="service" id="var_dumper.contextualized_cli_dumper.inner" />
<argument type="collection">
<argument type="service" key="source">
<service class="Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider">
<argument>%kernel.charset%</argument>
<argument type="string">%kernel.project_dir%</argument>
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
</service>
</argument>
</argument>
</service>

<service id="var_dumper.html_dumper" class="Symfony\Component\VarDumper\Dumper\HtmlDumper">
<argument>null</argument>
<argument>%kernel.charset%</argument>
Expand Down
26 changes: 25 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\VarDumper\Cloner;

use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;

/**
* @author Nicolas Grekas <[email protected]>
Expand All @@ -24,6 +25,7 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate
private $maxDepth = 20;
private $maxItemsPerDepth = -1;
private $useRefHandles = -1;
private $context = [];

/**
* @param array $data An array as returned by ClonerInterface::cloneVar()
Expand Down Expand Up @@ -227,6 +229,17 @@ public function withRefHandles($useRefHandles)
return $data;
}

/**
* @return static
*/
public function withContext(array $context)
{
$data = clone $this;
$data->context = $context;

return $data;
}

/**
* Seeks to a specific key in nested data structures.
*
Expand Down Expand Up @@ -281,7 +294,18 @@ public function seek($key)
public function dump(DumperInterface $dumper)
{
$refs = [0];
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
$cursor = new Cursor();

if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) {
$cursor->attr['if_links'] = true;
$cursor->hashType = -1;
$dumper->dumpScalar($cursor, 'default', '^');
$cursor->attr = ['if_links' => true];
$dumper->dumpScalar($cursor, 'default', ' ');
$cursor->hashType = 0;
}

$this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct($output = null, string $charset = null, int $flags =
]);
}

$this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f';
$this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l';
}

/**
Expand Down Expand Up @@ -490,6 +490,8 @@ protected function style($style, $value, $attr = [])
if (isset($attr['href'])) {
$value = "\033]8;;{$attr['href']}\033\\{$value}\033]8;;\033\\";
}
} elseif ($attr['if_links'] ?? false) {
return '';
}

return $value;
Expand Down Expand Up @@ -548,6 +550,10 @@ protected function dumpLine($depth, $endOfValue = false)

protected function endValue(Cursor $cursor)
{
if (-1 === $cursor->hashType) {
return;
}

if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) {
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
$this->line .= ',';
Expand Down Expand Up @@ -628,7 +634,7 @@ private function isWindowsTrueColor(): bool
private function getSourceLink(string $file, int $line)
{
if ($fmt = $this->displayOptions['fileLinkFormat']) {
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file);
return \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : ($fmt->format($file, $line) ?: 'file://'.$file.'#L'.$line);
}

return false;
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/ContextualizedDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;

/**
* @author Kévin Thérage <[email protected]>
*/
class ContextualizedDumper implements DataDumperInterface
{
private $wrappedDumper;
private $contextProviders;

/**
* @param ContextProviderInterface[] $contextProviders
*/
public function __construct(DataDumperInterface $wrappedDumper, array $contextProviders)
{
$this->wrappedDumper = $wrappedDumper;
$this->contextProviders = $contextProviders;
}

public function dump(Data $data)
{
$context = [];
foreach ($this->contextProviders as $contextProvider) {
$context[\get_class($contextProvider)] = $contextProvider->getContext();
}

$this->wrappedDumper->dump($data->withContext($context));
}
}
25 changes: 24 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/Cloner/VarClonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function testMaxIntBoundary()
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[context:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
)

)

EOTXT;
Expand Down Expand Up @@ -141,6 +145,10 @@ public function testClone()
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[context:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
)

)

EOTXT;
Expand Down Expand Up @@ -309,6 +317,10 @@ public function testLimits()
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[context:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
)

)

EOTXT;
Expand All @@ -327,7 +339,7 @@ public function testJsonCast()
$clone = $cloner->cloneVar($data);

$expected = <<<'EOTXT'
object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
object(Symfony\Component\VarDumper\Cloner\Data)#%d (7) {
["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
array(2) {
[0]=>
Expand Down Expand Up @@ -372,6 +384,9 @@ public function testJsonCast()
int(-1)
["useRefHandles":"Symfony\Component\VarDumper\Cloner\Data":private]=>
int(-1)
["context":"Symfony\Component\VarDumper\Cloner\Data":private]=>
array(0) {
}
}

EOTXT;
Expand Down Expand Up @@ -432,6 +447,10 @@ public function testCaster()
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[context:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
)

)

EOTXT;
Expand Down Expand Up @@ -501,6 +520,10 @@ public function testPhp74()
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[context:Symfony\Component\VarDumper\Cloner\Data:private] => Array
(
)

)

EOTXT;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Dumper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;

/**
* @author Kévin Thérage <[email protected]>
*/
class ContextualizedDumperTest extends TestCase
{
public function testContextualizedCliDumper()
{
$wrappedDumper = new CliDumper('php://output');
$wrappedDumper->setColors(true);

$var = 'example';
$href = sprintf('file://%s#L%s', __FILE__, 37);
$dumper = new ContextualizedDumper($wrappedDumper, [new SourceContextProvider()]);
$cloner = new VarCloner();
$data = $cloner->cloneVar($var);

ob_start();
$dumper->dump($data);
$out = ob_get_clean();

$this->assertStringContainsString("\e]8;;{$href}\e\\\e[", $out);
$this->assertStringContainsString("m{$var}\e[", $out);
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

// Load the global dump() function
Expand All @@ -38,6 +40,8 @@ public static function dump($var)
$dumper = \in_array(\PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper() : new HtmlDumper();
}

$dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]);

self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
Expand Down