Skip to content

Commit

Permalink
feat(output.helper): add print trace
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Aug 21, 2018
1 parent 7b5080e commit 92f41ba
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Helper/OutputHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Ahc\Cli\Helper;

use Ahc\Cli\Exception;
use Ahc\Cli\Input\Argument;
use Ahc\Cli\Input\Command;
use Ahc\Cli\Input\Option;
Expand Down Expand Up @@ -38,6 +39,72 @@ public function __construct(Writer $writer = null)
$this->writer = $writer ?? new Writer;
}

/**
* Print stack trace and error msg of an exception.
*
* @param \Throwable $e
*
* @return void
*/
public function printTrace(\Throwable $e)
{
$eClass = \get_class($e);

$this->writer->colors(
"{$eClass} <red>{$e->getMessage()}</end><eol/>" .
"(thrown in <yellow>{$e->getFile()}</end><white>:{$e->getLine()})</end>"
);

// Internal exception traces are not printed.
if ($e instanceof Exception) {
return;
}

$traceStr = '<eol/><eol/><bold>Stack Trace:</end><eol/><eol/>';

foreach ($e->getTrace() as $i => $trace) {
$trace += ['class' => '', 'type' => '', 'function' => '', 'file' => '', 'line' => '', 'args' => []];
$symbol = $trace['class'] . $trace['type'] . $trace['function'];
$args = $this->stringifyArgs($trace['args']);

$traceStr .= " <comment>$i)</end> <red>$symbol</end><comment>($args)</end>";
if ('' !== $trace['file']) {
$file = \realpath($trace['file']);
$traceStr .= "<eol/> <yellow>at $file</end><white>:{$trace['line']}</end><eol/>";
}
}

$this->writer->colors($traceStr);
}

protected function stringifyArgs(array $args)
{
$holder = [];

foreach ($args as $arg) {
$holder[] = $this->stringifyArg($arg);
}

return \implode(', ', $holder);
}

protected function stringifyArg($arg)
{
if (\is_scalar($arg)) {
return \var_export($arg, true);
}

if (\is_object($arg)) {
return \method_exists($arg, '__toString') ? (string) $arg : \get_class($arg);
}

if (\is_array($arg)) {
return '[' . $this->stringifyArgs($arg) . ']';
}

return \gettype($arg);
}

/**
* @param Argument[] $arguments
* @param string $header
Expand Down

0 comments on commit 92f41ba

Please sign in to comment.