1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* Pages extension for the phpBB Forum Software package. |
5
|
|
|
* |
6
|
|
|
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com> |
7
|
|
|
* @license GNU General Public License, version 2 (GPL-2.0) |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace blitze\sitemaker\exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base exception |
15
|
|
|
*/ |
16
|
|
|
class base extends \Exception |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Null if the message is a string, array if the message was submitted as an array |
20
|
|
|
* @var string|array |
21
|
|
|
*/ |
22
|
|
|
protected $message_full; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Send a string to translate all of the portions with the parent message |
26
|
|
|
* (typically used to format a string with the given message portions). |
27
|
|
|
* Null to ignore. Default: Null |
28
|
|
|
* @var string|null |
29
|
|
|
*/ |
30
|
|
|
protected $parent_message = null; |
31
|
|
|
|
32
|
|
|
protected $previous; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor |
36
|
|
|
* |
37
|
|
|
* Different from normal exceptions in that we do not enforce $message to be a string. |
38
|
|
|
* |
39
|
|
|
* @param string|array $message |
40
|
|
|
* @param int $code |
41
|
|
|
* @param \Exception $previous |
42
|
|
|
* @access public |
43
|
|
|
*/ |
44
|
32 |
|
public function __construct($message = null, $code = 0, \Exception $previous = null) |
45
|
|
|
{ |
46
|
|
|
// We're slightly changing the way exceptions work |
47
|
|
|
// Tools, such as xdebug, expect the message to be a string, so to prevent errors |
48
|
|
|
// with those tools, we store our full message in message_full and only a string in message |
49
|
32 |
|
if (is_array($message)) |
50
|
32 |
|
{ |
51
|
20 |
|
$this->message = (string) $message[0]; |
52
|
20 |
|
} |
53
|
|
|
else |
54
|
|
|
{ |
55
|
12 |
|
$this->message = $message; |
56
|
|
|
} |
57
|
32 |
|
$this->message_full = $message; |
58
|
|
|
|
59
|
32 |
|
$this->code = $code; |
60
|
32 |
|
$this->previous = $previous; |
61
|
32 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Translate this exception |
65
|
|
|
* |
66
|
|
|
* @param \phpbb\language\language $translator |
67
|
|
|
* @return array|string |
68
|
|
|
* @access public |
69
|
|
|
*/ |
70
|
32 |
|
public function get_message(\phpbb\language\language $translator) |
71
|
|
|
{ |
72
|
32 |
|
return $this->translate_portions($translator); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Translate all portions of the message sent to the exception |
77
|
|
|
* |
78
|
|
|
* Goes through each element of the array and tries to translate them |
79
|
|
|
* |
80
|
|
|
* @param \phpbb\language\language $translator |
81
|
|
|
* @return array|string Array if $parent_message === null else a string |
82
|
|
|
* @access protected |
83
|
|
|
*/ |
84
|
32 |
|
protected function translate_portions(\phpbb\language\language $translator) |
85
|
|
|
{ |
86
|
|
|
// Make sure our language file has been loaded |
87
|
32 |
|
$this->add_lang($translator); |
88
|
|
|
|
89
|
|
|
// Ensure we have an array |
90
|
32 |
|
if (!is_array($this->message_full)) |
91
|
32 |
|
{ |
92
|
12 |
|
$this->message_full = array($this->message_full); |
93
|
12 |
|
} |
94
|
|
|
|
95
|
|
|
// Translate each message portion |
96
|
32 |
|
foreach ($this->message_full as &$message) |
97
|
|
|
{ |
98
|
|
|
// Attempt to translate each portion |
99
|
32 |
|
$translated_message = $translator->lang('EXCEPTION_' . $message); |
100
|
|
|
|
101
|
|
|
// Check if translating did anything |
102
|
32 |
|
if ($translated_message !== 'EXCEPTION_' . $message) |
103
|
32 |
|
{ |
104
|
|
|
// It did, so replace message with the translated version |
105
|
|
|
$message = $translated_message; |
106
|
|
|
} |
107
|
32 |
|
} |
108
|
|
|
// Always unset a variable passed by reference in a foreach loop |
109
|
32 |
|
unset($message); |
110
|
|
|
|
111
|
32 |
|
if ($this->parent_message !== null) |
112
|
32 |
|
{ |
113
|
|
|
// Prepend the parent message to the message portions |
114
|
32 |
|
array_unshift($this->message_full, (string) $this->parent_message); |
115
|
|
|
|
116
|
|
|
// We return a string |
117
|
32 |
|
return call_user_func_array(array($translator, 'lang'), $this->message_full); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// We return an array |
121
|
|
|
return $this->message_full; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add our language file |
126
|
|
|
* |
127
|
|
|
* @param \phpbb\language\language $translator |
128
|
|
|
* @return null |
129
|
|
|
* @access public |
130
|
|
|
*/ |
131
|
32 |
|
public function add_lang(\phpbb\language\language $translator) |
132
|
|
|
{ |
133
|
32 |
|
static $is_loaded = false; |
134
|
|
|
|
135
|
|
|
// We only need to load the language file once |
136
|
|
|
if ($is_loaded) |
137
|
32 |
|
{ |
138
|
29 |
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Add our language file |
142
|
3 |
|
$translator->add_lang('exceptions', 'blitze/sitemaker'); |
143
|
|
|
|
144
|
|
|
// So the language file is only loaded once |
145
|
3 |
|
$is_loaded = true; |
146
|
3 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Output a string of this error message |
150
|
|
|
* |
151
|
|
|
* This will hopefully be never called, always catch the expected exceptions |
152
|
|
|
* and call get_message to translate them into an error that a user canunderstand |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
* @access public |
156
|
|
|
*/ |
157
|
|
|
public function __toString() |
158
|
|
|
{ |
159
|
|
|
return (is_array($this->message_full)) ? var_export($this->message_full, true) : (string) $this->message_full; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|