Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: patError

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /pattemplate/patErrorManager.php

Documentation is available at patErrorManager.php

  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 47 2005-09-15 02:55:27Z rhuk $
  8.  *
  9.  * @package    patError
  10.  */
  11.  
  12. /**
  13.  * error definition: illegal options.
  14.  */
  15. define'PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS');
  16.  
  17. /**
  18.  * error definition: callback function does not exist.
  19.  */
  20. define'PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE');
  21.  
  22. /**
  23.  * error definition: illegal error handling mode.
  24.  */
  25. define'PATERRORMANAGER_ERROR_ILLEGAL_MODE');
  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 <[email protected]>
  75.  * @author    Stephan Schmidt <[email protected]>
  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. {
  82.    /**
  83.     * method for checking whether the return value of a pat application method is a pat
  84.     * error object.
  85.     *
  86.     * @static
  87.     * @access    public
  88.     * @param    mixed    &$object 
  89.     * @return    boolean $result    True if argument is a patError-object, false otherwise.
  90.     */
  91.     function isError&$object )
  92.     {
  93.         if!is_object$object ) )
  94.         {
  95.             return false;
  96.         }
  97.  
  98.  
  99.  
  100.         ifstrtolower(get_class$object )) != strtolower$GLOBALS['_pat_errorClass'&& !is_subclass_of$object$GLOBALS['_pat_errorClass') )
  101.         {
  102.             return false;
  103.         }
  104.  
  105.         return  true;
  106.     }
  107.  
  108.    /**
  109.     * wrapper for the {@link raise()} method where you do not have to specify the
  110.     * error level - a {@link patError} object with error level E_ERROR will be returned.
  111.     *
  112.     * @static
  113.     * @access    public
  114.     * @param    string    $code    The application-internal error code for this error
  115.     * @param    string    $msg    The error message, which may also be shown the user if need be.
  116.     * @param    mixed    $info    Optional: Additional error information (usually only developer-relevant information that the user should never see, like a database DSN).
  117.     * @return    object    $error    The configured patError object
  118.     * @see        raise()
  119.     * @see        patError
  120.     */
  121.     function &raiseError$code$msg$info null )
  122.     {
  123.         return patErrorManager::raiseE_ERROR$code$msg$info );
  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::raiseE_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::raiseE_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.         ifin_array$code$GLOBALS['_pat_errorIgnores') )
  179.         {
  180.             return false;
  181.         }
  182.  
  183.         // this error was expected
  184.         if!empty$GLOBALS['_pat_errorExpects') )
  185.         {
  186.             $expected =    array_pop$GLOBALS['_pat_errorExpects');
  187.             ifin_array$code$expected ) )
  188.             {
  189.                 return false;
  190.             }
  191.         }
  192.  
  193.         // need patError
  194.         $class    =    $GLOBALS['_pat_errorClass'];
  195.         if!class_exists$class ) )
  196.         {
  197.             include_once dirname__FILE__ '/'$class .'.php';
  198.         }
  199.  
  200.         // build error object
  201.         $error            =&    new    $class$level$code$msg$info );
  202.  
  203.         // see what to do with this kind of error
  204.         $handling    =    patErrorManager::getErrorHandling$level );
  205.  
  206.         $function    =    'handleError' ucfirst$handling['mode');
  207.         if (is_callablearray'patErrorManager'$function ) )) {
  208.             return patErrorManager::$function$error$handling );
  209.         else {
  210.             // This is required to prevent a very unhelpful white-screen-of-death
  211.             die(
  212.                 'JError::raise -> Static method JError::' $function ' does not exist.' .
  213.                 ' Contact a developer to debug' .
  214.                 '<br/><strong>Error was</strong> ' .
  215.                 '<br/>' $error->getMessage()
  216.             );
  217.         }
  218.     }
  219.  
  220.    /**
  221.     * register a new error level
  222.     *
  223.     * This allows you to add custom error levels to the built-in
  224.     * - E_NOTICE
  225.     * - E_WARNING
  226.     * - E_NOTICE
  227.     *
  228.     * You may use this level in subsequent calls to raise().
  229.     * Error handling will be set to 'ignore' for the new level, you
  230.     * may change it by using setErrorHandling().
  231.     *
  232.     * You could be using PHP's predefined constants for error levels
  233.     * or any other integer value.
  234.     *
  235.     * @access    public
  236.     * @param    integer    error level
  237.     * @param    string    human-readable name
  238.     * @return    boolean    true on success; false if the level already has been registered
  239.     * @see        raise(), setErrorHandling()
  240.     * @link        http://www.php.net/manual/en/function.error-reporting.php
  241.     */
  242.     function registerErrorLevel$level$name )
  243.     {
  244.         ifisset$GLOBALS['_pat_errorLevels'][$level) )
  245.         {
  246.             return false;
  247.         }
  248.         $GLOBALS['_pat_errorLevels'][$level]    =    $name;
  249.         patErrorManager::setErrorHandling$level'ignore' );
  250.         return    true;
  251.     }
  252.  
  253.    /**
  254.     * sets the way the patErrorManager will handle teh different error levels. Use this
  255.     * if you want to override the default settings.
  256.     *
  257.     * Error handling modes:
  258.     * - ignore
  259.     * - trigger
  260.     * - verbose
  261.     * - echo
  262.     * - callback
  263.     * - die
  264.     *
  265.     * You may also set the error handling for several modes at once using PHP's bit operations.
  266.     * Examples:
  267.     * - E_ALL = Set the handling for all levels
  268.     * - E_ERROR | E_WARNING = Set the handling for errors and warnings
  269.     * - E_ALL ^ E_ERROR = Set the handling for all levels except errors
  270.     *
  271.     * @static
  272.     * @access    public
  273.     * @param    int        $level        The error level for which to set the error handling
  274.     * @param    string    $mode        The mode to use for the error handling.
  275.     * @param    mixed    $options    Optional: Any options needed for the given mode.
  276.     * @return    mixed    $result        True on success, or a patError object if failed.
  277.     * @see        getErrorHandling()
  278.     */
  279.     function setErrorHandling$level$mode$options null )
  280.     {
  281.         $levels    =    $GLOBALS['_pat_errorLevels'];
  282.  
  283.         $function    =    'handleError' ucfirst$mode );
  284.         if!is_callablearray'patErrorManager'$function ) ) )
  285.         {
  286.             return patErrorManager::raiseErrorE_ERROR,
  287.                                                 'patErrorManager:' PATERRORMANAGER_ERROR_ILLEGAL_MODE,
  288.                                                 'Error Handling mode is not knwon',
  289.                                                 'Mode: ' .  $mode ' is not implemented.'
  290.                                                 );
  291.         }
  292.  
  293.         foreach$levels as $eLevel => $eTitle )
  294.         {
  295.             if( ( $level $eLevel != $eLevel )
  296.             {
  297.                 continue;
  298.             }
  299.  
  300.             // set callback options
  301.             if$mode == 'callback' )
  302.             {
  303.                 if!is_array$options ) )
  304.                 {
  305.                     return patErrorManager::raiseErrorE_ERROR,
  306.                                                         'patErrorManager:' PATERRORMANAGER_ERROR_ILLEGAL_OPTIONS,
  307.                                                         'Options for callback not valid'
  308.                                                         );
  309.                 }
  310.  
  311.                 if!is_callable$options ) )
  312.                 {
  313.                     $tmp    =    array'GLOBAL' );
  314.                     ifis_array$options ) )
  315.                     {
  316.                         $tmp[0]    =    $options[0];
  317.                         $tmp[1]    =    $options[1];
  318.                     }
  319.                     else
  320.                     {
  321.                         $tmp[1]    =    $options;
  322.                     }
  323.  
  324.                     return patErrorManager::raiseError(    E_ERROR,
  325.                                                         'patErrorManager:' PATERRORMANAGER_ERROR_CALLBACK_NOT_CALLABLE,
  326.                                                         'Function is not callable',
  327.                                                         'Function:' $tmp[1]  ' scope ' $tmp[0'.'
  328.                                                         );
  329.                 }
  330.             }
  331.  
  332.  
  333.             // save settings
  334.             $GLOBALS['_pat_errorHandling'][$eLevel]    =    array'mode' => $mode );
  335.             if$options    != null )
  336.             {
  337.                 $GLOBALS['_pat_errorHandling'][$eLevel]['options']    =    $options;
  338.             }
  339.         }
  340.  
  341.         return  true;
  342.     }
  343.  
  344.    /**
  345.     * retrieves the current error handling settings for the specified error level.
  346.     *
  347.     * @access    public
  348.     * @param    int        $level        The error level to retrieve. This can be any of PHP's own error levels, e.g. E_ALL, E_NOTICE...
  349.     * @return    array    $handling    All error handling details
  350.     */
  351.     function getErrorHandling$level )
  352.     {
  353.         return $GLOBALS['_pat_errorHandling'][$level];
  354.     }
  355.  
  356.    /**
  357.     * translate an error level
  358.     *
  359.     * returns the human-readable name for an error level,
  360.     * e.g. E_ERROR will be translated to 'Error'.
  361.     *
  362.     * @access    public
  363.     * @param    integer        error level
  364.     * @return    string        human-readable representation
  365.     */
  366.     function translateErrorLevel$level )
  367.     {
  368.         ifisset$GLOBALS['_pat_errorLevels'][$level) )
  369.         {
  370.             return    $GLOBALS['_pat_errorLevels'][$level];
  371.         }
  372.         return    'Unknown error level';
  373.     }
  374.  
  375.    /**
  376.     * setErrorClass
  377.     *
  378.     * In order to autoload this class, the filename containing that class must be
  379.     * named like the class itself; with an appending ".php". Although the file must be stored
  380.     * in the same directory as patErrorManager.php (this file)
  381.     *
  382.     * @access public
  383.     * @param string $name classname
  384.     * @return boolean $result true on success
  385.     */
  386.     function setErrorClass$name )
  387.     {
  388.         // include old error-class
  389.         if$name !== $GLOBALS['_pat_errorClass'&& !class_exists$GLOBALS['_pat_errorClass') )
  390.         {
  391.             include_once dirname__FILE__ '/' $GLOBALS['_pat_errorClass''.php';
  392.         }
  393.  
  394.         $GLOBALS['_pat_errorClass']    =    $name;
  395.         return true;
  396.     }
  397.  
  398.    /**
  399.     * add error codes to be ingored
  400.     *
  401.     * @static
  402.     * @access public
  403.     * @param mixed $codes either an array of error code or a single code that will be ignored in future
  404.     * @return boolean $result true on success
  405.     */
  406.     function addIgnore$codes )
  407.     {
  408.         if!is_array$codes ) )
  409.         {
  410.             $codes    =    array$codes );
  411.         }
  412.  
  413.         $codes                            =    array_merge$GLOBALS['_pat_errorIgnores']$codes );
  414.         $GLOBALS['_pat_errorIgnores']    =    array_unique$codes );
  415.  
  416.         return true;
  417.     }
  418.  
  419.    /**
  420.     * removeIgnore
  421.     *
  422.     *
  423.     * @static
  424.     * @access public
  425.     * @return boolean $result true on success
  426.     */
  427.     function removeIgnore$codes )
  428.     {
  429.         if!is_array$codes ) )
  430.         {
  431.             $codes    =    array$codes );
  432.         }
  433.  
  434.         foreach$codes as $code )
  435.         {
  436.             $index    =    array_search$code$GLOBALS['_pat_errorIgnores');
  437.             if$index === false )
  438.             {
  439.                 continue;
  440.             }
  441.  
  442.             unset$GLOBALS['_pat_errorIgnores'][$index);
  443.         }
  444.  
  445.         // reorder the codes
  446.         $GLOBALS['_pat_errorIgnores']    =    array_values$GLOBALS['_pat_errorIgnores');
  447.  
  448.         return true;
  449.     }
  450.  
  451.    /**
  452.     * recieve all registerd error codes that will be ignored
  453.     *
  454.     * @static
  455.     * @access public
  456.     * @return array $codes list of error codes
  457.     */
  458.     function getIgnore()
  459.     {
  460.         return $GLOBALS['_pat_errorIgnores'];
  461.     }
  462.  
  463.    /**
  464.     * empty list of errors to be ignored
  465.     *
  466.     * @static
  467.     * @access public
  468.     * @return boolean $result true on success
  469.     */
  470.     function clearIgnore()
  471.     {
  472.         $GLOBALS['_pat_errorIgnores']    =    array();
  473.         return true;
  474.     }
  475.  
  476.    /**
  477.     * add expected errors to stack
  478.     *
  479.     * @static
  480.     * @access public
  481.     * @param mixed $codes either an array of error code or a single code that will be ignored in future
  482.     * @return boolean $result true on success
  483.     */
  484.     function pushExpect$codes )
  485.     {
  486.         if!is_array$codes ) )
  487.         {
  488.             $codes    =    array$codes );
  489.         }
  490.  
  491.         array_push$GLOBALS['_pat_errorExpects']$codes );
  492.  
  493.         return true;
  494.     }
  495.  
  496.    /**
  497.     * remove top of error-codes from stack
  498.     *
  499.     * @static
  500.     * @access public
  501.     * @return boolean $result true on success
  502.     */
  503.     function popExpect()
  504.     {
  505.         ifempty$GLOBALS['_pat_errorExpects') )
  506.         {
  507.             return false;
  508.         }
  509.  
  510.         array_pop$GLOBALS['_pat_errorExpects');
  511.         return true;
  512.     }
  513.  
  514.    /**
  515.     * recieve all registerd error codes that will be ignored
  516.     *
  517.     * @static
  518.     * @access public
  519.     * @return array $codes list of error codes
  520.     */
  521.     function getExpect()
  522.     {
  523.         return $GLOBALS['_pat_errorExpects'];
  524.     }
  525.  
  526.    /**
  527.     * empty list of errors to be ignored
  528.     *
  529.     * @static
  530.     * @access public
  531.     * @return boolean $result true on success
  532.     */
  533.     function clearExpect()
  534.     {
  535.         $GLOBALS['_pat_errorExpects']    =    array();
  536.         return true;
  537.     }
  538.  
  539.    /**
  540.     * handleError: Ignore
  541.     * Does nothing
  542.     *
  543.     * @access private
  544.     * @param object $error patError-Object
  545.     * @param array $options options for handler
  546.     * @return object $error error-object
  547.     * @see raise()
  548.     */
  549.     function &handleErrorIgnore&$error$options )
  550.     {
  551.         return $error;
  552.     }
  553.  
  554.    /**
  555.     * handleError: Echo
  556.     * display error message
  557.     *
  558.     * @access private
  559.     * @param object $error patError-Object
  560.     * @param array $options options for handler
  561.     * @return object $error error-object
  562.     * @see raise()
  563.     */
  564.     function &handleErrorEcho&$error$options )
  565.     {
  566.         $level_human    =    patErrorManager::translateErrorLevel$error->getLevel() );
  567.  
  568.         ifisset$_SERVER['HTTP_HOST') )
  569.         {
  570.             // output as html
  571.             echo "<br /><b>pat-$level_human</b>: $error->getMessage("<br />\n";
  572.         }
  573.         else
  574.         {
  575.             // output as simple text
  576.             ifdefined'STDERR' ) )
  577.             {
  578.                 fwriteSTDERR"pat-$level_human$error->getMessage("\n" );
  579.             }
  580.             else
  581.             {
  582.                 echo "pat-$level_human$error->getMessage("\n";
  583.             }
  584.         }
  585.         return $error;
  586.     }
  587.  
  588.    /**
  589.     * handleError: Verbose
  590.     * display verbose output for developing purpose
  591.     *
  592.     * @access private
  593.     * @param object $error patError-Object
  594.     * @param array $options options for handler
  595.     * @return object $error error-object
  596.     * @see raise()
  597.     */
  598.     function &handleErrorVerbose&$error$options )
  599.     {
  600.         $level_human    =    patErrorManager::translateErrorLevel$error->getLevel() );
  601.         $info            =    $error->getInfo();
  602.  
  603.         ifisset$_SERVER['HTTP_HOST') )
  604.         {
  605.             // output as html
  606.             echo "<br /><b>pat-$level_human</b>: $error->getMessage("<br />\n";
  607.             if$info != null )
  608.             {
  609.                 echo "&nbsp;&nbsp;&nbsp;" $error->getInfo("<br />\n";
  610.             }
  611.             echo $error->getBacktracetrue );
  612.         }
  613.         else
  614.         {
  615.             // output as simple text
  616.             echo "pat-$level_human$error->getMessage("\n";
  617.             if$info != null )
  618.             {
  619.                 echo "    " $error->getInfo("\n";
  620.             }
  621.  
  622.         }
  623.         return $error;
  624.     }
  625.  
  626.    /**
  627.     * handleError: die
  628.     * display error-message and die
  629.     *
  630.     * @access private
  631.     * @param object $error patError-Object
  632.     * @param array $options options for handler
  633.     * @return object $error error-object
  634.     * @see raise()
  635.     */
  636.     function &handleErrorDie&$error$options )
  637.     {
  638.         $level_human    =    patErrorManager::translateErrorLevel$error->getLevel() );
  639.  
  640.         ifisset$_SERVER['HTTP_HOST') )
  641.         {
  642.             // output as html
  643.             die"<br /><b>pat-$level_human</b$error->getMessage("<br />\n" );
  644.         }
  645.         else
  646.         {
  647.             // output as simple text
  648.             ifdefined'STDERR' ) )
  649.             {
  650.                 fwriteSTDERR"pat-$level_human $error->getMessage("\n" );
  651.             }
  652.             else
  653.             {
  654.                 die"pat-$level_human $error->getMessage("\n" );
  655.             }
  656.         }
  657.         return $error;
  658.     }
  659.  
  660.    /**
  661.     * handleError: trigger
  662.     * trigger php-error
  663.     *
  664.     * @access private
  665.     * @param object $error patError-Object
  666.     * @param array $options options for handler
  667.     * @return object $error error-object
  668.     * @see raise()
  669.     */
  670.     function &handleErrorTrigger&$error$options )
  671.     {
  672.         switch$error->getLevel() )
  673.         {
  674.             case    E_NOTICE:
  675.                 $level    =    E_USER_NOTICE;
  676.                 break;
  677.             case    E_WARNING:
  678.                 $level    =    E_USER_WARNING;
  679.                 break;
  680.             case    E_NOTICE:
  681.                 $level =    E_NOTICE;
  682.                 break;
  683.             default:
  684.                 $level    =    E_USER_ERROR;
  685.                 break;
  686.         }
  687.  
  688.         trigger_error$error->getMessage()$level );
  689.         return $error;
  690.     }
  691.  
  692.    /**
  693.     * handleError: callback
  694.     * forward error to custom handler
  695.     *
  696.     * @access private
  697.     * @param object $error patError-Object
  698.     * @param array $options options for handler
  699.     * @return object $error error-object
  700.     * @see raise()
  701.     */
  702.     function &handleErrorCallback&$error$options )
  703.     {
  704.         $opt    $options['options'];
  705.         $result &call_user_func$opt$error );
  706.         return $result;
  707.     }
  708. }
  709. ?>

Documentation generated on Mon, 05 Mar 2007 21:13:46 +0000 by phpDocumentor 1.3.1