Revision 6165,
1.2 KB
checked in by niltonneto, 12 years ago
(diff) |
Ticket #2507 - Refatorado e centralizado tratamento de Erros.
|
Line | |
---|
1 | <?php |
---|
2 | |
---|
3 | final class Errors { |
---|
4 | |
---|
5 | private static $sInstance; |
---|
6 | private $errors; |
---|
7 | |
---|
8 | public static function getInstance(){ |
---|
9 | if (!self::$sInstance) { |
---|
10 | self::$sInstance = new Errors(); |
---|
11 | } |
---|
12 | return self::$sInstance; |
---|
13 | } |
---|
14 | |
---|
15 | function __construct(){ |
---|
16 | $this->errors = array(); |
---|
17 | if($handle = fopen(__DIR__."/../../config/Errors.tsv", "r")){ |
---|
18 | while (!feof($handle)) { |
---|
19 | $line = trim(fgets($handle,1024)); |
---|
20 | if($line == null || $line[0] == "#") |
---|
21 | continue; |
---|
22 | $error = preg_split("/[\t]+/", $line); |
---|
23 | if(is_array($error)&& count($error) == 3) { |
---|
24 | $this->errors[] = array("code" => $error[0], "key" => $error[1], "message" => $error[2]); |
---|
25 | } |
---|
26 | } |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | static public function runException($needle, $argument = false){ |
---|
31 | $error = false; |
---|
32 | $type = (is_int($needle) ? "code" : "key"); |
---|
33 | foreach( Errors::getInstance()->errors as $value ){ |
---|
34 | if($value[$type] == $needle){ |
---|
35 | $error = $value; |
---|
36 | break; |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | if($error['message'] && $argument){ |
---|
41 | $error['message'] = str_replace("%1", $argument, $error['message']); |
---|
42 | } |
---|
43 | if(!$error){ |
---|
44 | Errors::getInstance()->runException("E_UNKNOWN_ERROR"); |
---|
45 | } |
---|
46 | |
---|
47 | throw new ResponseException($error['message'], $error['code']); |
---|
48 | } |
---|
49 | |
---|
50 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.