Skip to content

Commit

Permalink
🤦
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 23, 2020
1 parent c367c74 commit e9a4e55
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 37 deletions.
24 changes: 17 additions & 7 deletions Chevereto-Chevere/Chevere/Components/VarDump/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,32 @@
namespace Chevere\Components\VarDump;

use Chevere\Components\App\Instances\BootstrapInstance;
use Chevere\Components\Common\Interfaces\ToStringInterface;
use Chevere\Components\VarDump\Dumpers\ConsoleDumper;
use Chevere\Components\VarDump\Dumpers\HtmlDumper;

/**
* Context-aware dumper.
*/
final class Dumper
final class Dumper implements ToStringInterface
{
private string $dumped;

public function __construct(...$vars)
{
$dumped =
(BootstrapInstance::get()->isCli() ? new ConsoleDumper() : new HtmlDumper())
->withVars(...$vars)
->outputter()
->toString();
$dumper = BootstrapInstance::get()->isCli() ? new ConsoleDumper() : new HtmlDumper();
$this->dumped = $dumper
->withVars(...$vars)
->toString();
}

public function toString(): string
{
return $this->dumped;
}

screen()->runtime()->attachNl($dumped)->display();
public function toScreen(): void
{
screen()->runtime()->attachNl($this->dumped)->display();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ final public function withVars(...$vars): DumperInterface
return $new;
}
$new->setDebugBacktrace();
$new->outputter = $new->outputter
$new->output = $new->outputter
->withDumper($new)
->process();
->toString();

return $new;
}
Expand All @@ -94,6 +94,14 @@ final public function vars(): array
return $this->vars;
}

/**
* {@inheritdoc}
*/
final public function toString(): string
{
return $this->output;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -122,9 +130,5 @@ final private function setDebugBacktrace(): void
array_shift($this->debugBacktrace);
array_shift($this->debugBacktrace);
}
// foreach ($this->debugBacktrace as $pos => $item) {
// echo 'POS ' . $pos . ' ' . $item['file'] . ' -C ' . ($item['class'] ?? 'null') . ' -F ' . ($item['function'] ?? 'null') . "\n";
// }
// die();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public function withVars(...$vars): DumperInterface;
*/
public function vars(): array;

public function toString(): string;

/**
* Provides access to the debug backtrace. Can be called only after calling dump.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ public function dumper(): DumperInterface;

public function prepare(string $output): string;

public function process(): OutputterInterface;

public function toString(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,30 @@ final public function dumper(): DumperInterface
/**
* {@inheritdoc}
*/
final public function process(): OutputterInterface
final public function toString(): string
{
$this->output = $this->prepare($this->output);
$this->handleClass();
$this->output .= $this->dumper->formatter()
->applyWrap('_function', $this->dumper->debugBacktrace()[$this->dumper::OFFSET]['function'] . '()');
->applyWrap('_function', $this->dumper->debugBacktrace()[DumperInterface::OFFSET]['function'] . '()');

$this->handleFile();
$this->output .= "\n\n";
$this->handleArgs();
$this->output = trim($this->output);
$this->output = $this->callback($this->output);

return $this;
}

/**
* {@inheritdoc}
*/
final public function toString(): string
{
return $this->output;
return $this->callback($this->output);
}

final private function handleClass(): void
{
if (isset($this->dumper->debugBacktrace()[$this->dumper::OFFSET]['class'])) {
$class = $this->dumper->debugBacktrace()[$this->dumper::OFFSET]['class'];
if (isset($this->dumper->debugBacktrace()[DumperInterface::OFFSET]['class'])) {
$class = $this->dumper->debugBacktrace()[DumperInterface::OFFSET]['class'];
if (stringStartsWith('class@anonymous', $class)) {
$class = explode('0x', $class)[0];
}
$this->output .= $this->dumper->formatter()
->applyWrap('_class', $class) . $this->dumper->debugBacktrace()[$this->dumper::OFFSET]['type'];
->applyWrap('_class', $class) . $this->dumper->debugBacktrace()[DumperInterface::OFFSET]['type'];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Chevere\Components\VarDump\Outputters;

use Chevere\Components\VarDump\Interfaces\DumperInterface;

final class HtmlOutputter extends AbstractOutputter
{
/**
Expand All @@ -21,9 +23,9 @@ final class HtmlOutputter extends AbstractOutputter
public function prepare(string $output): string
{
if (false === headers_sent()) {
$output .= '<html style="background: ' . $this->dumper::BACKGROUND_SHADE . ';"><head></head><body>';
$output .= '<html style="background: ' . DumperInterface::BACKGROUND_SHADE . ';"><head></head><body>';
}
$output .= '<pre style="' . $this->dumper::STYLE . '">';
$output .= '<pre style="' . DumperInterface::STYLE . '">';

return $output;
}
Expand Down
26 changes: 21 additions & 5 deletions Chevereto-Chevere/resources/functions/dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,37 @@

use Chevere\Components\VarDump\Dumper;

if (!function_exists('xdump') && !function_exists('xdd')) {
if (!function_exists('varInfo')) {
/**
* Dumps information about one or more variables.
* Returns dump information about one or more variables.
*/
function varInfo(...$vars)
{
return
(new Dumper(...$vars))
->tostring();
}
}

if (!function_exists('xdump')) {
/**
* Dumps information about one or more variables to the screen.
*/
function xdump(...$vars)
{
new Dumper(...$vars);
(new Dumper(...$vars))
->toScreen();
}
}

if (!function_exists('xdd')) {
/**
* Dumps information about one or more variables and die().
* Dumps information about one or more variables to the screen and die().
*/
function xdd(...$vars)
{
new Dumper(...$vars);
(new Dumper(...$vars))
->toScreen();
die(0);
}
}

0 comments on commit e9a4e55

Please sign in to comment.