Skip to content

Commit 21205ad

Browse files
committed
Add exception printing method
1 parent bddd9a7 commit 21205ad

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Output/NullOutput.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
namespace PhpSchool\PhpWorkshop\Output;
66

7+
use Throwable;
8+
79
class NullOutput implements OutputInterface
810
{
911
public function printError(string $error): void
1012
{
1113
// noop
1214
}
1315

16+
public function printException(Throwable $exception): void
17+
{
18+
// noop
19+
}
20+
1421
public function write(string $content): void
1522
{
1623
// noop

src/Output/OutputInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PhpSchool\PhpWorkshop\Output;
66

7+
use Throwable;
8+
79
/**
810
* Output interface
911
*/
@@ -17,6 +19,14 @@ interface OutputInterface
1719
*/
1820
public function printError(string $error): void;
1921

22+
/**
23+
* Write an Exception. Should be decorated in someway
24+
* which highlights the severity.
25+
*
26+
* @param Throwable $exception
27+
*/
28+
public function printException(Throwable $exception): void;
29+
2030
/**
2131
* Write a string to the output.
2232
*

src/Output/StdOutput.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Colors\Color;
88
use PhpSchool\Terminal\Terminal;
9+
use Throwable;
910

1011
/**
1112
* Console output interface
@@ -22,10 +23,16 @@ class StdOutput implements OutputInterface
2223
*/
2324
private $terminal;
2425

25-
public function __construct(Color $color, Terminal $terminal)
26+
/**
27+
* @var string
28+
*/
29+
private $workshopBasePath;
30+
31+
public function __construct(Color $color, Terminal $terminal, string $workshopBasePath = '')
2632
{
2733
$this->color = $color;
2834
$this->terminal = $terminal;
35+
$this->workshopBasePath = $workshopBasePath;
2936
}
3037

3138
/**
@@ -41,6 +48,43 @@ public function printError(string $error): void
4148
echo "\n";
4249
}
4350

51+
/**
52+
* @param Throwable $exception
53+
*/
54+
public function printException(Throwable $exception): void
55+
{
56+
$message = $exception->getMessage();
57+
if (strpos($message, $this->workshopBasePath) !== null) {
58+
$message = str_replace($this->workshopBasePath, '', $message);
59+
}
60+
61+
$file = $exception->getFile();
62+
if (strpos($file, $this->workshopBasePath) !== null) {
63+
$file = str_replace($this->workshopBasePath, '', $file);
64+
}
65+
66+
$lines = [
67+
sprintf("In %s line %d:", $file, $exception->getLine()),
68+
sprintf("[%s (%s)]:", get_class($exception), $exception->getCode()),
69+
'',
70+
$message
71+
];
72+
73+
$length = max(array_map('strlen', $lines)) + 2;
74+
$this->emptyLine();
75+
$this->writeLine(' ' . $this->color->__invoke(str_repeat(' ', $length))->bg_red());
76+
77+
foreach ($lines as $line) {
78+
$line = str_pad($line, $length - 2, ' ', STR_PAD_RIGHT);
79+
$this->writeLine(' ' . $this->color->__invoke(" $line ")->bg_red()->white()->bold());
80+
}
81+
82+
$this->writeLine(' ' . $this->color->__invoke(str_repeat(' ', $length))->bg_red());
83+
$this->emptyLine();
84+
85+
$this->writeLine(implode("\n", array_map(fn ($l) => " $l", explode("\n", $exception->getTraceAsString()))));
86+
}
87+
4488
/**
4589
* Write a title section. Should be decorated in a way which makes
4690
* the title stand out.

0 commit comments

Comments
 (0)