Skip to content

Commit

Permalink
Merge 1e0c855 into a17b253
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrukowski committed Sep 10, 2018
2 parents a17b253 + 1e0c855 commit 70c11d1
Show file tree
Hide file tree
Showing 23 changed files with 366 additions and 288 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## [1.0.2] - 2018-09-10

* Refactor, external API has not changed

## [1.0.1] - 2018-04-21

* Use [`get_object_vars`](http://php.net/manual/en/function.get-object-vars.php)
Expand Down
35 changes: 35 additions & 0 deletions src/Helpers/BoolValue.php
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\Helpers;

/**
* @internal
*/
class BoolValue
{
private $value;

public function __construct($value = false)
{
$this->value = $value;
}

public function getValue()
{
return $this->value;
}

public function setValue($value)
{
$this->value = $value;
}
}
89 changes: 89 additions & 0 deletions src/Helpers/Container.php
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\Helpers;

use Awesomite\VarDumper\Config\Config;
use Awesomite\VarDumper\LightVarDumper;
use Awesomite\VarDumper\Objects\HasherFactory;
use Awesomite\VarDumper\Objects\HasherInterface;

/**
* @internal
*/
class Container
{
private $references;

private $depth;

private $config;

private $printNlOnEnd;

private $dumper;

private static $hasher;

public function __construct(Config $config, LightVarDumper $dumper)
{
$this->config = $config;
$this->dumper = $dumper;
}

/**
* @return Config
*/
public function getConfig()
{
return $this->config;
}

/**
* @return Stack
*/
public function getReferences()
{
return $this->references ?: $this->references = new Stack();
}

/**
* @return IntValue
*/
public function getDepth()
{
return $this->depth ?: $this->depth = new IntValue();
}

/**
* @return BoolValue
*/
public function getPrintNlOnEnd()
{
return $this->printNlOnEnd ?: $this->printNlOnEnd = new BoolValue(true);
}

/**
* @return LightVarDumper
*/
public function getDumper()
{
return $this->dumper;
}

/**
* @return HasherInterface
*/
public function getHasher()
{
return self::$hasher ?: self::$hasher = HasherFactory::create();
}
}
81 changes: 51 additions & 30 deletions src/LightVarDumper.php
Expand Up @@ -12,8 +12,7 @@
namespace Awesomite\VarDumper;

use Awesomite\VarDumper\Config\EditableConfig;
use Awesomite\VarDumper\Helpers\IntValue;
use Awesomite\VarDumper\Helpers\Stack;
use Awesomite\VarDumper\Helpers\Container;
use Awesomite\VarDumper\Helpers\Strings;
use Awesomite\VarDumper\Subdumpers\ArrayBigDumper;
use Awesomite\VarDumper\Subdumpers\ArrayRecursiveDumper;
Expand All @@ -24,7 +23,7 @@
use Awesomite\VarDumper\Subdumpers\ObjectBigDumper;
use Awesomite\VarDumper\Subdumpers\ObjectDebugInfoDumper;
use Awesomite\VarDumper\Subdumpers\ObjectRecursiveDumper;
use Awesomite\VarDumper\Subdumpers\ObjectTooDepthArrayDumper;
use Awesomite\VarDumper\Subdumpers\ObjectTooDepthDumper;
use Awesomite\VarDumper\Subdumpers\ResourceDumper;
use Awesomite\VarDumper\Subdumpers\ScalarDumper;
use Awesomite\VarDumper\Subdumpers\StringDumper;
Expand All @@ -39,15 +38,16 @@ final class LightVarDumper extends InternalVarDumper
const DEFAULT_MAX_DEPTH = 5;
const DEFAULT_INDENT = ' ';

/**
* @var Container
*/
private $container;

/**
* @var EditableConfig
*/
private $config;

private $depth;

private $references;

/**
* @var SubdumperInterface[]
*/
Expand All @@ -60,63 +60,84 @@ public function __construct($displayPlaceInCode = false, $stepShift = 0)
{
parent::__construct($displayPlaceInCode, $stepShift);

$this->config = new Config\EditableConfig(
$this->config = $config = new Config\EditableConfig(
static::DEFAULT_MAX_CHILDREN,
static::DEFAULT_MAX_DEPTH,
static::DEFAULT_MAX_STRING_LENGTH,
static::DEFAULT_MAX_LINE_LENGTH,
static::DEFAULT_INDENT
);
$this->references = $references = new Stack();
$this->depth = new IntValue();

$this->container = $container = new Container($config, $this);

$this->subdumpers = array(
new StringDumper($this->config),
new StringDumper($container),
new NullDumper(),
new ScalarDumper(),
new ObjectRecursiveDumper($references),
new ObjectTooDepthArrayDumper($this->depth, $this->config),
new ObjectDebugInfoDumper($this, $references, $this->depth, $this->config),
new ObjectBigDumper($this, $references, $this->depth, $this->config),
new ArrayRecursiveDumper($references),
new ArrayTooDepthDumper($this->depth, $this->config),
new ArraySimpleViewDumper($this, $this->config, $this->depth),
new ArraySingleStringDumper($this, $this->config),
new ArrayBigDumper($this, $references, $this->depth, $this->config),
new ObjectRecursiveDumper($container),
new ObjectTooDepthDumper($container),
new ObjectDebugInfoDumper($container),
new ObjectBigDumper($container),
new ArrayRecursiveDumper($container),
new ArrayTooDepthDumper($container),
new ArraySimpleViewDumper($container),
new ArraySingleStringDumper($container),
new ArrayBigDumper($container),
new ResourceDumper(),
);
}

public function dump($var)
{
if ($this->displayPlaceInCode && 0 === $this->depth->getValue()) {
if ($this->displayPlaceInCode && 0 === $this->container->getDepth()->getValue()) {
$this->dumpPlaceInCode(0);
}

$printNl = $this->container->getPrintNlOnEnd()->getValue();
$this->container->getPrintNlOnEnd()->setValue(true);
$return = false;

foreach ($this->subdumpers as $subdumper) {
if ($subdumper->supports($var)) {
$this->depth->incr();
$this->container->getDepth()->incr();
try {
$subdumper->dump($var);
$this->depth->decr();
if ($printNl) {
echo "\n";
}
$this->container->getDepth()->decr();
$return = true;
} catch (VarNotSupportedException $exception) {
$this->depth->decr();
$this->container->getDepth()->decr();
continue;
}

return;
break;
}
}

// @codeCoverageIgnoreStart
$this->container->getPrintNlOnEnd()->setValue($printNl);

if ($return) {
return;
}

// Theoretically the following lines are unnecessary
$prev = $this->displayPlaceInCode;
$this->displayPlaceInCode = false;
parent::dump($var);
$this->displayPlaceInCode = $prev;

return;
// @codeCoverageIgnoreEnd
if ($this->container->getPrintNlOnEnd()->getValue()) {
parent::dump($var);
} else {
\ob_start();
parent::dump($var);
$result = \ob_get_contents();
\ob_end_clean();

echo \mb_substr($result, 0, -1);
}

$this->displayPlaceInCode = $prev;
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Subdumpers/AbstractDumper.php
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the awesomite/var-dumper package.
*
* (c) Bartłomiej Krukowski <bartlomiej@krukowski.me>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Awesomite\VarDumper\Subdumpers;

use Awesomite\VarDumper\Helpers\Container;

/**
* @internal
*/
abstract class AbstractDumper implements SubdumperInterface
{
protected $container;

public function __construct(Container $container)
{
$this->container = $container;
}
}
44 changes: 0 additions & 44 deletions src/Subdumpers/AbstractObjectBigDumper.php

This file was deleted.

0 comments on commit 70c11d1

Please sign in to comment.