diff --git a/src/DataCollector/EventCollector.php b/src/DataCollector/EventCollector.php index f3cfdde9..572cab53 100644 --- a/src/DataCollector/EventCollector.php +++ b/src/DataCollector/EventCollector.php @@ -1,6 +1,7 @@ exporter = new VarCloner(); + $this->setDataFormatter(new SimpleFormatter()); } public function onWildcardEvent($name = null, $data = []) @@ -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; @@ -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; diff --git a/src/DataCollector/GateCollector.php b/src/DataCollector/GateCollector.php index 93dcf1de..a9a3e39e 100644 --- a/src/DataCollector/GateCollector.php +++ b/src/DataCollector/GateCollector.php @@ -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; @@ -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']); } @@ -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); } } diff --git a/src/DataCollector/ViewCollector.php b/src/DataCollector/ViewCollector.php index 4805aa90..b7c657e1 100644 --- a/src/DataCollector/ViewCollector.php +++ b/src/DataCollector/ViewCollector.php @@ -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; @@ -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() @@ -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; } diff --git a/src/DataFormatter/SimpleFormatter.php b/src/DataFormatter/SimpleFormatter.php new file mode 100644 index 00000000..0a1fc938 --- /dev/null +++ b/src/DataFormatter/SimpleFormatter.php @@ -0,0 +1,105 @@ +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 + */ + 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 + */ + private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value) + { + $array = new \ArrayObject($value); + + return $array['__PHP_Incomplete_Class_Name']; + } +}