1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of template |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Slick\Template\UserInterface; |
13
|
|
|
|
14
|
|
|
use Slick\ErrorHandler\Exception\ExceptionInspector; |
15
|
|
|
use Slick\ErrorHandler\Handler\HandlerInterface; |
16
|
|
|
use Slick\ErrorHandler\RunnerInterface; |
17
|
|
|
use Slick\Template\Engine\MarkdownLoader; |
18
|
|
|
use Slick\Template\Engine\TwigTemplateEngine; |
19
|
|
|
use Slick\Template\Extension\Slick; |
20
|
|
|
use Slick\Template\Extension\SlickApp; |
21
|
|
|
use Slick\Template\TemplateEngineInterface; |
22
|
|
|
use Throwable; |
23
|
|
|
use Twig\Environment; |
24
|
|
|
use Twig\Extension\DebugExtension; |
25
|
|
|
use Twig\Extra\Markdown\MarkdownExtension; |
26
|
|
|
use Twig\Loader\FilesystemLoader; |
27
|
|
|
use Twig\Extra\Markdown\DefaultMarkdown; |
28
|
|
|
use Twig\Extra\Markdown\MarkdownRuntime; |
29
|
|
|
use Twig\RuntimeLoader\RuntimeLoaderInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* PrettyErrorHandler |
33
|
|
|
* |
34
|
|
|
* @package Slick\Template\UserInterface |
35
|
|
|
*/ |
36
|
|
|
final class PrettyErrorHandler implements HandlerInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
public function __construct(private readonly TemplateEngineInterface $engine) |
40
|
|
|
{ |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function create(): static |
44
|
|
|
{ |
45
|
|
|
$path = dirname(dirname(__DIR__)) . '/templates'; |
46
|
|
|
$loader = new FilesystemLoader([$path]); |
47
|
|
|
$twig = new Environment($loader, ['cache' => false, 'debug' => true]); |
48
|
|
|
$twig->addExtension(new DebugExtension()); |
49
|
|
|
$twig->addExtension(new MarkdownExtension()); |
50
|
|
|
$twig->addRuntimeLoader(new MarkdownLoader()); |
51
|
|
|
$templateEngine = new TwigTemplateEngine($twig); |
52
|
|
|
(new Slick(new SlickApp()))->update($templateEngine); |
53
|
|
|
|
54
|
|
|
return new static($templateEngine); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function handle(Throwable $throwable, ExceptionInspector $inspector, RunnerInterface $runner): ?int |
58
|
|
|
{ |
59
|
|
|
$handler = $this; |
60
|
|
|
$runner->sendResponseCode($inspector->statusCode()); |
61
|
|
|
|
62
|
|
|
echo $this->engine |
63
|
|
|
->parse('errors/exception.twig') |
64
|
|
|
->process(compact('throwable', 'inspector', 'runner', 'handler')); |
65
|
|
|
return self::QUIT; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function functionCall(Throwable $throwable, int $key): string |
69
|
|
|
{ |
70
|
|
|
$lines = explode("#", $throwable->getTraceAsString()); |
71
|
|
|
$line = $lines[$key]; |
72
|
|
|
$parts = explode(":", $line); |
73
|
|
|
return trim(end($parts)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|