| [ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * Exception classes file 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Exceptions 11 */ 12 13 /** 14 * TException class 15 * 16 * TException is the base class for all PRADO exceptions. 17 * 18 * TException provides the functionality of translating an error code 19 * into a descriptive error message in a language that is preferred 20 * by user browser. Additional parameters may be passed together with 21 * the error code so that the translated message contains more detailed 22 * information. 23 * 24 * By default, TException looks for a message file by calling 25 * {@link getErrorMessageFile()} method, which uses the "message-xx.txt" 26 * file located under "System.Exceptions" folder, where "xx" is the 27 * code of the user preferred language. If such a file is not found, 28 * "message.txt" will be used instead. 29 * 30 * @author Qiang Xue <qiang.xue@gmail.com> 31 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 32 * @package System.Exceptions 33 * @since 3.0 34 */ 35 class TException extends Exception 36 { 37 private $_errorCode=''; 38 39 /** 40 * Constructor. 41 * @param string error message. This can be a string that is listed 42 * in the message file. If so, the message in the preferred language 43 * will be used as the error message. Any rest parameters will be used 44 * to replace placeholders ({0}, {1}, {2}, etc.) in the message. 45 */ 46 public function __construct($errorMessage) 47 { 48 $this->_errorCode=$errorMessage; 49 $errorMessage=$this->translateErrorMessage($errorMessage); 50 $args=func_get_args(); 51 array_shift($args); 52 $n=count($args); 53 $tokens=array(); 54 for($i=0;$i<$n;++$i) 55 $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]); 56 parent::__construct(strtr($errorMessage,$tokens)); 57 } 58 59 /** 60 * Translates an error code into an error message. 61 * @param string error code that is passed in the exception constructor. 62 * @return string the translated error message 63 */ 64 protected function translateErrorMessage($key) 65 { 66 $msgFile=$this->getErrorMessageFile(); 67 if(($entries=@file($msgFile))===false) 68 return $key; 69 else 70 { 71 foreach($entries as $entry) 72 { 73 @list($code,$message)=explode('=',$entry,2); 74 if(trim($code)===$key) 75 return trim($message); 76 } 77 return $key; 78 } 79 } 80 81 /** 82 * @return string path to the error message file 83 */ 84 protected function getErrorMessageFile() 85 { 86 $lang=Prado::getPreferredLanguage(); 87 $msgFile=Prado::getFrameworkPath().'/Exceptions/messages-'.$lang.'.txt'; 88 if(!is_file($msgFile)) 89 $msgFile=Prado::getFrameworkPath().'/Exceptions/messages.txt'; 90 return $msgFile; 91 } 92 93 /** 94 * @return string error code 95 */ 96 public function getErrorCode() 97 { 98 return $this->_errorCode; 99 } 100 101 /** 102 * @param string error code 103 */ 104 public function setErrorCode($code) 105 { 106 $this->_errorCode=$code; 107 } 108 109 /** 110 * @return string error message 111 */ 112 public function getErrorMessage() 113 { 114 return $this->getMessage(); 115 } 116 117 /** 118 * @param string error message 119 */ 120 protected function setErrorMessage($message) 121 { 122 $this->message=$message; 123 } 124 } 125 126 /** 127 * TSystemException class 128 * 129 * TSystemException is the base class for all framework-level exceptions. 130 * 131 * @author Qiang Xue <qiang.xue@gmail.com> 132 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 133 * @package System.Exceptions 134 * @since 3.0 135 */ 136 class TSystemException extends TException 137 { 138 } 139 140 /** 141 * TApplicationException class 142 * 143 * TApplicationException is the base class for all user application-level exceptions. 144 * 145 * @author Qiang Xue <qiang.xue@gmail.com> 146 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 147 * @package System.Exceptions 148 * @since 3.0 149 */ 150 class TApplicationException extends TException 151 { 152 } 153 154 /** 155 * TInvalidOperationException class 156 * 157 * TInvalidOperationException represents an exception caused by invalid operations. 158 * 159 * @author Qiang Xue <qiang.xue@gmail.com> 160 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 161 * @package System.Exceptions 162 * @since 3.0 163 */ 164 class TInvalidOperationException extends TSystemException 165 { 166 } 167 168 /** 169 * TInvalidDataTypeException class 170 * 171 * TInvalidDataTypeException represents an exception caused by invalid data type. 172 * 173 * @author Qiang Xue <qiang.xue@gmail.com> 174 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 175 * @package System.Exceptions 176 * @since 3.0 177 */ 178 class TInvalidDataTypeException extends TSystemException 179 { 180 } 181 182 /** 183 * TInvalidDataValueException class 184 * 185 * TInvalidDataValueException represents an exception caused by invalid data value. 186 * 187 * @author Qiang Xue <qiang.xue@gmail.com> 188 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 189 * @package System.Exceptions 190 * @since 3.0 191 */ 192 class TInvalidDataValueException extends TSystemException 193 { 194 } 195 196 /** 197 * TConfigurationException class 198 * 199 * TConfigurationException represents an exception caused by invalid configurations, 200 * such as error in an application configuration file or control template file. 201 * 202 * @author Qiang Xue <qiang.xue@gmail.com> 203 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 204 * @package System.Exceptions 205 * @since 3.0 206 */ 207 class TConfigurationException extends TSystemException 208 { 209 } 210 211 /** 212 * TIOException class 213 * 214 * TIOException represents an exception related with improper IO operations. 215 * 216 * @author Qiang Xue <qiang.xue@gmail.com> 217 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 218 * @package System.Exceptions 219 * @since 3.0 220 */ 221 class TIOException extends TSystemException 222 { 223 } 224 225 /** 226 * TDbException class 227 * 228 * TDbException represents an exception related with DB operations. 229 * 230 * @author Qiang Xue <qiang.xue@gmail.com> 231 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 232 * @package System.Exceptions 233 * @since 3.0 234 */ 235 class TDbException extends TSystemException 236 { 237 } 238 239 /** 240 * TDbConnectionException class 241 * 242 * TDbConnectionException represents an exception caused by DB connection failure. 243 * 244 * @author Qiang Xue <qiang.xue@gmail.com> 245 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 246 * @package System.Exceptions 247 * @since 3.0 248 */ 249 class TDbConnectionException extends TDbException 250 { 251 } 252 253 /** 254 * TNotSupportedException class 255 * 256 * TNotSupportedException represents an exception caused by using an unsupported PRADO feature. 257 * 258 * @author Qiang Xue <qiang.xue@gmail.com> 259 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 260 * @package System.Exceptions 261 * @since 3.0 262 */ 263 class TNotSupportedException extends TSystemException 264 { 265 } 266 267 /** 268 * TPhpErrorException class 269 * 270 * TPhpErrorException represents an exception caused by a PHP error. 271 * This exception is mainly thrown within a PHP error handler. 272 * 273 * @author Qiang Xue <qiang.xue@gmail.com> 274 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 275 * @package System.Exceptions 276 * @since 3.0 277 */ 278 class TPhpErrorException extends TSystemException 279 { 280 /** 281 * Constructor. 282 * @param integer error number 283 * @param string error string 284 * @param string error file 285 * @param integer error line number 286 */ 287 public function __construct($errno,$errstr,$errfile,$errline) 288 { 289 static $errorTypes=array( 290 E_ERROR => "Error", 291 E_WARNING => "Warning", 292 E_PARSE => "Parsing Error", 293 E_NOTICE => "Notice", 294 E_CORE_ERROR => "Core Error", 295 E_CORE_WARNING => "Core Warning", 296 E_COMPILE_ERROR => "Compile Error", 297 E_COMPILE_WARNING => "Compile Warning", 298 E_USER_ERROR => "User Error", 299 E_USER_WARNING => "User Warning", 300 E_USER_NOTICE => "User Notice", 301 E_STRICT => "Runtime Notice" 302 ); 303 $errorType=isset($errorTypes[$errno])?$errorTypes[$errno]:'Unknown Error'; 304 parent::__construct("[$errorType] $errstr (@line $errline in file $errfile)."); 305 } 306 } 307 308 309 /** 310 * THttpException class 311 * 312 * THttpException represents an exception that is caused by invalid operations 313 * of end-users. The {@link getStatusCode StatusCode} gives the type of HTTP error. 314 * It is used by {@link TErrorHandler} to provide different error output to users. 315 * 316 * @author Qiang Xue <qiang.xue@gmail.com> 317 * @version $Id: TException.php 1397 2006-09-07 07:55:53Z wei $ 318 * @package System.Exceptions 319 * @since 3.0 320 */ 321 class THttpException extends TSystemException 322 { 323 private $_statusCode; 324 325 /** 326 * Constructor. 327 * @param integer HTTP status code, such as 404, 500, etc. 328 * @param string error message. This can be a string that is listed 329 * in the message file. If so, the message in the preferred language 330 * will be used as the error message. Any rest parameters will be used 331 * to replace placeholders ({0}, {1}, {2}, etc.) in the message. 332 */ 333 public function __construct($statusCode,$errorMessage) 334 { 335 $this->_statusCode=$statusCode; 336 $this->setErrorCode($errorMessage); 337 $errorMessage=$this->translateErrorMessage($errorMessage); 338 $args=func_get_args(); 339 array_shift($args); 340 array_shift($args); 341 $n=count($args); 342 $tokens=array(); 343 for($i=0;$i<$n;++$i) 344 $tokens['{'.$i.'}']=TPropertyValue::ensureString($args[$i]); 345 parent::__construct(strtr($errorMessage,$tokens)); 346 } 347 348 /** 349 * @return integer HTTP status code, such as 404, 500, etc. 350 */ 351 public function getStatusCode() 352 { 353 return $this->_statusCode; 354 } 355 } 356 357 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |