patErrorManager.php 27.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
<?php
/**
 * patErrorManager main error management class used by pat tools for the
 * application-internal error management. Creates patError objects for
 * any errors for precise error management.
 *
 * $Id: patErrorManager.php,v 1.16 2005/03/06 12:43:32 schst Exp $
 *
 * @package        patError
 */

/**
 * error definition: illegal options.
 */
define( 'PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS', 1 );

/**
 * error definition: callback function does not exist.
 */
define( 'PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE', 2 );

/**
 * error definition: illegal error handling mode.
 */
define( 'PATERRORMANAGER_ERROR_ILLEGAL_MODE', 3 );


/**
 * global definitions needed to keep track of things when calling the patErrorManager
 * static methods.
 */
$GLOBALS['_pat_errorHandling']        =        array(
                                                                                        E_NOTICE        => array( 'mode' => 'echo' ),
                                                                                        E_WARNING        => array( 'mode' => 'echo' ),
                                                                                        E_ERROR                => array( 'mode' => 'die' )
                                                                                );

/**
 * available error levels
 * Stored in a variable to keep them flexible
 */
$GLOBALS['_pat_errorLevels']        =        array(
                                                                                        E_NOTICE        => 'Notice',
                                                                                        E_WARNING        => 'Warning',
                                                                                        E_ERROR                => 'Error'
                                                                                );
/**
 * error class names
 * Stored in a variable allows to change during runtime
 */
$GLOBALS['_pat_errorClass']        =        'patError';

/**
 * ignore errors
 * Store error-codes that will be ignored forever
 */
$GLOBALS['_pat_errorIgnores']        =        array();

/**
 * expects errors
 * Store error-codes that will be ignored once
 */
$GLOBALS['_pat_errorExpects']        =        array();


/**
 * patErrorManager main error management class used by pat tools for the
 * application-internal error management. Creates patError objects for
 * any errors for precise error management.
 *
 * @static
 * @package        patError
 * @version        0.3
 * @author        gERD Schaufelberger <gerd@php-tools.net>
 * @author        Stephan Schmidt <schst@php-tools.net>
 * @license        LGPL
 * @link        http://www.php-tools.net
 * @todo        implement ignoreError() to ignore errrors with a certain code
 * @todo        implement expectError() to ignore an error with a certain code only once.
 */
class patErrorManager
{
   /**
        * method for checking whether the return value of a pat application method is a pat
        * error object.
        *
        * @static
        * @access        public
        * @param        mixed        &$object
        * @return        boolean $result        True if argument is a patError-object, false otherwise.
        */
    function isError( &$object )
    {
                if( !is_object( $object ) )
                {
                        return false;
                }

                if( strtolower( get_class( $object ) ) != strtolower( $GLOBALS['_pat_errorClass'] ) && !is_subclass_of( $object, $GLOBALS['_pat_errorClass'] ) )
                {
                        return false;
                }

        return  true;
    }

   /**
        * wrapper for the {@link raise()} method where you do not have to specify the
        * error level - a {@link patError} object with error level E_ERROR will be returned.
        *
        * @static
        * @access        public
        * @param        string        $code        The application-internal error code for this error
        * @param        string        $msg        The error message, which may also be shown the user if need be.
        * @param        mixed        $info        Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
        * @return        object        $error        The configured patError object
        * @see                raise()
        * @see                patError
        */
        function &raiseError( $code, $msg, $info = null )
        {
                return patErrorManager::raise( E_ERROR, $code, $msg, $info );
        }

   /**
        * wrapper for the {@link raise()} method where you do not have to specify the
        * error level - a {@link patError} object with error level E_WARNING will be returned.
        *
        * @static
        * @access        public
        * @param        string        $code        The application-internal error code for this error
        * @param        string        $msg        The error message, which may also be shown the user if need be.
        * @param        mixed        $info        Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
        * @return        object        $error        The configured patError object
        * @see                raise()
        * @see                patError
        */
        function &raiseWarning( $code, $msg, $info = null )
        {
                return patErrorManager::raise( E_WARNING, $code, $msg, $info );
        }

   /**
        * wrapper for the {@link raise()} method where you do not have to specify the
        * error level - a {@link patError} object with error level E_NOTICE will be returned.
        *
        * @static
        * @access        public
        * @param        string        $code        The application-internal error code for this error
        * @param        string        $msg        The error message, which may also be shown the user if need be.
        * @param        mixed        $info        Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
        * @return        object        $error        The configured patError object
        * @see                raise()
        * @see                patError
        */
        function &raiseNotice( $code, $msg, $info = null )
        {
                return patErrorManager::raise( E_NOTICE, $code, $msg, $info );
        }

