[ Index ]
 

Code source de Flux CMS 1.5

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/inc/ -> patErrorManager.php (source)

   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 5019 2005-07-13 06:53:21Z chregu $
   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( strtolower( 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          $r = patErrorManager::raise( E_ERROR, $code, $msg, $info );
 123          return $r;
 124      }
 125      
 126     /**
 127      * wrapper for the {@link raise()} method where you do not have to specify the
 128      * error level - a {@link patError} object with error level E_WARNING will be returned.
 129      *
 130      * @static
 131      * @access    public
 132      * @param    string    $code    The application-internal error code for this error
 133      * @param    string    $msg    The error message, which may also be shown the user if need be.
 134      * @param    mixed    $info    Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
 135      * @return    object    $error    The configured patError object
 136      * @see        raise()
 137      * @see        patError
 138      */
 139      function &raiseWarning( $code, $msg, $info = null )
 140      {
 141          return patErrorManager::raise( E_WARNING, $code, $msg, $info );
 142      }
 143      
 144     /**
 145      * wrapper for the {@link raise()} method where you do not have to specify the
 146      * error level - a {@link patError} object with error level E_NOTICE will be returned.
 147      *
 148      * @static
 149      * @access    public
 150      * @param    string    $code    The application-internal error code for this error
 151      * @param    string    $msg    The error message, which may also be shown the user if need be.
 152      * @param    mixed    $info    Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
 153      * @return    object    $error    The configured patError object
 154      * @see        raise()
 155      * @see        patError
 156      */
 157      function &raiseNotice( $code, $msg, $info = null )
 158      {
 159          return patErrorManager::raise( E_NOTICE, $code, $msg, $info );
 160      }
 161      
 162     /**
 163      * creates a new patError object given the specified information.
 164      *
 165      * @access    public
 166      * @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.
 167      * @param    string    $code    The application-internal error code for this error
 168      * @param    string    $msg    The error message, which may also be shown the user if need be.
 169      * @param    mixed    $info    Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
 170      * @return    mixed    $error    The configured patError object or false if this error should be ignored
 171      * @see        patError
 172      * @todo        implement 'simple' mode that returns just false (BC for patConfiguration)
 173      * @todo        either remove HTML tags and entities from output or test for enviroment!!! <b></b> in shell is ugly!
 174      */
 175      function &raise( $level, $code, $msg, $info = null )
 176      {
 177          // ignore this error?
 178          if( in_array( $code, $GLOBALS['_pat_errorIgnores'] ) )
 179          {
 180              return false;
 181          }
 182  
 183          // need patError
 184          $class    =    $GLOBALS['_pat_errorClass'];
 185          if( !class_exists( $class ) )
 186          {
 187              include_once dirname( __FILE__ ) . '/'. $class .'.php';
 188          }
 189          
 190          // build error object
 191          $error            =&    new    $class( $level, $code, $msg, $info );
 192  
 193          // this error was expected
 194          if( !empty( $GLOBALS['_pat_errorExpects'] ) )
 195          {
 196              $expected =    array_pop( $GLOBALS['_pat_errorExpects'] );
 197              array_push( $GLOBALS['_pat_errorExpects'], $expected );
 198              if( in_array( $code, $expected ) )
 199              {
 200                  return $error;
 201              }
 202          }
 203          
 204          // see what to do with this kind of error        
 205          $handling    =    patErrorManager::getErrorHandling( $level );
 206          
 207          $function    =    'handleError' . ucfirst( $handling['mode'] );
 208          return patErrorManager::$function( $error, $handling );
 209      }
 210  
 211     /**
 212      * register a new error level
 213      *
 214      * This allows you to add custom error levels to the built-in
 215      * - E_NOTICE
 216      * - E_WARNING
 217      * - E_NOTICE
 218      *
 219      * You may use this level in subsequent calls to raise().
 220      * Error handling will be set to 'ignore' for the new level, you
 221      * may change it by using setErrorHandling().
 222      *
 223      * You could be using PHP's predefined constants for error levels
 224      * or any other integer value.
 225      *
 226      * @access    public
 227      * @param    integer    error level
 228      * @param    string    human-readable name
 229      * @return    boolean    true on success; false if the level already has been registered
 230      * @see        raise(), setErrorHandling()
 231      * @link        http://www.php.net/manual/en/function.error-reporting.php
 232      */
 233  	function registerErrorLevel( $level, $name )
 234      {
 235          if( isset( $GLOBALS['_pat_errorLevels'][$level] ) )
 236          {
 237              return false;
 238          }
 239          $GLOBALS['_pat_errorLevels'][$level]    =    $name;
 240          patErrorManager::setErrorHandling( $level, 'ignore' );
 241          return    true;
 242      }
 243      
 244     /**
 245      * sets the way the patErrorManager will handle teh different error levels. Use this
 246      * if you want to override the default settings.
 247      *
 248      * Error handling modes:
 249      * - ignore
 250      * - trigger
 251      * - verbose
 252      * - echo
 253      * - callback
 254      * - die
 255      *
 256      * You may also set the error handling for several modes at once using PHP's bit operations.
 257      * Examples:
 258      * - E_ALL = Set the handling for all levels
 259      * - E_ERROR | E_WARNING = Set the handling for errors and warnings
 260      * - E_ALL ^ E_ERROR = Set the handling for all levels except errors
 261      *
 262      * @static
 263      * @access    public
 264      * @param    int        $level        The error level for which to set the error handling
 265      * @param    string    $mode        The mode to use for the error handling.
 266      * @param    mixed    $options    Optional: Any options needed for the given mode.
 267      * @return    mixed    $result        True on success, or a patError object if failed.
 268      * @see        getErrorHandling()
 269      */
 270      function setErrorHandling( $level, $mode, $options = null )
 271      {
 272          $levels    =    $GLOBALS['_pat_errorLevels'];
 273  
 274          $function    =    'handleError' . ucfirst( $mode );
 275          if( !is_callable( array( 'patErrorManager', $function ) ) )
 276          {
 277              return patErrorManager::raiseError( E_ERROR, 
 278                                                  'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_MODE, 
 279                                                  'Error Handling mode is not knwon',
 280                                                  'Mode: ' .  $mode . ' is not implemented.'
 281                                                  );
 282          }
 283          
 284          foreach( $levels as $eLevel => $eTitle )
 285          {
 286              if( ( $level & $eLevel ) != $eLevel )
 287              {
 288                  continue;
 289              }
 290  
 291              // set callback options
 292              if( $mode == 'callback' )
 293              {
 294                  if( !is_array( $options ) )
 295                  {
 296                      return patErrorManager::raiseError( E_ERROR, 
 297                                                          'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS, 
 298                                                          'Options for callback not valid' 
 299                                                          );
 300                  }
 301                  
 302                  if( !is_callable( $options ) )
 303                  {
 304                      $tmp    =    array( 'GLOBAL' );
 305                      if( is_array( $options ) )
 306                      {
 307                          $tmp[0]    =    $options[0];
 308                          $tmp[1]    =    $options[1];
 309                      }
 310                      else
 311                      {
 312                          $tmp[1]    =    $options;
 313                      }
 314                      
 315                      return patErrorManager::raiseError(    E_ERROR, 
 316                                                          'patErrorManager:' . PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE, 
 317                                                          'Function is not callable', 
 318                                                          'Function:' . $tmp[1]  . ' scope ' . $tmp[0] . '.' 
 319                                                          );
 320                  }
 321              }
 322                  
 323          
 324              // save settings
 325              $GLOBALS['_pat_errorHandling'][$eLevel]    =    array( 'mode' => $mode );
 326              if( $options    != null )
 327              {
 328                  $GLOBALS['_pat_errorHandling'][$eLevel]['options']    =    $options;
 329              }
 330          }
 331              
 332          return  true;
 333      }
 334  
 335     /**
 336      * retrieves the current error handling settings for the specified error level.
 337      *
 338      * @access    public
 339      * @param    int        $level        The error level to retrieve. This can be any of PHP's own error levels, e.g. E_ALL, E_NOTICE...
 340      * @return    array    $handling    All error handling details
 341      */
 342      function getErrorHandling( $level )
 343      {
 344          return $GLOBALS['_pat_errorHandling'][$level];
 345      }
 346  
 347     /**
 348      * translate an error level
 349      *
 350      * returns the human-readable name for an error level,
 351      * e.g. E_ERROR will be translated to 'Error'.
 352      *
 353      * @access    public
 354      * @param    integer        error level
 355      * @return    string        human-readable representation
 356      */
 357  	function translateErrorLevel( $level )
 358      {
 359          if( isset( $GLOBALS['_pat_errorLevels'][$level] ) )
 360          {
 361              return    $GLOBALS['_pat_errorLevels'][$level];
 362          }
 363          return    'Unknown error level';
 364      }
 365      
 366     /**
 367      * setErrorClass
 368      *
 369      * In order to autoload this class, the filename containing that class must be
 370      * named like the class itself; with an appending ".php". Although the file must be stored
 371      * in the same directory as patErrorManager.php (this file)
 372      *
 373      * @access public
 374      * @param string $name classname
 375      * @return boolean $result true on success
 376      */
 377      function setErrorClass( $name )
 378      {
 379          // include old error-class
 380          if( $name !== $GLOBALS['_pat_errorClass'] && !class_exists( $GLOBALS['_pat_errorClass'] ) )
 381          {
 382              include_once dirname( __FILE__ ) . '/' . $GLOBALS['_pat_errorClass'] . '.php';
 383          }
 384      
 385          $GLOBALS['_pat_errorClass']    =    $name;
 386          return true;
 387      }
 388  
 389     /**
 390      * add error codes to be ingored
 391      *
 392      * @static
 393      * @access public
 394      * @param mixed $codes either an array of error code or a single code that will be ignored in future
 395      * @return boolean $result true on success
 396      */
 397      function addIgnore( $codes )
 398      {
 399          if( !is_array( $codes ) )
 400          {
 401              $codes    =    array( $codes );
 402          }
 403      
 404          $codes                            =    array_merge( $GLOBALS['_pat_errorIgnores'], $codes );
 405          $GLOBALS['_pat_errorIgnores']    =    array_unique( $codes );
 406  
 407          return true;
 408      }
 409      
 410     /**
 411      * removeIgnore
 412      *
 413      *
 414      * @static
 415      * @access public
 416      * @return boolean $result true on success
 417      */
 418      function removeIgnore( $codes )
 419      {
 420          if( !is_array( $codes ) )
 421          {
 422              $codes    =    array( $codes );
 423          }
 424          
 425          foreach( $codes as $code )
 426          {
 427              $index    =    array_search( $code, $GLOBALS['_pat_errorIgnores'] );
 428              if( $index === false )
 429              {
 430                  continue;
 431              }
 432              
 433              unset( $GLOBALS['_pat_errorIgnores'][$index] );
 434          }
 435  
 436          // reorder the codes        
 437          $GLOBALS['_pat_errorIgnores']    =    array_values( $GLOBALS['_pat_errorIgnores'] );
 438          
 439          return true;
 440      }
 441      
 442     /**
 443      * recieve all registerd error codes that will be ignored
 444      *
 445      * @static
 446      * @access public
 447      * @return array $codes list of error codes
 448      */
 449      function getIgnore()
 450      {
 451          return $GLOBALS['_pat_errorIgnores'];
 452      }
 453  
 454     /**
 455      * empty list of errors to be ignored
 456      *
 457      * @static
 458      * @access public
 459      * @return boolean $result true on success
 460      */
 461      function clearIgnore()
 462      {
 463          $GLOBALS['_pat_errorIgnores']    =    array();
 464          return true;
 465      }
 466      
 467     /**
 468      * add expected errors to stack
 469      *
 470      * @static
 471      * @access public
 472      * @param mixed $codes either an array of error code or a single code that will be ignored in future
 473      * @return boolean $result true on success
 474      */
 475      function pushExpect( $codes )
 476      {
 477          if( !is_array( $codes ) )
 478          {
 479              $codes    =    array( $codes );
 480          }
 481          
 482          array_push( $GLOBALS['_pat_errorExpects'], $codes );
 483          
 484          return true;
 485      }
 486  
 487     /**
 488      * remove top of error-codes from stack
 489      *
 490      * @static
 491      * @access public
 492      * @return boolean $result true on success
 493      */
 494      function popExpect()
 495      {
 496          if( empty( $GLOBALS['_pat_errorExpects'] ) )
 497          {
 498              return false;
 499          }
 500          
 501          array_pop( $GLOBALS['_pat_errorExpects'] );
 502          return true;
 503      }
 504  
 505     /**
 506      * recieve all registerd error codes that will be ignored
 507      *
 508      * @static
 509      * @access public
 510      * @return array $codes list of error codes
 511      */
 512      function getExpect()
 513      {
 514          return $GLOBALS['_pat_errorExpects'];
 515      }
 516  
 517     /**
 518      * empty list of errors to be ignored
 519      *
 520      * @static
 521      * @access public
 522      * @return boolean $result true on success
 523      */
 524      function clearExpect()
 525      {
 526          $GLOBALS['_pat_errorExpects']    =    array();
 527          return true;
 528      }
 529      
 530     /**
 531      * handleError: Ignore
 532      * Does nothing
 533      *
 534      * @access private
 535      * @param object $error patError-Object
 536      * @param array $options options for handler
 537      * @return object $error error-object
 538      * @see raise()
 539      */
 540      function &handleErrorIgnore( &$error, $options )
 541      {
 542          return $error;
 543      }
 544      
 545     /**
 546      * handleError: Echo
 547      * display error message
 548      *
 549      * @access private
 550      * @param object $error patError-Object
 551      * @param array $options options for handler
 552      * @return object $error error-object
 553      * @see raise()
 554      */
 555      function &handleErrorEcho( &$error, $options )
 556      {
 557          $level_human    =    patErrorManager::translateErrorLevel( $error->getLevel() );
 558      
 559          if( isset( $_SERVER['HTTP_HOST'] ) )
 560          {
 561              // output as html
 562              echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n";
 563          }
 564          else
 565          {
 566              // output as simple text
 567              if( defined( 'STDERR' ) )
 568              {
 569                  fwrite( STDERR, "pat-$level_human: " . $error->getMessage() . "\n" );
 570              }
 571              else
 572              {
 573                  echo "pat-$level_human: " . $error->getMessage() . "\n";
 574              }
 575          }
 576          return $error;
 577      }
 578      
 579     /**
 580      * handleError: Verbose
 581      * display verbose output for developing purpose
 582      *
 583      * @access private
 584      * @param object $error patError-Object
 585      * @param array $options options for handler
 586      * @return object $error error-object
 587      * @see raise()
 588      */
 589      function &handleErrorVerbose( &$error, $options )
 590      {
 591          $level_human    =    patErrorManager::translateErrorLevel( $error->getLevel() );
 592          $info            =    $error->getInfo();
 593      
 594          if( isset( $_SERVER['HTTP_HOST'] ) )
 595          {
 596              // output as html
 597              echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n";
 598              if( $info != null )
 599              {
 600                  echo "&nbsp;&nbsp;&nbsp;" . $error->getInfo() . "<br />\n";
 601              }
 602          }
 603          else
 604          {
 605              // output as simple text
 606              echo "pat-$level_human: " . $error->getMessage() . "\n";
 607              if( $info != null )
 608              {
 609                  echo "    " . $error->getInfo() . "\n";
 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          return call_user_func( $opt, $error );
 695      }
 696  }
 697  ?>


Généré le : Wed Nov 21 13:08:55 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics