1 <?php
2
3 4 5 6 7 8 9 10 11 12
13
14 namespace Slimore\Middleware;
15
16 17 18 19 20 21
22
23 class Exceptions extends \Slim\Middleware
24 {
25 26 27
28 protected $settings;
29
30 31 32 33
34
35 public function __construct($settings = [])
36 {
37 $this->settings = $settings;
38 }
39
40 41 42
43
44 public function call()
45 {
46 try
47 {
48 $this->next->call();
49 }
50 catch (\Exception $e)
51 {
52 $log = $this->app->getLog();
53 $env = $this->app->environment();
54 $env['slim.log'] = $log;
55 $env['slim.log']->error($e);
56 $this->app->contentType('text/html');
57 $this->app->response()->status(500);
58 $this->app->response()->body($this->renderBody($env, $e));
59 }
60 }
61
62 63 64 65 66 67 68
69
70 public function renderBody(&$env, $exception)
71 {
72 $title = 'Slimore Application ErrorException';
73 $code = $exception->getCode();
74 $message = $exception->getMessage();
75 $file = $exception->getFile();
76 $line = $exception->getLine();
77 $type = get_class($exception);
78 $trace = str_replace(array('#', "\n"), array('<div class="trace-line">#', '</div>'), $exception->getTraceAsString());
79
80 $html = html($title, [
81 '<meta http-equiv="X-UA-Compatible" content="IE=edge" />',
82 '<meta name="renderer" content="webkit" />',
83 '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />',
84 style([
85 '*{margin:0;padding:0;}',
86 'html {font-size: 62.5%;}',
87 'html, body {max-width: 100%;width: 100%;}',
88 'body {font-size: 1.4rem;font-family: "Microsoft Yahei", Helvetica, Tahoma, STXihei, arial,verdana,sans-serif;background:#fff;color:#555;}',
89 'img {border: none;}',
90 'pre, pre code {font-family:Consolas, arial,verdana,sans-serif;}',
91 '#layout {padding: 6rem;}',
92 '#main {margin: 0 auto;line-height: 1.5;}',
93 '#main > h1 {font-size: 10rem;margin-bottom: 1rem;}',
94 '#main > h3 {margin-bottom: 2rem; font-size: 1.8rem;}',
95 '#main > h4 {margin-bottom: 1rem; font-size: 1.8rem;}',
96 '#main pre {margin: 1.5rem 0;white-space: pre-wrap;word-wrap: break-word;}',
97 '#main > p {white-space: pre-wrap;word-wrap: break-word;line-height: 1.3;margin-bottom: 0.6rem;}',
98 '#main > p > strong {width: 7rem;display:inline-block;}',
99 '.logo {text-align: left;border-top:1px solid #eee;margin-top: 5rem;padding: 3rem 0 0;color: #ccc;}',
100 '.logo > h1 {font-weight: normal;font-size: 5rem;}',
101 '.logo img {margin-left: -2rem;}',
102 '.trace-line {padding: 0.3rem 0.6rem;margin-left:-0.6rem;-webkit-transition: background-color 300ms ease-out;transition: background-color 300ms ease-out;}',
103 '.trace-line:hover {background:#fffccc;}'
104 ])
105 ], [
106 '<div id="layout">',
107 '<div id="main">',
108 '<h1>:(</h1>',
109 '<h3>' . $type . '</h3>',
110 '<h4>Details :</h4>',
111 '<p><strong>Type: </strong> ' . $type . '</p>',
112 '<p><strong>Message: </strong> ' . $message . '</p>',
113 '<p><strong>File: </strong> ' . $file . '</p>',
114 '<p><strong>Line: </strong> ' . $line . '</p>',
115 '<br/>',
116 '<h4>Trace:</h4>',
117 '<pre>' . $trace . '</pre>',
118 '<div class="logo">',
119 '<h1>Slimore</h1>',
120 '<p>The fully (H)MVC framework, based on the Slim PHP Framwork.</p>',
121 '</div>',
122 '</div>',
123 '</div>'
124 ]);
125
126 return $html;
127 }
128 }