[ Index ] |
|
Code source de Joomla 1.0.13 |
1 <?php 2 /** 3 * patErrorManager main error management class used by pat tools for the 4 * application-internal error management. Creates patError objects for 5 * any errors for precise error management. 6 * 7 * $Id: patErrorManager.php 3165 2006-04-19 23:53:40Z eddieajau $ 8 * 9 * @package patError 10 */ 11 12 /** 13 * error definition: illegal options. 14 */ 15 define( 'PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS', 1 ); 16 17 /** 18 * error definition: callback function does not exist. 19 */ 20 define( 'PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE', 2 ); 21 22 /** 23 * error definition: illegal error handling mode. 24 */ 25 define( 'PATERRORMANAGER_ERROR_ILLEGAL_MODE', 3 ); 26 27 28 /** 29 * global definitions needed to keep track of things when calling the patErrorManager 30 * static methods. 31 */ 32 $GLOBALS['_pat_errorHandling'] = array( 33 E_NOTICE => array( 'mode' => 'echo' ), 34 E_WARNING => array( 'mode' => 'echo' ), 35 E_ERROR => array( 'mode' => 'die' ) 36 ); 37 38 /** 39 * available error levels 40 * Stored in a variable to keep them flexible 41 */ 42 $GLOBALS['_pat_errorLevels'] = array( 43 E_NOTICE => 'Notice', 44 E_WARNING => 'Warning', 45 E_ERROR => 'Error' 46 ); 47 /** 48 * error class names 49 * Stored in a variable allows to change during runtime 50 */ 51 $GLOBALS['_pat_errorClass'] = 'patError'; 52 53 /** 54 * ignore errors 55 * Store error-codes that will be ignored forever 56 */ 57 $GLOBALS['_pat_errorIgnores'] = array(); 58 59 /** 60 * expects errors 61 * Store error-codes that will be ignored once 62 */ 63 $GLOBALS['_pat_errorExpects'] = array(); 64 65 66 /** 67 * patErrorManager main error management class used by pat tools for the 68 * application-internal error management. Creates patError objects for 69 * any errors for precise error management. 70 * 71 * @static 72 * @package patError 73 * @version 0.3 74 * @author gERD Schaufelberger <gerd@php-tools.net> 75 * @author Stephan Schmidt <schst@php-tools.net> 76 * @license LGPL 77 * @link http://www.php-tools.net 78 * @todo implement ignoreError() to ignore errrors with a certain code 79 * @todo implement expectError() to ignore an error with a certain code only once. 80 */ 81 class patErrorManager 82 { 83 /** 84 * method for checking whether the return value of a pat application method is a pat 85 * error object. 86 * 87 * @static 88 * @access public 89 * @param mixed &$object 90 * @return boolean $result True if argument is a patError-object, false otherwise. 91 */ 92 function isError( &$object ) 93 { 94 if( !is_object( $object ) ) 95 { 96 return false; 97 } 98 99 if( get_class( $object ) != strtolower( $GLOBALS['_pat_errorClass'] ) && !is_subclass_of( $object, $GLOBALS['_pat_errorClass'] ) ) 100 { 101 return false; 102 } 103 104 return true; 105 } 106 107 /** 108 * wrapper for the {@link raise()} method where you do not have to specify the 109 * error level - a {@link patError} object with error level E_ERROR will be returned. 110 * 111 * @static 112 * @access public 113 * @param string $code The application-internal error code for this error 114 * @param string $msg The error message, which may also be shown the user if need be. 115 * @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN). 116 * @return object $error The configured patError object 117 * @see raise() 118 * @see patError 119 */ 120 function &raiseError( $code, $msg, $info = null ) 121 { 122 return patErrorManager::raise( E_ERROR, $code, $msg, $info ); 123 } 124 125 /** 126 * wrapper for the {@link raise()} method where you do not have to specify the 127 * error level - a {@link patError} object with error level E_WARNING will be returned. 128 * 129 * @static 130 * @access public 131 * @param string $code The application-internal error code for this error 132 * @param string $msg The error message, which may also be shown the user if need be. 133 * @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN). 134 * @return object $error The configured patError object 135 * @see raise() 136 * @see patError 137 */ 138 function &raiseWarning( $code, $msg, $info = null ) 139 { 140 return patErrorManager::raise( E_WARNING, $code, $msg, $info ); 141 } 142 143 /** 144 * wrapper for the {@link raise()} method where you do not have to specify the 145 * error level - a {@link patError} object with error level E_NOTICE will be returned. 146 * 147 * @static 148 * @access public 149 * @param string $code The application-internal error code for this error 150 * @param string $msg The error message, which may also be shown the user if need be. 151 * @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN). 152 * @return object $error The configured patError object 153 * @see raise() 154 * @see patError 155 */ 156 function &raiseNotice( $code, $msg, $info = null ) 157 { 158 return patErrorManager::raise( E_NOTICE, $code, $msg, $info ); 159 } 160 161 /** 162 * creates a new patError object given the specified information. 163 * 164 * @access public 165 * @param int $level The error level - use any of PHP's own error levels for this: E_ERROR, E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE. 166 * @param string $code The application-internal error code for this error 167 * @param string $msg The error message, which may also be shown the user if need be. 168 * @param mixed $info Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN). 169 * @return mixed $error The configured patError object or false if this error should be ignored 170 * @see patError 171 * @todo implement 'simple' mode that returns just false (BC for patConfiguration) 172 * @todo either remove HTML tags and entities from output or test for enviroment!!! <b></b> in shell is ugly! 173 */ 174 function &raise( $level, $code, $msg, $info = null ) 175 { 176 // ignore this error? 177 if( in_array( $code, $GLOBALS['_pat_errorIgnores'] ) ) 178 { 179 return false; 180 } 181 182 // this error was expected 183 if( !empty( $GLOBALS['_pat_errorExpects'] ) ) 184 { 185 $expected = array_pop( $GLOBALS['_pat_errorExpects'] ); 186 if( in_array( $code, $expected ) ) 187 { 188 return false; 189 } 190 } 191 192 // need patError 193 $class = $GLOBALS['_pat_errorClass']; 194 if( !class_exists( $class ) ) 195 { 196 include_once dirname( __FILE__ ) . '/'. $class .'.php'; 197 } 198 199 // build error object 200 $error = new $class( $level, $code, $msg, $info ); 201 202 // see what to do with this kind of error 203 $handling = patErrorManager::getErrorHandling( $level ); 204 205 $function = 'handleError' . ucfirst( $handling['mode'] ); 206 return patErrorManager::$function( $error, $handling ); 207 } 208 209 /** 210 * register a new error level 211 * 212 * This allows you to add custom error levels to the built-in 213 * - E_NOTICE 214 * - E_WARNING 215 * - E_NOTICE 216 * 217 * You may use this level in subsequent calls to raise(). 218 * Error handling will be set to 'ignore' for the new level, you 219 * may change it by using setErrorHandling(). 220 * 221 * You could be using PHP's predefined constants for error levels 222 * or any other integer value. 223 * 224 * @access public 225 * @param integer error level 226 * @param string human-readable name 227 * @return boolean true on success; false if the level already has been registered 228 * @see raise(), setErrorHandling() 229 * @link http://www.php.net/manual/en/function.error-reporting.php 230 */ 231 function registerErrorLevel( $level, $name ) 232 { 233 if( isset( $GLOBALS['_pat_errorLevels'][$level] ) ) 234 { 235 return false; 236 } 237 $GLOBALS['_pat_errorLevels'][$level] = $name; 238 patErrorManager::setErrorHandling( $level, 'ignore' ); 239 return true; 240 } 241 242 /** 243 * sets the way the patErrorManager will handle teh different error levels. Use this 244 * if you want to override the default settings. 245 * 246 * Error handling modes: 247 * - ignore 248 * - trigger 249 * - verbose 250 * - echo 251 * - callback 252 * - die 253 * 254 * You may also set the error handling for several modes at once using PHP's bit operations. 255 * Examples: 256 * - E_ALL = Set the handling for all levels 257 * - E_ERROR | E_WARNING = Set the handling for errors and warnings 258 * - E_ALL ^ E_ERROR = Set the handling for all levels except errors 259 * 260 * @static 261 * @access public 262 * @param int $level The error level for which to set the error handling 263 * @param string $mode The mode to use for the error handling. 264 * @param mixed $options Optional: Any options needed for the given mode. 265 * @return mixed $result True on success, or a patError object if failed. 266 * @see getErrorHandling() 267 */ 268 function setErrorHandling( $level, $mode, $options = null ) 269 { 270 $levels = $GLOBALS['_pat_errorLevels']; 271 272 $function = 'handleError' . ucfirst( $mode ); 273 if( !is_callable( array( 'patErrorManager', $function ) ) ) 274 { 275 return patErrorManager::raiseError( E_ERROR, 276 'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_MODE, 277 'Error Handling mode is not knwon', 278 'Mode: ' . $mode . ' is not implemented.' 279 ); 280 } 281 282 foreach( $levels as $eLevel => $eTitle ) 283 { 284 if( ( $level & $eLevel ) != $eLevel ) 285 { 286 continue; 287 } 288 289 // set callback options 290 if( $mode == 'callback' ) 291 { 292 if( !is_array( $options ) ) 293 { 294 return patErrorManager::raiseError( E_ERROR, 295 'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS, 296 'Options for callback not valid' 297 ); 298 } 299 300 if( !is_callable( $options ) ) 301 { 302 $tmp = array( 'GLOBAL' ); 303 if( is_array( $options ) ) 304 { 305 $tmp[0] = $options[0]; 306 $tmp[1] = $options[1]; 307 } 308 else 309 { 310 $tmp[1] = $options; 311 } 312 313 return patErrorManager::raiseError( E_ERROR, 314 'patErrorManager:' . PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE, 315 'Function is not callable', 316 'Function:' . $tmp[1] . ' scope ' . $tmp[0] . '.' 317 ); 318 } 319 } 320 321 322 // save settings 323 $GLOBALS['_pat_errorHandling'][$eLevel] = array( 'mode' => $mode ); 324 if( $options != null ) 325 { 326 $GLOBALS['_pat_errorHandling'][$eLevel]['options'] = $options; 327 } 328 } 329 330 return true; 331 } 332 333 /** 334 * retrieves the current error handling settings for the specified error level. 335 * 336 * @access public 337 * @param int $level The error level to retrieve. This can be any of PHP's own error levels, e.g. E_ALL, E_NOTICE... 338 * @return array $handling All error handling details 339 */ 340 function getErrorHandling( $level ) 341 { 342 return $GLOBALS['_pat_errorHandling'][$level]; 343 } 344 345 /** 346 * translate an error level 347 * 348 * returns the human-readable name for an error level, 349 * e.g. E_ERROR will be translated to 'Error'. 350 * 351 * @access public 352 * @param integer error level 353 * @return string human-readable representation 354 */ 355 function translateErrorLevel( $level ) 356 { 357 if( isset( $GLOBALS['_pat_errorLevels'][$level] ) ) 358 { 359 return $GLOBALS['_pat_errorLevels'][$level]; 360 } 361 return 'Unknown error level'; 362 } 363 364 /** 365 * setErrorClass 366 * 367 * In order to autoload this class, the filename containing that class must be 368 * named like the class itself; with an appending ".php". Although the file must be stored 369 * in the same directory as patErrorManager.php (this file) 370 * 371 * @access public 372 * @param string $name classname 373 * @return boolean $result true on success 374 */ 375 function setErrorClass( $name ) 376 { 377 // include old error-class 378 if( $name !== $GLOBALS['_pat_errorClass'] && !class_exists( $GLOBALS['_pat_errorClass'] ) ) 379 { 380 include_once dirname( __FILE__ ) . '/' . $GLOBALS['_pat_errorClass'] . '.php'; 381 } 382 383 $GLOBALS['_pat_errorClass'] = $name; 384 return true; 385 } 386 387 /** 388 * add error codes to be ingored 389 * 390 * @static 391 * @access public 392 * @param mixed $codes either an array of error code or a single code that will be ignored in future 393 * @return boolean $result true on success 394 */ 395 function addIgnore( $codes ) 396 { 397 if( !is_array( $codes ) ) 398 { 399 $codes = array( $codes ); 400 } 401 402 $codes = array_merge( $GLOBALS['_pat_errorIgnores'], $codes ); 403 $GLOBALS['_pat_errorIgnores'] = array_unique( $codes ); 404 405 return true; 406 } 407 408 /** 409 * removeIgnore 410 * 411 * 412 * @static 413 * @access public 414 * @return boolean $result true on success 415 */ 416 function removeIgnore( $codes ) 417 { 418 if( !is_array( $codes ) ) 419 { 420 $codes = array( $codes ); 421 } 422 423 foreach( $codes as $code ) 424 { 425 $index = array_search( $code, $GLOBALS['_pat_errorIgnores'] ); 426 if( $index === false ) 427 { 428 continue; 429 } 430 431 unset( $GLOBALS['_pat_errorIgnores'][$index] ); 432 } 433 434 // reorder the codes 435 $GLOBALS['_pat_errorIgnores'] = array_values( $GLOBALS['_pat_errorIgnores'] ); 436 437 return true; 438 } 439 440 /** 441 * recieve all registerd error codes that will be ignored 442 * 443 * @static 444 * @access public 445 * @return array $codes list of error codes 446 */ 447 function getIgnore() 448 { 449 return $GLOBALS['_pat_errorIgnores']; 450 } 451 452 /** 453 * empty list of errors to be ignored 454 * 455 * @static 456 * @access public 457 * @return boolean $result true on success 458 */ 459 function clearIgnore() 460 { 461 $GLOBALS['_pat_errorIgnores'] = array(); 462 return true; 463 } 464 465 /** 466 * add expected errors to stack 467 * 468 * @static 469 * @access public 470 * @param mixed $codes either an array of error code or a single code that will be ignored in future 471 * @return boolean $result true on success 472 */ 473 function pushExpect( $codes ) 474 { 475 if( !is_array( $codes ) ) 476 { 477 $codes = array( $codes ); 478 } 479 480 array_push( $GLOBALS['_pat_errorExpects'], $codes ); 481 482 return true; 483 } 484 485 /** 486 * remove top of error-codes from stack 487 * 488 * @static 489 * @access public 490 * @return boolean $result true on success 491 */ 492 function popExpect() 493 { 494 if( empty( $GLOBALS['_pat_errorExpects'] ) ) 495 { 496 return false; 497 } 498 499 array_pop( $GLOBALS['_pat_errorExpects'] ); 500 return true; 501 } 502 503 /** 504 * recieve all registerd error codes that will be ignored 505 * 506 * @static 507 * @access public 508 * @return array $codes list of error codes 509 */ 510 function getExpect() 511 { 512 return $GLOBALS['_pat_errorExpects']; 513 } 514 515 /** 516 * empty list of errors to be ignored 517 * 518 * @static 519 * @access public 520 * @return boolean $result true on success 521 */ 522 function clearExpect() 523 { 524 $GLOBALS['_pat_errorExpects'] = array(); 525 return true; 526 } 527 528 /** 529 * handleError: Ignore 530 * Does nothing 531 * 532 * @access private 533 * @param object $error patError-Object 534 * @param array $options options for handler 535 * @return object $error error-object 536 * @see raise() 537 */ 538 function &handleErrorIgnore( &$error, $options ) 539 { 540 return $error; 541 } 542 543 /** 544 * handleError: Echo 545 * display error message 546 * 547 * @access private 548 * @param object $error patError-Object 549 * @param array $options options for handler 550 * @return object $error error-object 551 * @see raise() 552 */ 553 function &handleErrorEcho( &$error, $options ) 554 { 555 $level_human = patErrorManager::translateErrorLevel( $error->getLevel() ); 556 557 if( isset( $_SERVER['HTTP_HOST'] ) ) 558 { 559 // output as html 560 echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n"; 561 } 562 else 563 { 564 // output as simple text 565 if( defined( 'STDERR' ) ) 566 { 567 fwrite( STDERR, "pat-$level_human: " . $error->getMessage() . "\n" ); 568 } 569 else 570 { 571 echo "pat-$level_human: " . $error->getMessage() . "\n"; 572 } 573 } 574 return $error; 575 } 576 577 /** 578 * handleError: Verbose 579 * display verbose output for developing purpose 580 * 581 * @access private 582 * @param object $error patError-Object 583 * @param array $options options for handler 584 * @return object $error error-object 585 * @see raise() 586 */ 587 function &handleErrorVerbose( &$error, $options ) 588 { 589 $level_human = patErrorManager::translateErrorLevel( $error->getLevel() ); 590 $info = $error->getInfo(); 591 592 if( isset( $_SERVER['HTTP_HOST'] ) ) 593 { 594 // output as html 595 echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n"; 596 if( $info != null ) 597 { 598 echo " " . $error->getInfo() . "<br />\n"; 599 } 600 echo $error->getBacktrace( true ); 601 } 602 else 603 { 604 // output as simple text 605 echo "pat-$level_human: " . $error->getMessage() . "\n"; 606 if( $info != null ) 607 { 608 echo " " . $error->getInfo() . "\n"; 609 } 610 611 } 612 return $error; 613 } 614 615 /** 616 * handleError: die 617 * display error-message and die 618 * 619 * @access private 620 * @param object $error patError-Object 621 * @param array $options options for handler 622 * @return object $error error-object 623 * @see raise() 624 */ 625 function &handleErrorDie( &$error, $options ) 626 { 627 $level_human = patErrorManager::translateErrorLevel( $error->getLevel() ); 628 629 if( isset( $_SERVER['HTTP_HOST'] ) ) 630 { 631 // output as html 632 die( "<br /><b>pat-$level_human</b> " . $error->getMessage() . "<br />\n" ); 633 } 634 else 635 { 636 // output as simple text 637 if( defined( 'STDERR' ) ) 638 { 639 fwrite( STDERR, "pat-$level_human " . $error->getMessage() . "\n" ); 640 } 641 else 642 { 643 die( "pat-$level_human " . $error->getMessage() . "\n" ); 644 } 645 } 646 return $error; 647 } 648 649 /** 650 * handleError: trigger 651 * trigger php-error 652 * 653 * @access private 654 * @param object $error patError-Object 655 * @param array $options options for handler 656 * @return object $error error-object 657 * @see raise() 658 */ 659 function &handleErrorTrigger( &$error, $options ) 660 { 661 switch( $error->getLevel() ) 662 { 663 case E_NOTICE: 664 $level = E_USER_NOTICE; 665 break; 666 case E_WARNING: 667 $level = E_USER_WARNING; 668 break; 669 case E_NOTICE: 670 $level = E_NOTICE; 671 break; 672 default: 673 $level = E_USER_ERROR; 674 break; 675 } 676 677 trigger_error( $error->getMessage(), $level ); 678 return $error; 679 } 680 681 /** 682 * handleError: callback 683 * forward error to custom handler 684 * 685 * @access private 686 * @param object $error patError-Object 687 * @param array $options options for handler 688 * @return object $error error-object 689 * @see raise() 690 */ 691 function &handleErrorCallback( &$error, $options ) 692 { 693 $opt = $options['options']; 694 $result = call_user_func( $opt, $error ); 695 return $result; 696 } 697 } 698 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Wed Nov 21 14:43:32 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |