Revision 6165,
1.2 KB
checked in by niltonneto, 12 years ago
(diff) |
Ticket #2507 - Refatorado e centralizado tratamento de Erros.
|
Rev | Line | |
---|
[6150] | 1 | <?php |
---|
| 2 | |
---|
| 3 | final class Errors { |
---|
| 4 | |
---|
[6165] | 5 | private static $sInstance; |
---|
| 6 | private $errors; |
---|
[6150] | 7 | |
---|
[6165] | 8 | public static function getInstance(){ |
---|
| 9 | if (!self::$sInstance) { |
---|
| 10 | self::$sInstance = new Errors(); |
---|
| 11 | } |
---|
| 12 | return self::$sInstance; |
---|
| 13 | } |
---|
[6150] | 14 | |
---|
[6165] | 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 | } |
---|
[6150] | 29 | |
---|
[6165] | 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 | } |
---|
[6150] | 39 | |
---|
[6165] | 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 | } |
---|
[6150] | 46 | |
---|
[6165] | 47 | throw new ResponseException($error['message'], $error['code']); |
---|
| 48 | } |
---|
[6150] | 49 | |
---|
[6165] | 50 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.