   /**
        * creates a new patError object given the specified information.
        *
        * @access        public
        * @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.
        * @param        string        $code        The application-internal error code for this error
        * @param        string        $msg        The error message, which may also be shown the user if need be.
        * @param        mixed        $info        Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
        * @return        mixed        $error        The configured patError object or false if this error should be ignored
        * @see                patError
        * @todo                implement 'simple' mode that returns just false (BC for patConfiguration)
        * @todo                either remove HTML tags and entities from output or test for enviroment!!! <b></b> in shell is ugly!
        */
    function &raise( $level, $code, $msg, $info = null )
    {
                // ignore this error?
                if( in_array( $code, $GLOBALS['_pat_errorIgnores'] ) )
                {
                        return false;
                }

            // need patError
                $class        =        $GLOBALS['_pat_errorClass'];
                if( !class_exists( $class ) )
                {
                        include_once dirname( __FILE__ ) . '/'. $class .'.php';
                }

                // build error object
                $error                        =&        new        $class( $level, $code, $msg, $info );

                // this error was expected
                if( !empty( $GLOBALS['_pat_errorExpects'] ) )
                {
                        $expected =        array_pop( $GLOBALS['_pat_errorExpects'] );
                        array_push( $GLOBALS['_pat_errorExpects'], $expected );
                        if( in_array( $code, $expected ) )
                        {
                                return $error;
                        }
                }

                // see what to do with this kind of error
                $handling        =        patErrorManager::getErrorHandling( $level );

                $function        =        'handleError' . ucfirst( $handling['mode'] );
                return patErrorManager::$function( $error, $handling );
    }

   /**
        * register a new error level
        *
        * This allows you to add custom error levels to the built-in
        * - E_NOTICE
        * - E_WARNING
        * - E_NOTICE
        *
        * You may use this level in subsequent calls to raise().
        * Error handling will be set to 'ignore' for the new level, you
        * may change it by using setErrorHandling().
        *
        * You could be using PHP's predefined constants for error levels
        * or any other integer value.
        *
        * @access        public
        * @param        integer        error level
        * @param        string        human-readable name
        * @return        boolean        true on success; false if the level already has been registered
        * @see                raise(), setErrorHandling()
        * @link                http://www.php.net/manual/en/function.error-reporting.php
        */
        function registerErrorLevel( $level, $name )
        {
                if( isset( $GLOBALS['_pat_errorLevels'][$level] ) )
                {
                        return false;
                }
                $GLOBALS['_pat_errorLevels'][$level]        =        $name;
                patErrorManager::setErrorHandling( $level, 'ignore' );
                return        true;
        }

   /**
        * sets the way the patErrorManager will handle teh different error levels. Use this
        * if you want to override the default settings.
        *
        * Error handling modes:
        * - ignore
        * - trigger
        * - verbose
        * - echo
        * - callback
        * - die
        *
        * You may also set the error handling for several modes at once using PHP's bit operations.
        * Examples:
        * - E_ALL = Set the handling for all levels
        * - E_ERROR | E_WARNING = Set the handling for errors and warnings
        * - E_ALL ^ E_ERROR = Set the handling for all levels except errors
        *
        * @static
        * @access        public
        * @param        int                $level                The error level for which to set the error handling
        * @param        string        $mode                The mode to use for the error handling.
        * @param        mixed        $options        Optional: Any options needed for the given mode.
        * @return        mixed        $result                True on success, or a patError object if failed.
        * @see                getErrorHandling()
        */
    function setErrorHandling( $level, $mode, $options = null )
    {
                $levels        =        $GLOBALS['_pat_errorLevels'];

                $function        =        'handleError' . ucfirst( $mode );
                if( !is_callable( array( 'patErrorManager', $function ) ) )
                {
                        return patErrorManager::raiseError( E_ERROR,
                                                                                                'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_MODE,
                                                                                                'Error Handling mode is not knwon',
                                                                                                'Mode: ' .  $mode . ' is not implemented.'
                                                                                                );
                }

                foreach( $levels as $eLevel => $eTitle )
                {
                        if( ( $level & $eLevel ) != $eLevel )
                        {
                                continue;
                        }

                        // set callback options
                        if( $mode == 'callback' )
                        {
                                if( !is_array( $options ) )
                                {
                                        return patErrorManager::raiseError( E_ERROR,
                                                                                                                'patErrorManager:' . PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS,
                                                                                                                'Options for callback not valid'
                                                                                                                );
                                }

                                if( !is_callable( $options ) )
                                {
                                        $tmp        =        array( 'GLOBAL' );
                                        if( is_array( $options ) )
                                        {
                                                $tmp[0]        =        $options[0];
                                                $tmp[1]        =        $options[1];
                                        }
                                        else
                                        {
                                                $tmp[1]        =        $options;
                                        }

                                        return patErrorManager::raiseError(        E_ERROR,
                                                                                                                'patErrorManager:' . PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE,
                                                                                                                'Function is not callable',
                                                                                                                'Function:' . $tmp[1]  . ' scope ' . $tmp[0] . '.'
                                                                                                                );
                                }
                        }


                        // save settings
                        $GLOBALS['_pat_errorHandling'][$eLevel]        =        array( 'mode' => $mode );
                        if( $options        != null )
                        {
                                $GLOBALS['_pat_errorHandling'][$eLevel]['options']        =        $options;
                        }
                }

        return  true;
    }

   /**
        * retrieves the current error handling settings for the specified error level.
        *
        * @access        public
        * @param        int                $level                The error level to retrieve. This can be any of PHP's own error levels, e.g. E_ALL, E_NOTICE...
        * @return        array        $handling        All error handling details
        */
    function getErrorHandling( $level )
    {
                return $GLOBALS['_pat_errorHandling'][$level];
    }

   /**
        * translate an error level
        *
        * returns the human-readable name for an error level,
        * e.g. E_ERROR will be translated to 'Error'.
        *
        * @access        public
        * @param        integer                error level
        * @return        string                human-readable representation
        */
        function translateErrorLevel( $level )
        {
                if( isset( $GLOBALS['_pat_errorLevels'][$level] ) )
                {
                        return        $GLOBALS['_pat_errorLevels'][$level];
                }
                return        'Unknown error level';
        }

   /**
        * setErrorClass
        *
        * In order to autoload this class, the filename containing that class must be
        * named like the class itself; with an appending ".php". Although the file must be stored
        * in the same directory as patErrorManager.php (this file)
        *
        * @access public
        * @param string $name classname
        * @return boolean $result true on success
        */
    function setErrorClass( $name )
    {
                // include old error-class
                if( $name !== $GLOBALS['_pat_errorClass'] && !class_exists( $GLOBALS['_pat_errorClass'] ) )
                {
                        include_once dirname( __FILE__ ) . '/' . $GLOBALS['_pat_errorClass'] . '.php';
                }

                $GLOBALS['_pat_errorClass']        =        $name;
                return true;
    }

   /**
        * add error codes to be ingored
        *
        * @static
        * @access public
        * @param mixed $codes either an array of error code or a single code that will be ignored in future
        * @return boolean $result true on success
        */
    function addIgnore( $codes )
    {
                if( !is_array( $codes ) )
                {
                        $codes        =        array( $codes );
                }

                $codes                                                        =        array_merge( $GLOBALS['_pat_errorIgnores'], $codes );
                $GLOBALS['_pat_errorIgnores']        =        array_unique( $codes );

                return true;
    }

   /**
        * removeIgnore
        *
        *
        * @static
        * @access public
        * @return boolean $result true on success
        */
    function removeIgnore( $codes )
    {
                if( !is_array( $codes ) )
                {
                        $codes        =        array( $codes );
                }

                foreach( $codes as $code )
                {
                        $index        =        array_search( $code, $GLOBALS['_pat_errorIgnores'] );
                        if( $index === false )
                        {
                                continue;
                        }

                        unset( $GLOBALS['_pat_errorIgnores'][$index] );
                }

                // reorder the codes
                $GLOBALS['_pat_errorIgnores']        =        array_values( $GLOBALS['_pat_errorIgnores'] );

                return true;
    }

   /**
        * recieve all registerd error codes that will be ignored
        *
        * @static
        * @access public
        * @return array $codes list of error codes
        */
    function getIgnore()
    {
                return $GLOBALS['_pat_errorIgnores'];
    }

   /**
        * empty list of errors to be ignored
        *
        * @static
        * @access public
        * @return boolean $result true on success
        */
    function clearIgnore()
    {
                $GLOBALS['_pat_errorIgnores']        =        array();
                return true;
    }

   /**
        * add expected errors to stack
        *
        * @static
        * @access public
        * @param mixed $codes either an array of error code or a single code that will be ignored in future
        * @return boolean $result true on success
        */
    function pushExpect( $codes )
    {
                if( !is_array( $codes ) )
                {
                        $codes        =        array( $codes );
                }

                array_push( $GLOBALS['_pat_errorExpects'], $codes );

                return true;
    }

