PageRenderTime 17ms CodeModel.GetById 7ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/error.class.php

https://bitbucket.org/jwetmore/diablo-iii-api-tests
PHP | 252 lines | 59 code | 31 blank | 162 comment | 1 complexity | 49b9013f3f91fa93a03a5c37a3367236 MD5 | raw file
  1<?php
  2/**
  3 * Error Class
  4 *
  5 * This class is a helper for thrown errors in that it holds the data for an evergreen exception
  6 * to allow the error information to be accessed from url's or pages loaded by the thrown error.
  7 *
  8 *
  9 * Copyright 2007-2010, NaturalCodeProject (http://www.naturalcodeproject.com)
 10 *
 11 * Licensed under The MIT License
 12 * Redistributions of files must retain the above copyright notice.
 13 *
 14 * @copyright		Copyright 2007-2010, NaturalCodeProject (http://www.naturalcodeproject.com)
 15 * @package			evergreen
 16 * @subpackage		lib
 17 * @version			$Revision$
 18 * @modifiedby		$LastChangedBy$
 19 * @lastmodified	$Date$
 20 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 21 */
 22
 23/**
 24 * Error Class
 25 *
 26 * This class is a helper for thrown errors in that it holds the data for an evergreen exception
 27 * to allow the error information to be accessed from url's or pages loaded by the thrown error.
 28 *
 29 * Hooks:
 30 * Error.setupError.before
 31 * Error.setupError.after
 32 * Error.getMessage
 33 * Error.getCode
 34 * Error.getFile
 35 * Error.getLine
 36 * Error.getTrace
 37 * Error.getParams
 38 * Error.triggered
 39 *
 40 * @package       evergreen
 41 * @subpackage    lib
 42 */
 43final class Error {
 44	/**
 45	 * Indicator used to tell if an error has been triggered.
 46	 * 
 47	 * @access private
 48	 * @static
 49	 * @var boolean
 50	 */
 51	private static $triggered = false;
 52	
 53	/**
 54	 * Current triggered error's message.
 55	 * 
 56	 * @access private
 57	 * @static
 58	 * @var string
 59	 */
 60	private static $message = "Unknown exception";
 61	
 62	/**
 63	 * Current triggered error's code.
 64	 * 
 65	 * @access private
 66	 * @static
 67	 * @var mixed
 68	 */
 69	private static $code = 0;
 70	
 71	/**
 72	 * Current triggered error's file.
 73	 * 
 74	 * @access private
 75	 * @static
 76	 * @var string
 77	 */
 78	private static $file = null;
 79	
 80	/**
 81	 * Current triggered error's line.
 82	 * 
 83	 * @access private
 84	 * @static
 85	 * @var integer
 86	 */
 87	private static $line = 0;
 88	
 89	/**
 90	 * Current triggered error's trace.
 91	 * 
 92	 * @access private
 93	 * @static
 94	 * @var array
 95	 */
 96	private static $trace = array();
 97	
 98	/**
 99	 * Current triggered error's params.
100	 * 
101	 * @access private
102	 * @static
103	 * @var array
104	 */
105	private static $params = array();
106	
107	/**
108	 * Sets up the error class with data from the thrown error.
109	 * 
110	 * @access public
111	 * @static
112	 * @final
113	 * @param object $e The exception that was thrown
114	 */
115	final public static function setupError($e) {
116		// call hook
117		Hook::call('Error.setupError.before', array(&$e));
118		
119		if (is_object($e)) {
120			self::$message = $e->getMessage();
121			self::$code = $e->getCode();
122			self::$file = $e->getFile();
123			self::$line = $e->getLine();
124			self::$trace = $e->getTrace();
125			self::$params = $e->getParams();
126			self::$triggered = true;
127		}
128		
129		// call hook
130		Hook::call('Error.setupError.after');
131	}
132	
133	/**
134	 * Returns the error message.
135	 * 
136	 * @access public
137	 * @static
138	 * @final
139	 * @return string
140	 */
141	final public static function getMessage() {
142		$message = self::$message;
143		
144		// call hook
145		Hook::call('Error.getMessage', array(&$message));
146		
147		return $message;
148	}
149	
150	/**
151	 * Returns the error code.
152	 * 
153	 * @access public
154	 * @static
155	 * @final
156	 * @return mixed
157	 */
158	final public static function getCode() {
159		$code = self::$code;
160		
161		// call hook
162		Hook::call('Error.getCode', array(&$code));
163		
164		return $message;
165	}
166	
167	/**
168	 * Returns the file that the error occurred in.
169	 * 
170	 * @access public
171	 * @static
172	 * @final
173	 * @return string
174	 */
175	final public static function getFile() {
176		$file = self::$file;
177		
178		// call hook
179		Hook::call('Error.getFile', array(&$file));
180		
181		return $file;
182	}
183	
184	/**
185	 * Returns the line number the error occurred at.
186	 * 
187	 * @access public
188	 * @static
189	 * @final
190	 * @return integer
191	 */
192	final public static function getLine() {
193		$line = self::$line;
194		
195		// call hook
196		Hook::call('Error.getLine', array(&$line));
197		
198		return $line;
199	}
200	
201	/**
202	 * Returns the error trace.
203	 * 
204	 * @access public
205	 * @static
206	 * @final
207	 * @return array
208	 */
209	final public static function getTrace() {
210		$trace = self::$trace;
211		
212		// call hook
213		Hook::call('Error.getTrace', array(&$trace));
214		
215		return $trace;
216	}
217	
218	/**
219	 * Returns the params from the error.
220	 * 
221	 * @access public
222	 * @static
223	 * @final
224	 * @return array
225	 */
226    final public static function getParams() {
227        $params = self::$params;
228		
229		// call hook
230		Hook::call('Error.getParams', array(&$params));
231		
232		return $params;
233    }
234	
235	/**
236	 * Returns if an error has been triggered or not.
237	 * 
238	 * @access public
239	 * @static
240	 * @final
241	 * @return boolean true if an error has been triggered and boolean false if not
242	 */
243	final public static function triggered() {
244		$triggered = self::$triggered;
245		
246		// call hook
247		Hook::call('Error.triggered', array(&$triggered));
248		
249		return $triggered;
250	}
251}
252?>