Skip to content

Commit

Permalink
Add SimpleFormatter for faster exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Feb 25, 2018
1 parent 59a7a08 commit 664fa43
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 14 deletions.
11 changes: 4 additions & 7 deletions src/DataCollector/EventCollector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Barryvdh\Debugbar\DataCollector;

use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
use DebugBar\DataCollector\TimeDataCollector;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Str;
Expand All @@ -11,14 +12,10 @@ class EventCollector extends TimeDataCollector
/** @var Dispatcher */
protected $events;

/** @var VarCloner */
protected $exporter;

public function __construct($requestStartTime = null)
{
parent::__construct($requestStartTime);

$this->exporter = new VarCloner();
$this->setDataFormatter(new SimpleFormatter());
}

public function onWildcardEvent($name = null, $data = [])
Expand Down Expand Up @@ -55,7 +52,7 @@ public function onWildcardEvent($name = null, $data = [])
$listener = $reflector->getName() . ' (' . $filename . ':' . $reflector->getStartLine() . '-' . $reflector->getEndLine() . ')';
} else {
// Not sure if this is possible, but to prevent edge cases
$listener = $this->formatVar($listener);
$listener = $this->getDataFormatter()->formatVar($listener);
}

$params['listeners.' . $i] = $listener;
Expand All @@ -76,7 +73,7 @@ protected function prepareParams($params)
if (is_object($value) && Str::is('Illuminate\*\Events\*', get_class($value))) {
$value = $this->prepareParams(get_object_vars($value));
}
$data[$key] = htmlentities($this->exporter->cloneVar($value), ENT_QUOTES, 'UTF-8', false);
$data[$key] = htmlentities($this->getDataFormatter()->formatVar($value), ENT_QUOTES, 'UTF-8', false);
}

return $data;
Expand Down
8 changes: 3 additions & 5 deletions src/DataCollector/GateCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Barryvdh\Debugbar\DataCollector;

use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
use DebugBar\DataCollector\MessagesCollector;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Access\Authorizable;
Expand All @@ -12,16 +13,13 @@
*/
class GateCollector extends MessagesCollector
{
/** @var ValueExporter */
protected $exporter;
/**
* @param Gate $gate
*/
public function __construct(Gate $gate)
{
parent::__construct('gate');
$this->exporter = new VarCloner();

$this->setDataFormatter(new SimpleFormatter());
$gate->after([$this, 'addCheck']);
}

Expand All @@ -33,7 +31,7 @@ public function addCheck(Authorizable $user = null, $ability, $result, $argument
'ability' => $ability,
'result' => $result,
($user ? snake_case(class_basename($user)) : 'user') => ($user ? $user->id : null),
'arguments' => $this->exporter->cloneVar($arguments),
'arguments' => $this->getDataFormatter()->formatVar($arguments),
], $label, false);
}
}
5 changes: 3 additions & 2 deletions src/DataCollector/ViewCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Barryvdh\Debugbar\DataCollector;

use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
use DebugBar\Bridge\Twig\TwigCollector;
use Illuminate\View\View;
use Symfony\Component\VarDumper\Cloner\VarCloner;
Expand All @@ -18,10 +19,10 @@ class ViewCollector extends TwigCollector
*/
public function __construct($collectData = true)
{
$this->setDataFormatter(new SimpleFormatter());
$this->collect_data = $collectData;
$this->name = 'views';
$this->templates = [];
$this->exporter = new VarCloner();
}

public function getName()
Expand Down Expand Up @@ -75,7 +76,7 @@ public function addView(View $view)
} else {
$data = [];
foreach ($view->getData() as $key => $value) {
$data[$key] = $this->exporter->cloneVar($value);
$data[$key] = $this->getDataFormatter()->formatVar($value);
}
$params = $data;
}
Expand Down
105 changes: 105 additions & 0 deletions src/DataFormatter/SimpleFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Barryvdh\Debugbar\DataFormatter;

use DebugBar\DataFormatter\DataFormatter;

/**
* Simple DataFormatter based on the deprecated Symfony ValueExporter
*
* @see https://github.com/symfony/symfony/blob/v3.4.4/src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php
*/
class SimpleFormatter extends DataFormatter
{
/**
* @param $data
* @return string
*/
public function formatVar($data)
{
return $this->exportValue($data);
}

/**
* Converts a PHP value to a string.
*
* @param mixed $value The PHP value
* @param int $depth Only for internal usage
* @param bool $deep Only for internal usage
*
* @return string The string representation of the given value
* @author Bernhard Schussek <[email protected]>
*/
private function exportValue($value, $depth = 1, $deep = false)
{
if ($value instanceof \__PHP_Incomplete_Class) {
return sprintf('__PHP_Incomplete_Class(%s)', $this->getClassNameFromIncomplete($value));
}

if (is_object($value)) {
if ($value instanceof \DateTimeInterface) {
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ATOM));
}

return sprintf('Object(%s)', get_class($value));
}

if (is_array($value)) {
if (empty($value)) {
return '[]';
}

$indent = str_repeat(' ', $depth);

$a = array();
foreach ($value as $k => $v) {
if (is_array($v)) {
$deep = true;
}
$a[] = sprintf('%s => %s', $k, $this->exportValue($v, $depth + 1, $deep));
}

if ($deep) {
return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), str_repeat(' ', $depth - 1));
}

$s = sprintf('[%s]', implode(', ', $a));

if (80 > strlen($s)) {
return $s;
}

return sprintf("[\n%s%s\n]", $indent, implode(sprintf(",\n%s", $indent), $a));
}

if (is_resource($value)) {
return sprintf('Resource(%s#%d)', get_resource_type($value), $value);
}

if (null === $value) {
return 'null';
}

if (false === $value) {
return 'false';
}

if (true === $value) {
return 'true';
}

return (string) $value;
}

/**
* @param \__PHP_Incomplete_Class $value
* @return mixed
* @author Bernhard Schussek <[email protected]>
*/
private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
{
$array = new \ArrayObject($value);

return $array['__PHP_Incomplete_Class_Name'];
}
}

0 comments on commit 664fa43

Please sign in to comment.