   /**
        * remove top of error-codes from stack
        *
        * @static
        * @access public
        * @return boolean $result true on success
        */
    function popExpect()
    {
                if( empty( $GLOBALS['_pat_errorExpects'] ) )
                {
                        return false;
                }

                array_pop( $GLOBALS['_pat_errorExpects'] );
                return true;
    }

   /**
        * recieve all registerd error codes that will be ignored
        *
        * @static
        * @access public
        * @return array $codes list of error codes
        */
    function getExpect()
    {
                return $GLOBALS['_pat_errorExpects'];
    }

   /**
        * empty list of errors to be ignored
        *
        * @static
        * @access public
        * @return boolean $result true on success
        */
    function clearExpect()
    {
                $GLOBALS['_pat_errorExpects']        =        array();
                return true;
    }

   /**
        * handleError: Ignore
        * Does nothing
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorIgnore( &$error, $options )
    {
                return $error;
    }

   /**
        * handleError: Echo
        * display error message
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorEcho( &$error, $options )
    {
                $level_human        =        patErrorManager::translateErrorLevel( $error->getLevel() );

                if( isset( $_SERVER['HTTP_HOST'] ) )
                {
                        // output as html
                        echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n";
                }
                else
                {
                        // output as simple text
                        if( defined( 'STDERR' ) )
                        {
                                fwrite( STDERR, "pat-$level_human: " . $error->getMessage() . "\n" );
                        }
                        else
                        {
                                echo "pat-$level_human: " . $error->getMessage() . "\n";
                        }
                }
                return $error;
    }

   /**
        * handleError: Verbose
        * display verbose output for developing purpose
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorVerbose( &$error, $options )
    {
                $level_human        =        patErrorManager::translateErrorLevel( $error->getLevel() );
                $info                        =        $error->getInfo();

                if( isset( $_SERVER['HTTP_HOST'] ) )
                {
                        // output as html
                        echo "<br /><b>pat-$level_human</b>: " . $error->getMessage() . "<br />\n";
                        if( $info != null )
                        {
                                echo "&nbsp;&nbsp;&nbsp;" . $error->getInfo() . "<br />\n";
                        }
                }
                else
                {
                        // output as simple text
                        echo "pat-$level_human: " . $error->getMessage() . "\n";
                        if( $info != null )
                        {
                                echo "    " . $error->getInfo() . "\n";
                        }
                }
                return $error;
    }

   /**
        * handleError: die
        * display error-message and die
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorDie( &$error, $options )
    {
                $level_human        =        patErrorManager::translateErrorLevel( $error->getLevel() );

                if( isset( $_SERVER['HTTP_HOST'] ) )
                {
                        // output as html
                        die( "<br /><b>pat-$level_human</b> " . $error->getMessage() . "<br />\n" );
                }
                else
                {
                        // output as simple text
                        if( defined( 'STDERR' ) )
                        {
                                fwrite( STDERR, "pat-$level_human " . $error->getMessage() . "\n" );
                        }
                        else
                        {
                                die( "pat-$level_human " . $error->getMessage() . "\n" );
                        }
                }
                return $error;
    }

   /**
        * handleError: trigger
        * trigger php-error
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorTrigger( &$error, $options )
    {
                switch( $error->getLevel() )
                {
                        case        E_NOTICE:
                                $level        =        E_USER_NOTICE;
                                break;
                        case        E_WARNING:
                                $level        =        E_USER_WARNING;
                                break;
                        case        E_NOTICE:
                                $level =        E_NOTICE;
                                break;
                        default:
                                $level        =        E_USER_ERROR;
                                break;
                }

                trigger_error( $error->getMessage(), $level );
                return $error;
    }

   /**
        * handleError: callback
        * forward error to custom handler
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return object $error error-object
        * @see raise()
        */
    function &handleErrorCallback( &$error, $options )
    {
                $opt        =        $options['options'];
                return call_user_func( $opt, $error );
    }
   /**
        * handleError: throw an exception
        *
        * @access private
        * @param object $error patError-Object
        * @param array $options options for handler
        * @return null
        * @throws Exception
        * @see raise()
        */
    function &handleErrorException(&$error, $options)
    {
        if (isset($options['options'])) {
            $exception = $options['options'];
        } else {
                $exception = 'Exception';
        }
        $code = 'throw new '.$exception.'("'.$error->getMessage().'");';
        eval($code);
    }
}
?>