1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Exception classes file |
4
|
|
|
* |
5
|
|
|
* @author Qiang Xue <[email protected]> |
6
|
|
|
* @link https://github.com/pradosoft/prado |
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
8
|
|
|
* @package Prado\Exceptions |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Prado\Exceptions; |
12
|
|
|
|
13
|
|
|
use Prado\Prado; |
14
|
|
|
use Prado\TPropertyValue; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* TException class |
18
|
|
|
* |
19
|
|
|
* TException is the base class for all PRADO exceptions. |
20
|
|
|
* |
21
|
|
|
* TException provides the functionality of translating an error code |
22
|
|
|
* into a descriptive error message in a language that is preferred |
23
|
|
|
* by user browser. Additional parameters may be passed together with |
24
|
|
|
* the error code so that the translated message contains more detailed |
25
|
|
|
* information. |
26
|
|
|
* |
27
|
|
|
* By default, TException looks for a message file by calling |
28
|
|
|
* {@link getErrorMessageFile()} method, which uses the "message-xx.txt" |
29
|
|
|
* file located under "Prado\Exceptions" folder, where "xx" is the |
30
|
|
|
* code of the user preferred language. If such a file is not found, |
31
|
|
|
* "message.txt" will be used instead. |
32
|
|
|
* |
33
|
|
|
* @author Qiang Xue <[email protected]> |
34
|
|
|
* @package Prado\Exceptions |
35
|
|
|
* @since 3.0 |
36
|
|
|
*/ |
37
|
|
|
class TException extends \Exception |
38
|
|
|
{ |
39
|
|
|
private $_errorCode = ''; |
40
|
|
|
private static $_messagefiles = []; |
41
|
|
|
protected static $_messageCache = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* @param string $errorMessage error message. This can be a string that is listed |
46
|
|
|
* in the message file. If so, the message in the preferred language |
47
|
|
|
* will be used as the error message. Any rest parameters will be used |
48
|
|
|
* to replace placeholders ({0}, {1}, {2}, etc.) in the message. |
49
|
113 |
|
*/ |
50
|
|
|
public function __construct($errorMessage) |
51
|
113 |
|
{ |
52
|
113 |
|
$this->_errorCode = $errorMessage; |
53
|
113 |
|
$errorMessage = $this->translateErrorMessage($errorMessage); |
54
|
113 |
|
$args = func_get_args(); |
55
|
113 |
|
array_shift($args); |
56
|
113 |
|
$n = count($args); |
57
|
113 |
|
$tokens = []; |
58
|
71 |
|
for ($i = 0; $i < $n; ++$i) { |
59
|
|
|
$tokens['{' . $i . '}'] = TPropertyValue::ensureString($args[$i]); |
60
|
113 |
|
} |
61
|
113 |
|
parent::__construct(strtr($errorMessage, $tokens)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Adds to the various files to read when rendering an error |
66
|
|
|
* @param string $file the extra message file |
67
|
|
|
* @since 4.2.0 |
68
|
113 |
|
*/ |
69
|
|
|
public static function addMessageFile($file) |
70
|
113 |
|
{ |
71
|
|
|
if (preg_match('/^(.*)(-.{2.4})?\.(.{2,4})$/', $file, $matching)) { |
72
|
|
|
$lang = Prado::getPreferredLanguage(); |
73
|
113 |
|
$msgFile = $matching[1] . '-' . $lang . '.' . $matching[2]; |
74
|
2 |
|
if (is_file($msgFile)) { |
75
|
2 |
|
$file = $msgFile; |
76
|
2 |
|
} |
77
|
2 |
|
} |
78
|
|
|
TException::$_messagefiles[] = $file; |
79
|
|
|
} |
80
|
|
|
|
81
|
113 |
|
/** |
82
|
|
|
* Translates an error code into an error message. |
83
|
|
|
* @param string $key error code that is passed in the exception constructor. |
84
|
|
|
* @return string the translated error message |
85
|
|
|
*/ |
86
|
|
|
protected function translateErrorMessage($key) |
87
|
108 |
|
{ |
88
|
|
|
$msgFiles = TException::$_messagefiles; |
89
|
108 |
|
$msgFiles[] = $this->getErrorMessageFile(); |
90
|
108 |
|
$value = $key; |
91
|
108 |
|
|
92
|
107 |
|
// Cache messages |
93
|
|
|
foreach ($msgFiles as $msgFile) { |
94
|
108 |
|
if (!isset(self::$_messageCache[$msgFile])) { |
95
|
|
|
if (($entries = @file($msgFile)) !== false) { |
96
|
|
|
foreach ($entries as $entry) { |
97
|
|
|
[$code, $message] = array_merge(explode('=', $entry, 2), ['']); |
98
|
|
|
self::$_messageCache[$msgFile][trim($code)] = trim($message); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
$value = self::$_messageCache[$msgFile][$key] ?? $value; |
103
|
|
|
} |
104
|
|
|
return $value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
5 |
|
* @return string path to the error message file |
109
|
|
|
*/ |
110
|
5 |
|
protected function getErrorMessageFile() |
111
|
5 |
|
{ |
112
|
|
|
$lang = Prado::getPreferredLanguage(); |
113
|
|
|
$msgFile = Prado::getFrameworkPath() . '/Exceptions/messages/messages-' . $lang . '.txt'; |
114
|
|
|
if (!is_file($msgFile)) { |
115
|
|
|
$msgFile = Prado::getFrameworkPath() . '/Exceptions/messages/messages.txt'; |
116
|
|
|
} |
117
|
|
|
return $msgFile; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string error code |
122
|
|
|
*/ |
123
|
|
|
public function getErrorCode() |
124
|
|
|
{ |
125
|
|
|
return $this->_errorCode; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $code error code |
130
|
|
|
*/ |
131
|
|
|
public function setErrorCode($code) |
132
|
|
|
{ |
133
|
|
|
$this->_errorCode = $code; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string error message |
138
|
|
|
*/ |
139
|
|
|
public function getErrorMessage() |
140
|
|
|
{ |
141
|
|
|
return $this->getMessage(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $message error message |
146
|
|
|
*/ |
147
|
|
|
protected function setErrorMessage($message) |
148
|
|
|
{ |
149
|
|
|
$this->message = $message; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|