8000 [VarDumper][PhpUnitBridge] VarDumperServerListener by ogizanagi · Pull Request #26696 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper][PhpUnitBridge] VarDumperServerListener #26696

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[VarDumper] Easy server dumper registration
  • Loading branch information
ogizanagi committed Apr 16, 2018
commit bd797735642033de7cb6a3d0d85df15a548f56e2
44 changes: 44 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/ServerDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@

namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\VarDumper;

/**
* ServerDumper forwards serialized Data clones to a server.
Expand Down Expand Up @@ -42,6 +50,42 @@ public function __construct(string $host, DataDumperInterface $wrappedDumper = n
$this->contextProviders = $contextProviders;
}

/**
* @final
*/
public static function getDefaultContextProviders(Request $request = null, string $projectDir = null): array
{
$contextProviders = array();

if ('cli' !== PHP_SAPI && ($request || class_exists(Request::class))) {
$requestStack = new RequestStack();
$requestStack->push($request ?? Request::createFromGlobals());
$contextProviders['request'] = new RequestContextProvider($requestStack);
}

$fileLinkFormatter = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null, $projectDir) : null;

return $contextProviders + array(
'cli' => new CliContextProvider(),
'source' => new SourceContextProvider(null, $projectDir, $fileLinkFormatter),
);
}

/**
* @final
*/
public static function register(string $host = null, bool $lock = false, array $contextProviders = array()): ?callable
{
$contextProviders += self::getDefaultContextProviders();
$host = $host ?? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912';
$cloner = new VarCloner();
$dumper = new self($host, VarDumper::getDefaultDumper(), $contextProviders);

return VarDumper::setHandler(function ($var) use ($dumper, $cloner) {
$dumper->dump($cloner->cloneVar($var));
}, $lock);
}

public function getContextProviders(): array
{
return $this->contextProviders;
Expand Down
26 changes: 24 additions & 2 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

// Load the global dump() function
Expand All @@ -24,12 +25,13 @@
class VarDumper
{
private static $handler;
private static $locked = false;

public static function dump($var)
{
if (null === self::$handler) {
$cloner = new VarCloner();
$dumper = \in_array(PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
$dumper = self::getDefaultDumper();
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
Expand All @@ -38,11 +40,31 @@ public static function dump($var)
return call_user_func(self::$handler, $var);
}

public static function setHandler(callable $callable = null)
/**
* @final since 4.1
*/
public static function setHandler(callable $callable = null/*, bool $lock = false*/)/*: ?callable*/
{
$lock = \func_num_args() > 1 ? func_get_arg(1) : false;
$prevHandler = self::$handler;

if (self::$locked) {
return $prevHandler;
}
if ($lock) {
self::$locked = true;
}

self::$handler = $callable;

return $prevHandler;
}

/**
* @final
*/
public static function getDefaultDumper(): DataDumperInterface
{
return \in_array(PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
}
}
0