forked from squareboat/sneaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExceptionHandler.php
103 lines (88 loc) · 2.55 KB
/
ExceptionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace SquareBoat\Sneaker;
use Illuminate\View\Factory;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
class ExceptionHandler
{
/**
* The view factory implementation.
*
* @var \Illuminate\View\Factory
*/
private $view;
/**
* Create a new exception handler instance.
*
* @param \Illuminate\View\Factory $view
* @return void
*/
public function __construct(Factory $view)
{
$this->view = $view;
}
/**
* Create a string for the given exception.
*
* @param \Exception $exception
* @return string
*/
public function convertExceptionToString($exception)
{
return $this->view->make('sneaker::email.subject', compact('exception'))->render();
}
/**
* Create a html for the given exception.
*
* @param \Exception $exception
* @return string
*/
public function convertExceptionToHtml($exception)
{
$flat = $this->getFlattenedException($exception);
$handler = new SymfonyExceptionHandler();
return $this->decorate($handler->getContent($flat), $handler->getStylesheet($flat), $flat);
}
/**
* Converts the Exception in a PHP Exception to be able to serialize it.
*
* @param \Exception $exception
* @return \Symfony\Component\Debug\Exception\FlattenException
*/
private function getFlattenedException($exception)
{
if (!$exception instanceof FlattenException) {
$exception = FlattenException::createFromThrowable($exception);
}
return $exception;
}
/**
* Get the html response content.
*
* @param string $content
* @param string $css
* @return string
*/
private function decorate($content, $css, $exception)
{
$content = $this->removeTitle($content);
return $this->view->make('sneaker::email.body', compact('content', 'css', 'exception'))->render();
}
/**
* Removes title from content as it is same for all exceptions and has no real value.
*
* @param string $content
* @return string
*/
private function removeTitle($content)
{
$titles = [
'Whoops, looks like something went wrong.',
'Sorry, the page you are looking for could not be found.',
];
foreach ($titles as $title) {
$content = str_replace("<h1>{$title}</h1>", '', $content);
}
return $content;
}
}