8000 Add SimpleFormatter for faster exporting · laravel101/laravel-debugbar@664fa43 · GitHub
[go: up one dir, main page]

Skip to content

Commit 664fa43

Browse files
committed
Add SimpleFormatter for faster exporting
1 parent 59a7a08 commit 664fa43

File tree

4 files changed

+115
-14
lines changed

4 files changed

+115
-14
lines changed

src/DataCollector/EventCollector.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Barryvdh\Debugbar\DataCollector;
33

4+
use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
45
use DebugBar\DataCollector\TimeDataCollector;
56
use Illuminate\Events\Dispatcher;
67
use Illuminate\Support\Str;
@@ -11,14 +12,10 @@ class EventCollector extends TimeDataCollector
1112
/** @var Dispatcher */
1213
protected $events;
1314

14-
/** @var VarCloner */
15-
protected $exporter;
16-
1715
public function __construct($requestStartTime = null)
1816
{
1917
parent::__construct($requestStartTime);
20-
21-
$this->exporter = new VarCloner();
18+
$this->setDataFormatter(new SimpleFormatter());
2219
}
2320

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

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

8279
return $data;

src/DataCollector/GateCollector.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Barryvdh\Debugbar\DataCollector;
44

5+
use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
56
use DebugBar\DataCollector\MessagesCollector;
67
use Illuminate\Contracts\Auth\Access\Gate;
78
use Illuminate\Contracts\Auth\Access\Authorizable;
@@ -12,16 +13,13 @@
1213
*/
1314
class GateCollector extends MessagesCollector
1415
{
15-
/** @var ValueExporter */
16-
protected $exporter;
1716
/**
1817
* @param Gate $gate
1918
*/
2019
public function __construct(Gate $gate)
2120
{
2221
parent::__construct('gate');
23-
$this->exporter = new VarCloner();
24-
22+
$this->setDataFormatter(new SimpleFormatter());
2523
$gate->after([$this, 'addCheck']);
2624
}
2725

@@ -33,7 +31,7 @@ public function addCheck(Authorizable $user = null, $ability, $result, $argument
3331
'ability' => $ability,
3432
'result' => $result,
3533
($user ? snake_case(class_basename($user)) : 'user') => ($user ? $user->id : null),
36-
'arguments' => $this->exporter->cloneVar($arguments),
34+
'arguments' => $this->getDataFormatter()->formatVar($arguments),
3735
], $label, false);
3836
}
3937
}

src/DataCollector/ViewCollector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Barryvdh\Debugbar\DataCollector;
44

5+
use Barryvdh\Debugbar\DataFormatter\SimpleFormatter;
56
use DebugBar\Bridge\Twig\TwigCollector;
67
use Illuminate\View\View;
78
use Symfony\Component\VarDumper\Cloner\VarCloner;
@@ -18,10 +19,10 @@ class ViewCollector extends TwigCollector
1819
*/
1920
public function __construct($collectData = true)
2021
{
22+
$this->setDataFormatter(new SimpleFormatter());
2123
$this->collect_data = $collectData;
2224
$this->name = 'views';
2325
$this->templates = [];
24-
$this->exporter = new VarCloner();
2526
}
2627

2728
public function getName()
@@ -75,7 +76,7 @@ public function addView(View $view)
7576
} else {
7677
$data = [];
7778
foreach ($view->getData() as $key => $value) {
78-
$data[$key] = $this->exporter->cloneVar($value);
79+
$data[$key] = $this->getDataFormatter()->formatVar($value);
7980
}
8081
$params = $data;
8182
}

src/DataFormatter/SimpleFormatter.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\DataFormatter;
4+
5+
use DebugBar\DataFormatter\DataFormatter;
6+
7+
/**
8+
* Simple DataFormatter based on the deprecated Symfony ValueExporter
9+
*
10+
* @see https://github.com/symfony/symfony/blob/v3.4.4/src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php
11+
*/
12+
class SimpleFormatter extends DataFormatter
13+
{
14+
/**
15+
* @param $data
16+
* @return string
17+
*/
18+
public function formatVar($data)
19+
{
20+
return $this->exportValue($data);
21+
}
22+
23+
/**
24+
* Converts a PHP value to a string.
25+
*
26+
* @param mixed $value The PHP value
27+
* @param int $depth Only for internal usage
28+
* @param bool $deep Only for internal usage
29+
*
30+
* @return string The string representation of the given value
31+
* @author Bernhard Schussek <bschussek@gmail.com>
32+
*/
33+
private function exportValue($value, $depth = 1, $deep = false)
34+
{
35+
if ($value instanceof \__PHP_Incomplete_Class) {
36+
return sprintf('__PHP_Incomplete_Class(%s)', $this->getClassNameFromIncomplete($value));
37+
}
38+
39+
if (is_object($value)) {
40+
if ($value instanceof \DateTimeInterface) {
41+
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ATOM));
42+
}
43+
44+
return sprintf('Object(%s)', get_class($value));
45+
}
46+
47+
if (is_array($value)) {
48+
if (empty($value)) {
49+
return '[]';
50+
}
51+
52+
$indent = str_repeat(' ', $depth);
53+
54+
$a = array();
55+
foreach ($value as $k => $v) {
56+
if (is_array($v)) {
57+
$deep = true;
58+
}
59+
$a[] = sprintf('%s => %s', $k, $this->exportValue($v, $depth + 1, $deep));
60+
}
61+
62+
if ($deep) {
63+
return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), str_repeat(' ', $depth - 1));
64+
}
65+
66+
$s = sprintf('[%s]', implode(', ', $a));
67+
68+
if (80 > strlen($s)) {
69+
return $s;
70+
}
71+
72+
return sprintf("[\n%s%s\n]", $indent, implode(sprintf(",\n%s", $indent), $a));
73+
}
74+
75+
if (is_resource($value)) {
76+
return sprintf('Resource(%s#%d)', get_resource_type($value), $value);
77+
}
78+
79+
if (null === $value) {
80+
return 'null';
81+
}
82+
83+
if (false === $value) {
84+
return 'false';
85+
}
86+
87+
if (true === $value) {
88+
return 'true';
89+
}
90+
91+
return (string) $value;
92+
}
93+
94+
/**
95+
* @param \__PHP_Incomplete_Class $value
96+
* @return mixed
97+
* @author Bernhard Schussek <bschussek@gmail.com>
98+
*/
99+
private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
100+
{
101+
$array = new \ArrayObject($value);
102+
103+
return $array['__PHP_Incomplete_Class_Name'];
104+
}
105+
}

0 commit comments

Comments
 (0)
0