1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of plumphp/plum-console. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Eckerstorfer <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Plum\PlumConsole; |
13
|
|
|
|
14
|
|
|
use Plum\Plum\Result; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Throwable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* ExceptionFormatter. |
20
|
|
|
* |
21
|
|
|
* @author Florian Eckerstorfer |
22
|
|
|
* @copyright 2015 Florian Eckerstorfer |
23
|
|
|
*/ |
24
|
|
|
class ExceptionFormatter |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var OutputInterface |
28
|
|
|
*/ |
29
|
|
|
private $output; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $options = [ |
35
|
|
|
'minMessageVerbosity' => OutputInterface::VERBOSITY_VERBOSE, |
36
|
|
|
'minTraceVerbosity' => OutputInterface::VERBOSITY_VERY_VERBOSE, |
37
|
|
|
'messageTemplate' => '<error>%s</error>', |
38
|
|
|
'traceTemplate' => '%s', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param OutputInterface $output |
43
|
|
|
* @param array $options |
44
|
|
|
* |
45
|
|
|
* @codeCoverageIgnore |
46
|
|
|
*/ |
47
|
|
|
public function __construct(OutputInterface $output, array $options = []) |
48
|
|
|
{ |
49
|
|
|
$this->output = $output; |
50
|
|
|
$this->options = array_merge($this->options, $options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Result $result |
55
|
|
|
*/ |
56
|
2 |
|
public function outputExceptions(Result $result) |
57
|
|
|
{ |
58
|
2 |
|
foreach ($result->getExceptions() as $exception) { |
59
|
2 |
|
$this->outputExceptionMessage($exception) |
60
|
2 |
|
->outputExceptionTrace($exception); |
61
|
2 |
|
} |
62
|
|
|
|
63
|
2 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Throwable $exception |
68
|
|
|
*/ |
69
|
1 |
|
protected function outputExceptionMessage(Throwable $exception) |
70
|
|
|
{ |
71
|
1 |
|
if ($this->output->getVerbosity() >= $this->options['minMessageVerbosity']) { |
72
|
1 |
|
$this->output->writeln(sprintf('<error>%s</error>', $exception->getMessage())); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Throwable $exception |
80
|
|
|
*/ |
81
|
1 |
|
protected function outputExceptionTrace(Throwable $exception) |
82
|
|
|
{ |
83
|
1 |
|
if ($this->output->getVerbosity() >= $this->options['minTraceVerbosity']) { |
84
|
1 |
|
$this->output->writeln(sprintf($this->options['traceTemplate'], $exception->getTraceAsString())); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|