1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Exception; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Arch\Controller; |
8
|
|
|
use Ffcms\Core\Helper\Type\Str; |
9
|
|
|
|
10
|
|
|
abstract class TemplateException extends \Exception |
11
|
|
|
{ |
12
|
|
|
public $status = 404; |
13
|
|
|
public $title = '404 Not Found'; |
14
|
|
|
public $text = 'An unexpected error occurred: %e%'; |
15
|
|
|
public $tpl = 'default'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Display exception template |
19
|
|
|
* @throws JsonException |
20
|
|
|
* @throws \Exception |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
|
|
public function display() |
24
|
|
|
{ |
25
|
|
|
// hide path root and stip tags from exception message |
26
|
|
|
$msg = Str::replace(root, '$DOCUMENT_ROOT', $this->getMessage()); |
27
|
|
|
$msg = App::$Security->strip_tags($msg); |
28
|
|
|
// get message translation |
29
|
|
|
$this->text = App::$Translate->get('Default', $this->text, ['e' => $msg]); |
30
|
|
|
|
31
|
|
|
// if exception is throwed not from html-based environment |
32
|
|
|
if (env_type !== 'html') { |
33
|
|
|
if (env_type === 'json') { |
34
|
|
|
return (new JsonException($msg))->display(); |
35
|
|
|
} else { |
36
|
|
|
return $this->text; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// add notification in debug bar if exist |
41
|
|
|
if (App::$Debug !== null) { |
42
|
|
|
App::$Debug->addException($this); |
43
|
|
|
} |
44
|
|
|
// return rendered result |
45
|
|
|
return $this->buildFakePage(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Build fake page with error based on fake controller initiation |
50
|
|
|
*/ |
51
|
|
|
protected function buildFakePage() |
52
|
|
|
{ |
53
|
|
|
// initialize fake controller to display page with exception |
54
|
|
|
$fakeController = new Controller(); |
55
|
|
|
// check if used no layout template |
56
|
|
|
if (defined('env_no_layout') && env_no_layout === true) { |
57
|
|
|
$fakeController->layout = null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// add global title tag value |
61
|
|
|
$fakeController->setGlobalVar('title', App::$Translate->get('Default', $this->title)); |
62
|
|
|
// build error text |
63
|
|
|
$rawResponse = 'error'; |
64
|
|
|
try { |
65
|
|
|
$rawResponse = App::$View->render('native/errors/' . $this->tpl, ['msg' => $this->text]); |
66
|
|
|
if (Str::likeEmpty($rawResponse)) { |
|
|
|
|
67
|
|
|
$rawResponse = $this->text; |
68
|
|
|
} |
69
|
|
|
} catch (SyntaxException $e) { |
70
|
|
|
$rawResponse = $this->text; |
71
|
|
|
} |
72
|
|
|
// set controller body response |
73
|
|
|
$fakeController->setResponse($rawResponse); |
74
|
|
|
// set status code for header |
75
|
|
|
App::$Response->setStatusCode((int)$this->status); |
76
|
|
|
// return compiled html output |
77
|
|
|
return $fakeController->getOutput(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.