Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: PEAR

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 /pear/PEAR.php

Documentation is available at PEAR.php

  1. <?php
  2. /**
  3.  * PEAR, the PHP Extension and Application Repository
  4.  *
  5.  * PEAR class and PEAR_Error class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  10.  * that is available through the world-wide-web at the following URI:
  11.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  12.  * the PHP License and are unable to obtain it through the web, please
  13.  * send a note to [email protected] so we can mail you a copy immediately.
  14.  *
  15.  * @category   pear
  16.  * @package    PEAR
  17.  * @author     Sterling Hughes <[email protected]>
  18.  * @author     Stig Bakken <[email protected]>
  19.  * @author     Tomas V.V.Cox <[email protected]>
  20.  * @author     Greg Beaver <[email protected]>
  21.  * @copyright  1997-2006 The PHP Group
  22.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  23.  * @version    CVS: $Id: PEAR.php 4522 2006-08-15 00:35:07Z eddiea $
  24.  * @link       http://pear.php.net/package/PEAR
  25.  * @since      File available since Release 0.1
  26.  */
  27.  
  28. /**#@+
  29.  * ERROR constants
  30.  */
  31. define('PEAR_ERROR_RETURN',     1);
  32. define('PEAR_ERROR_PRINT',      2);
  33. define('PEAR_ERROR_TRIGGER',    4);
  34. define('PEAR_ERROR_DIE',        8);
  35. define('PEAR_ERROR_CALLBACK',  16);
  36. /**
  37.  * WARNING: obsolete
  38.  * @deprecated
  39.  */
  40. define('PEAR_ERROR_EXCEPTION'32);
  41. /**#@-*/
  42. define('PEAR_ZE2'(function_exists('version_compare'&&
  43.                     version_compare(zend_version()"2-dev""ge")));
  44.  
  45. if (substr(PHP_OS03== 'WIN'{
  46.     define('OS_WINDOWS'true);
  47.     define('OS_UNIX',    false);
  48.     define('PEAR_OS',    'Windows');
  49. else {
  50.     define('OS_WINDOWS'false);
  51.     define('OS_UNIX',    true);
  52.     define('PEAR_OS',    'Unix')// blatant assumption
  53. }
  54.  
  55. // instant backwards compatibility
  56. if (!defined('PATH_SEPARATOR')) {
  57.     if (OS_WINDOWS{
  58.         define('PATH_SEPARATOR'';');
  59.     else {
  60.         define('PATH_SEPARATOR'':');
  61.     }
  62. }
  63.  
  64. $GLOBALS['_PEAR_default_error_mode']     PEAR_ERROR_RETURN;
  65. $GLOBALS['_PEAR_default_error_options']  E_USER_NOTICE;
  66. $GLOBALS['_PEAR_destructor_object_list'array();
  67. $GLOBALS['_PEAR_shutdown_funcs']         array();
  68. $GLOBALS['_PEAR_error_handler_stack']    array();
  69.  
  70. @ini_set('track_errors'true);
  71.  
  72.  * Base class for other PEAR classes.  Provides rudimentary
  73.  * emulation of destructors.
  74.  *
  75.  * If you want a destructor in your class, inherit PEAR and make a
  76.  * destructor method called _yourclassname (same name as the
  77.  * constructor, but with a "_" prefix).  Also, in your constructor you
  78.  * have to call the PEAR constructor: $this->PEAR();.
  79.  * The destructor method will be called without parameters.  Note that
  80.  * at in some SAPI implementations (such as Apache), any output during
  81.  * the request shutdown (in which destructors are called) seems to be
  82.  * discarded.  If you need to get any debug information from your
  83.  * destructor, use error_log(), syslog() or something similar.
  84.  *
  85.  * IMPORTANT! To use the emulated destructors you need to create the
  86.  * objects by reference: $obj =& new PEAR_child;
  87.  *
  88.  * @category   pear
  89.  * @package    PEAR
  90.  * @author     Stig Bakken <[email protected]>
  91.  * @author     Tomas V.V. Cox <[email protected]>
  92.  * @author     Greg Beaver <[email protected]>
  93.  * @copyright  1997-2006 The PHP Group
  94.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  95.  * @version    Release: 1.4.10
  96.  * @link       http://pear.php.net/package/PEAR
  97.  * @see        PEAR_Error
  98.  * @since      Class available since PHP 4.0.2
  99.  * @link        http://pear.php.net/manual/en/core.pear.php#core.pear.pear
  100.  */
  101. class PEAR
  102. {
  103.     // {{{ properties
  104.  
  105.     
  106.     /**
  107.      * Whether to enable internal debug messages.
  108.      *
  109.      * @var     bool 
  110.      * @access  private
  111.      */
  112.     var $_debug false;
  113.  
  114.     /**
  115.      * Default error mode for this object.
  116.      *
  117.      * @var     int 
  118.      * @access  private
  119.      */
  120.     var $_default_error_mode null;
  121.  
  122.     /**
  123.      * Default error options used for this object when error mode
  124.      * is PEAR_ERROR_TRIGGER.
  125.      *
  126.      * @var     int 
  127.      * @access  private
  128.      */
  129.     var $_default_error_options null;
  130.  
  131.     /**
  132.      * Default error handler (callback) for this object, if error mode is
  133.      * PEAR_ERROR_CALLBACK.
  134.      *
  135.      * @var     string 
  136.      * @access  private
  137.      */
  138.     var $_default_error_handler '';
  139.  
  140.     /**
  141.      * Which class to use for error objects.
  142.      *
  143.      * @var     string 
  144.      * @access  private
  145.      */
  146.     var $_error_class 'PEAR_Error';
  147.  
  148.     /**
  149.      * An array of expected errors.
  150.      *
  151.      * @var     array 
  152.      * @access  private
  153.      */
  154.     var $_expected_errors array();
  155.  
  156.     // }}}
  157.  
  158.     // {{{ constructor
  159.  
  160.     
  161.     /**
  162.      * Constructor.  Registers this object in
  163.      * $_PEAR_destructor_object_list for destructor emulation if a
  164.      * destructor object exists.
  165.      *
  166.      * @param string $error_class  (optional) which class to use for
  167.      *         error objects, defaults to PEAR_Error.
  168.      * @access public
  169.      * @return void 
  170.      */
  171.     function PEAR($error_class null)
  172.     {
  173.         $classname strtolower(get_class($this));
  174.         if ($this->_debug{
  175.             print "PEAR constructor calledclass=$classname\n";
  176.         }
  177.         if ($error_class !== null{
  178.             $this->_error_class $error_class;
  179.         }
  180.         while ($classname && strcasecmp($classname"pear")) {
  181.             $destructor "_$classname";
  182.             if (method_exists($this$destructor)) {
  183.                 global $_PEAR_destructor_object_list;
  184.                 $_PEAR_destructor_object_list[&$this;
  185.                 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  186.                     register_shutdown_function("_PEAR_call_destructors");
  187.                     $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'true;
  188.                 }
  189.                 break;
  190.             else {
  191.                 $classname get_parent_class($classname);
  192.             }
  193.         }
  194.     }
  195.  
  196.     // }}}
  197.     // {{{ destructor
  198.  
  199.     
  200.     /**
  201.      * Destructor (the emulated type of...).  Does nothing right now,
  202.      * but is included for forward compatibility, so subclass
  203.      * destructors should always call it.
  204.      *
  205.      * See the note in the class desciption about output from
  206.      * destructors.
  207.      *
  208.      * @access public
  209.      * @return void 
  210.      */
  211.     function _PEAR({
  212.         if ($this->_debug{
  213.             printf("PEAR destructor called, class=%s\n"strtolower(get_class($this)));
  214.         }
  215.     }
  216.  
  217.     // }}}
  218.     // {{{ getStaticProperty()
  219.  
  220.     
  221.     /**
  222.     * If you have a class that's mostly/entirely static, and you need static
  223.     * properties, you can use this method to simulate them. Eg. in your method(s)
  224.     * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
  225.     * You MUST use a reference, or they will not persist!
  226.     *
  227.     * @access public
  228.     * @param  string $class  The calling classname, to prevent clashes
  229.     * @param  string $var    The variable to retrieve.
  230.     * @return mixed   A reference to the variable. If not set it will be
  231.     *                  auto initialised to NULL.
  232.     */
  233.     function &getStaticProperty($class$var)
  234.     {
  235.         static $properties;
  236.         return $properties[$class][$var];
  237.     }
  238.  
  239.     // }}}
  240.     // {{{ registerShutdownFunc()
  241.  
  242.     
  243.     /**
  244.     * Use this function to register a shutdown method for static
  245.     * classes.
  246.     *
  247.     * @access public
  248.     * @param  mixed $func  The function name (or array of class/method) to call
  249.     * @param  mixed $args  The arguments to pass to the function
  250.     * @return void 
  251.     */
  252.     function registerShutdownFunc($func$args array())
  253.     {
  254.         // if we are called statically, there is a potential
  255.         // that no shutdown func is registered.  Bug #6445
  256.         if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  257.             register_shutdown_function("_PEAR_call_destructors");
  258.             $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'true;
  259.         }
  260.         $GLOBALS['_PEAR_shutdown_funcs'][array($func$args);
  261.     }
  262.  
  263.     // }}}
  264.     // {{{ isError()
  265.  
  266.     
  267.     /**
  268.      * Tell whether a value is a PEAR error.
  269.      *
  270.      * @param   mixed $data   the value to test
  271.      * @param   int   $code   if $data is an error object, return true
  272.      *                         only if $code is a string and
  273.      *                         $obj->getMessage() == $code or
  274.      *                         $code is an integer and $obj->getCode() == $code
  275.      * @access  public
  276.      * @return  bool    true if parameter is an error
  277.      */
  278.     function isError($data$code null)
  279.     {
  280.         if (is_a($data'PEAR_Error')) {
  281.             if (is_null($code)) {
  282.                 return true;
  283.             elseif (is_string($code)) {
  284.                 return $data->getMessage(== $code;
  285.             else {
  286.                 return $data->getCode(== $code;
  287.             }
  288.         }
  289.         return false;
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ setErrorHandling()
  294.  
  295.     
  296.     /**
  297.      * Sets how errors generated by this object should be handled.
  298.      * Can be invoked both in objects and statically.  If called
  299.      * statically, setErrorHandling sets the default behaviour for all
  300.      * PEAR objects.  If called in an object, setErrorHandling sets
  301.      * the default behaviour for that object.
  302.      *
  303.      * @param int $mode 
  304.      *         One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  305.      *         PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  306.      *         PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  307.      *
  308.      * @param mixed $options 
  309.      *         When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  310.      *         of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  311.      *
  312.      *         When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  313.      *         to be the callback function or method.  A callback
  314.      *         function is a string with the name of the function, a
  315.      *         callback method is an array of two elements: the element
  316.      *         at index 0 is the object, and the element at index 1 is
  317.      *         the name of the method to call in the object.
  318.      *
  319.      *         When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  320.      *         a printf format string used when printing the error
  321.      *         message.
  322.      *
  323.      * @access public
  324.      * @return void 
  325.      * @see PEAR_ERROR_RETURN
  326.      * @see PEAR_ERROR_PRINT
  327.      * @see PEAR_ERROR_TRIGGER
  328.      * @see PEAR_ERROR_DIE
  329.      * @see PEAR_ERROR_CALLBACK
  330.      * @see PEAR_ERROR_EXCEPTION
  331.      *
  332.      * @since PHP 4.0.5
  333.      */
  334.  
  335.     function setErrorHandling($mode null$options null)
  336.     {
  337.         if (isset($this&& is_a($this'PEAR')) {
  338.             $setmode     &$this->_default_error_mode;
  339.             $setoptions  &$this->_default_error_options;
  340.         else {
  341.             $setmode     &$GLOBALS['_PEAR_default_error_mode'];
  342.             $setoptions  &$GLOBALS['_PEAR_default_error_options'];
  343.         }
  344.  
  345.         switch ($mode{
  346.             case PEAR_ERROR_EXCEPTION:
  347.             case PEAR_ERROR_RETURN:
  348.             case PEAR_ERROR_PRINT:
  349.             case PEAR_ERROR_TRIGGER:
  350.             case PEAR_ERROR_DIE:
  351.             case null:
  352.                 $setmode $mode;
  353.                 $setoptions $options;
  354.                 break;
  355.  
  356.             case PEAR_ERROR_CALLBACK:
  357.                 $setmode $mode;
  358.                 // class/object method callback
  359.                 if (is_callable($options)) {
  360.                     $setoptions $options;
  361.                 else {
  362.                     trigger_error("invalid error callback"E_USER_WARNING);
  363.                 }
  364.                 break;
  365.  
  366.             default:
  367.                 trigger_error("invalid error mode"E_USER_WARNING);
  368.                 break;
  369.         }
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ expectError()
  374.  
  375.     
  376.     /**
  377.      * This method is used to tell which errors you expect to get.
  378.      * Expected errors are always returned with error mode
  379.      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
  380.      * and this method pushes a new element onto it.  The list of
  381.      * expected errors are in effect until they are popped off the
  382.      * stack with the popExpect() method.
  383.      *
  384.      * Note that this method can not be called statically
  385.      *
  386.      * @param mixed $code a single error code or an array of error codes to expect
  387.      *
  388.      * @return int     the new depth of the "expected errors" stack
  389.      * @access public
  390.      */
  391.     function expectError($code '*')
  392.     {
  393.         if (is_array($code)) {
  394.             array_push($this->_expected_errors$code);
  395.         else {
  396.             array_push($this->_expected_errorsarray($code));
  397.         }
  398.         return sizeof($this->_expected_errors);
  399.     }
  400.  
  401.     // }}}
  402.     // {{{ popExpect()
  403.  
  404.     
  405.     /**
  406.      * This method pops one element off the expected error codes
  407.      * stack.
  408.      *
  409.      * @return array   the list of error codes that were popped
  410.      */
  411.     function popExpect()
  412.     {
  413.         return array_pop($this->_expected_errors);
  414.     }
  415.  
  416.     // }}}
  417.     // {{{ _checkDelExpect()
  418.  
  419.     
  420.     /**
  421.      * This method checks unsets an error code if available
  422.      *
  423.      * @param mixed error code
  424.      * @return bool true if the error code was unset, false otherwise
  425.      * @access private
  426.      * @since PHP 4.3.0
  427.      */
  428.     function _checkDelExpect($error_code)
  429.     {
  430.         $deleted false;
  431.  
  432.         foreach ($this->_expected_errors AS $key => $error_array{
  433.             if (in_array($error_code$error_array)) {
  434.                 unset($this->_expected_errors[$key][array_search($error_code$error_array)]);
  435.                 $deleted true;
  436.             }
  437.  
  438.             // clean up empty arrays
  439.             if (== count($this->_expected_errors[$key])) {
  440.                 unset($this->_expected_errors[$key]);
  441.             }
  442.         }
  443.         return $deleted;
  444.     }
  445.  
  446.     // }}}
  447.     // {{{ delExpect()
  448.  
  449.     
  450.     /**
  451.      * This method deletes all occurences of the specified element from
  452.      * the expected error codes stack.
  453.      *
  454.      * @param  mixed $error_code error code that should be deleted
  455.      * @return mixed list of error codes that were deleted or error
  456.      * @access public
  457.      * @since PHP 4.3.0
  458.      */
  459.     function delExpect($error_code)
  460.     {
  461.         $deleted false;
  462.  
  463.         if ((is_array($error_code&& (!= count($error_code)))) {
  464.             // $error_code is a non-empty array here;
  465.             // we walk through it trying to unset all
  466.             // values
  467.             foreach($error_code as $key => $error{
  468.                 if ($this->_checkDelExpect($error)) {
  469.                     $deleted =  true;
  470.                 else {
  471.                     $deleted false;
  472.                 }
  473.             }
  474.             return $deleted true PEAR::raiseError("The expected error you submitted does not exist")// IMPROVE ME
  475.         elseif (!empty($error_code)) {
  476.             // $error_code comes alone, trying to unset it
  477.             if ($this->_checkDelExpect($error_code)) {
  478.                 return true;
  479.             else {
  480.                 return PEAR::raiseError("The expected error you submitted does not exist")// IMPROVE ME
  481.             }
  482.         else {
  483.             // $error_code is empty
  484.             return PEAR::raiseError("The expected error you submitted is empty")// IMPROVE ME
  485.         }
  486.     }
  487.  
  488.     // }}}
  489.     // {{{ raiseError()
  490.  
  491.     
  492.     /**
  493.      * This method is a wrapper that returns an instance of the
  494.      * configured error class with this object's default error
  495.      * handling applied.  If the $mode and $options parameters are not
  496.      * specified, the object's defaults are used.
  497.      *
  498.      * @param mixed $message a text error message or a PEAR error object
  499.      *
  500.      * @param int $code      a numeric error code (it is up to your class
  501.      *                   to define these if you want to use codes)
  502.      *
  503.      * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  504.      *                   PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  505.      *                   PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  506.      *
  507.      * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  508.      *                   specifies the PHP-internal error level (one of
  509.      *                   E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  510.      *                   If $mode is PEAR_ERROR_CALLBACK, this
  511.      *                   parameter specifies the callback function or
  512.      *                   method.  In other error modes this parameter
  513.      *                   is ignored.
  514.      *
  515.      * @param string $userinfo If you need to pass along for example debug
  516.      *                   information, this parameter is meant for that.
  517.      *
  518.      * @param string $error_class The returned error object will be
  519.      *                   instantiated from this class, if specified.
  520.      *
  521.      * @param bool $skipmsg If true, raiseError will only pass error codes,
  522.      *                   the error message parameter will be dropped.
  523.      *
  524.      * @access public
  525.      * @return object   PEAR error object
  526.      * @see PEAR::setErrorHandling
  527.      * @since PHP 4.0.5
  528.      */
  529.     function &raiseError($message null,
  530.                          $code null,
  531.                          $mode null,
  532.                          $options null,
  533.                          $userinfo null,
  534.                          $error_class null,
  535.                          $skipmsg false)
  536.     {
  537.         // The error is yet a PEAR error object
  538.         if (is_object($message)) {
  539.             $code        $message->getCode();
  540.             $userinfo    $message->getUserInfo();
  541.             $error_class $message->getType();
  542.             $message->error_message_prefix '';
  543.             $message     $message->getMessage();
  544.         }
  545.  
  546.         if (isset($this&& isset($this->_expected_errors&& sizeof($this->_expected_errors&& sizeof($exp end($this->_expected_errors))) {
  547.             if ($exp[0== "*" ||
  548.                 (is_int(reset($exp)) && in_array($code$exp)) ||
  549.                 (is_string(reset($exp)) && in_array($message$exp))) {
  550.                 $mode PEAR_ERROR_RETURN;
  551.             }
  552.         }
  553.         // No mode given, try global ones
  554.         if ($mode === null{
  555.             // Class error handler
  556.             if (isset($this&& isset($this->_default_error_mode)) {
  557.                 $mode    $this->_default_error_mode;
  558.                 $options $this->_default_error_options;
  559.             // Global error handler
  560.             elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  561.                 $mode    $GLOBALS['_PEAR_default_error_mode'];
  562.                 $options $GLOBALS['_PEAR_default_error_options'];
  563.             }
  564.         }
  565.  
  566.         if ($error_class !== null{
  567.             $ec $error_class;
  568.         elseif (isset($this&& isset($this->_error_class)) {
  569.             $ec $this->_error_class;
  570.         else {
  571.             $ec 'PEAR_Error';
  572.         }
  573.         if ($skipmsg{
  574.             $a &new $ec($code$mode$options$userinfo);
  575.             return $a;
  576.         else {
  577.             $a &new $ec($message$code$mode$options$userinfo);
  578.             return $a;
  579.         }
  580.     }
  581.  
  582.     // }}}
  583.     // {{{ throwError()
  584.  
  585.     
  586.     /**
  587.      * Simpler form of raiseError with fewer options.  In most cases
  588.      * message, code and userinfo are enough.
  589.      *
  590.      * @param string $message 
  591.      *
  592.      */
  593.     function &throwError($message null,
  594.                          $code null,
  595.                          $userinfo null)
  596.     {
  597.         if (isset($this&& is_a($this'PEAR')) {
  598.             $a &$this->raiseError($message$codenullnull$userinfo);
  599.             return $a;
  600.         else {
  601.             $a &PEAR::raiseError($message$codenullnull$userinfo);
  602.             return $a;
  603.         }
  604.     }
  605.  
  606.     // }}}
  607.         function staticPushErrorHandling($mode$options null)
  608.     {
  609.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  610.         $def_mode    &$GLOBALS['_PEAR_default_error_mode'];
  611.         $def_options &$GLOBALS['_PEAR_default_error_options'];
  612.         $stack[array($def_mode$def_options);
  613.         switch ($mode{
  614.             case PEAR_ERROR_EXCEPTION:
  615.             case PEAR_ERROR_RETURN:
  616.             case PEAR_ERROR_PRINT:
  617.             case PEAR_ERROR_TRIGGER:
  618.             case PEAR_ERROR_DIE:
  619.             case null:
  620.                 $def_mode $mode;
  621.                 $def_options $options;
  622.                 break;
  623.  
  624.             case PEAR_ERROR_CALLBACK:
  625.                 $def_mode $mode;
  626.                 // class/object method callback
  627.                 if (is_callable($options)) {
  628.                     $def_options $options;
  629.                 else {
  630.                     trigger_error("invalid error callback"E_USER_WARNING);
  631.                 }
  632.                 break;
  633.  
  634.             default:
  635.                 trigger_error("invalid error mode"E_USER_WARNING);
  636.                 break;
  637.         }
  638.         $stack[array($mode$options);
  639.         return true;
  640.     }
  641.  
  642.     function staticPopErrorHandling()
  643.     {
  644.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  645.         $setmode     &$GLOBALS['_PEAR_default_error_mode'];
  646.         $setoptions  &$GLOBALS['_PEAR_default_error_options'];
  647.         array_pop($stack);
  648.         list($mode$options$stack[sizeof($stack1];
  649.         array_pop($stack);
  650.         switch ($mode{
  651.             case PEAR_ERROR_EXCEPTION:
  652.             case PEAR_ERROR_RETURN:
  653.             case PEAR_ERROR_PRINT:
  654.             case PEAR_ERROR_TRIGGER:
  655.             case PEAR_ERROR_DIE:
  656.             case null:
  657.                 $setmode $mode;
  658.                 $setoptions $options;
  659.                 break;
  660.  
  661.             case PEAR_ERROR_CALLBACK:
  662.                 $setmode $mode;
  663.                 // class/object method callback
  664.                 if (is_callable($options)) {
  665.                     $setoptions $options;
  666.                 else {
  667.                     trigger_error("invalid error callback"E_USER_WARNING);
  668.                 }
  669.                 break;
  670.  
  671.             default:
  672.                 trigger_error("invalid error mode"E_USER_WARNING);
  673.                 break;
  674.         }
  675.         return true;
  676.     }
  677.  
  678.     // {{{ pushErrorHandling()
  679.  
  680.     
  681.     /**
  682.      * Push a new error handler on top of the error handler options stack. With this
  683.      * you can easily override the actual error handler for some code and restore
  684.      * it later with popErrorHandling.
  685.      *
  686.      * @param mixed $mode (same as setErrorHandling)
  687.      * @param mixed $options (same as setErrorHandling)
  688.      *
  689.      * @return bool Always true
  690.      *
  691.      * @see PEAR::setErrorHandling
  692.      */
  693.     function pushErrorHandling($mode$options null)
  694.     {
  695.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  696.         if (isset($this&& is_a($this'PEAR')) {
  697.             $def_mode    &$this->_default_error_mode;
  698.             $def_options &$this->_default_error_options;
  699.         else {
  700.             $def_mode    &$GLOBALS['_PEAR_default_error_mode'];
  701.             $def_options &$GLOBALS['_PEAR_default_error_options'];
  702.         }
  703.         $stack[array($def_mode$def_options);
  704.  
  705.         if (isset($this&& is_a($this'PEAR')) {
  706.             $this->setErrorHandling($mode$options);
  707.         else {
  708.             PEAR::setErrorHandling($mode$options);
  709.         }
  710.         $stack[array($mode$options);
  711.         return true;
  712.     }
  713.  
  714.     // }}}
  715.     // {{{ popErrorHandling()
  716.  
  717.     
  718.     /**
  719.     * Pop the last error handler used
  720.     *
  721.     * @return bool Always true
  722.     *
  723.     * @see PEAR::pushErrorHandling
  724.     */
  725.     function popErrorHandling()
  726.     {
  727.         $stack &$GLOBALS['_PEAR_error_handler_stack'];
  728.         array_pop($stack);
  729.         list($mode$options$stack[sizeof($stack1];
  730.         array_pop($stack);
  731.         if (isset($this&& is_a($this'PEAR')) {
  732.             $this->setErrorHandling($mode$options);
  733.         else {
  734.             PEAR::setErrorHandling($mode$options);
  735.         }
  736.         return true;
  737.     }
  738.  
  739.     // }}}
  740.     // {{{ loadExtension()
  741.  
  742.     
  743.     /**
  744.     * OS independant PHP extension load. Remember to take care
  745.     * on the correct extension name for case sensitive OSes.
  746.     *
  747.     * @param string $ext The extension name
  748.     * @return bool Success or not on the dl() call
  749.     */
  750.     function loadExtension($ext)
  751.     {
  752.         if (!extension_loaded($ext)) {
  753.             // if either returns true dl() will produce a FATAL error, stop that
  754.             if ((ini_get('enable_dl'!= 1|| (ini_get('safe_mode'== 1)) {
  755.                 return false;
  756.             }
  757.             if (OS_WINDOWS{
  758.                 $suffix '.dll';
  759.             elseif (PHP_OS == 'HP-UX'{
  760.                 $suffix '.sl';
  761.             elseif (PHP_OS == 'AIX'{
  762.                 $suffix '.a';
  763.             elseif (PHP_OS == 'OSX'{
  764.                 $suffix '.bundle';
  765.             else {
  766.                 $suffix '.so';
  767.             }
  768.             return @dl('php_'.$ext.$suffix|| @dl($ext.$suffix);
  769.         }
  770.         return true;
  771.     }
  772.  
  773.     // }}}
  774. }
  775.  
  776. // {{{ _PEAR_call_destructors()
  777.  
  778. {
  779.     global $_PEAR_destructor_object_list;
  780.     if (is_array($_PEAR_destructor_object_list&&
  781.         sizeof($_PEAR_destructor_object_list))
  782.     {
  783.         reset($_PEAR_destructor_object_list);
  784.         if (@PEAR::getStaticProperty('PEAR''destructlifo')) {
  785.             $_PEAR_destructor_object_list array_reverse($_PEAR_destructor_object_list);
  786.         }
  787.         while (list($k$objrefeach($_PEAR_destructor_object_list)) {
  788.             $classname get_class($objref);
  789.             while ($classname{
  790.                 $destructor "_$classname";
  791.                 if (method_exists($objref$destructor)) {
  792.                     $objref->$destructor();
  793.                     break;
  794.                 else {
  795.                     $classname get_parent_class($classname);
  796.                 }
  797.             }
  798.         }
  799.         // Empty the object list to ensure that destructors are
  800.         // not called more than once.
  801.         $_PEAR_destructor_object_list array();
  802.     }
  803.  
  804.     // Now call the shutdown functions
  805.     if (is_array($GLOBALS['_PEAR_shutdown_funcs']AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  806.         foreach ($GLOBALS['_PEAR_shutdown_funcs'as $value{
  807.             call_user_func_array($value[0]$value[1]);
  808.         }
  809.     }
  810. }
  811.  
  812. // }}}
  813. /**
  814.  * Standard PEAR error class for PHP 4
  815.  *
  816.  * This class is supserseded by {@link PEAR_Exception} in PHP 5
  817.  *
  818.  * @category   pear
  819.  * @package    PEAR
  820.  * @author     Stig Bakken <[email protected]>
  821.  * @author     Tomas V.V. Cox <[email protected]>
  822.  * @author     Gregory Beaver <[email protected]>
  823.  * @copyright  1997-2006 The PHP Group
  824.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  825.  * @version    Release: 1.4.10
  826.  * @link       http://pear.php.net/manual/en/core.pear.pear-error.php
  827.  * @see        PEAR::raiseError(), PEAR::throwError()
  828.  * @since      Class available since PHP 4.0.2
  829.  */
  830. class PEAR_Error
  831. {
  832.     // {{{ properties
  833.  
  834.     
  835.     var $error_message_prefix = '';
  836.     var $mode                 = PEAR_ERROR_RETURN;
  837.     var $level                = E_USER_NOTICE;
  838.     var $code                 = -1;
  839.     var $message              = '';
  840.     var $userinfo             = '';
  841.     var $backtrace            = null;
  842.  
  843.     // }}}
  844.     // {{{ constructor
  845.  
  846.     
  847.     /**
  848.      * PEAR_Error constructor
  849.      *
  850.      * @param string $message  message
  851.      *
  852.      * @param int $code     (optional) error code
  853.      *
  854.      * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  855.      *  PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  856.      *  PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  857.      *
  858.      * @param mixed $options   (optional) error level, _OR_ in the case of
  859.      *  PEAR_ERROR_CALLBACK, the callback function or object/method
  860.      *  tuple.
  861.      *
  862.      * @param string $userinfo (optional) additional user/debug info
  863.      *
  864.      * @access public
  865.      *
  866.      */
  867.     function PEAR_Error($message 'unknown error'$code null,
  868.                         $mode null$options null$userinfo null)
  869.     {
  870.         if ($mode === null{
  871.             $mode PEAR_ERROR_RETURN;
  872.         }
  873.         $this->message   = $message;
  874.         $this->code      = $code;
  875.         $this->mode      = $mode;
  876.         $this->userinfo  = $userinfo;
  877.         if (function_exists("debug_backtrace")) {
  878.             if (@!PEAR::getStaticProperty('PEAR_Error''skiptrace')) {
  879.                 $this->backtrace = debug_backtrace();
  880.             }
  881.         }
  882.         if ($mode PEAR_ERROR_CALLBACK{
  883.             $this->level = E_USER_NOTICE;
  884.             $this->callback $options;
  885.         else {
  886.             if ($options === null{
  887.                 $options E_USER_NOTICE;
  888.             }
  889.             $this->level = $options;
  890.             $this->callback null;
  891.         }
  892.         if ($this->mode PEAR_ERROR_PRINT{
  893.             if (is_null($options|| is_int($options)) {
  894.                 $format "%s";
  895.             else {
  896.                 $format $options;
  897.             }
  898.             printf($format$this->getMessage());
  899.         }
  900.         if ($this->mode PEAR_ERROR_TRIGGER{
  901.             trigger_error($this->getMessage()$this->level);
  902.         }
  903.         if ($this->mode PEAR_ERROR_DIE{
  904.             $msg $this->getMessage();
  905.             if (is_null($options|| is_int($options)) {
  906.                 $format "%s";
  907.                 if (substr($msg-1!= "\n"{
  908.                     $msg .= "\n";
  909.                 }
  910.             else {
  911.                 $format $options;
  912.             }
  913.             die(sprintf($format$msg));
  914.         }
  915.         if ($this->mode PEAR_ERROR_CALLBACK{
  916.             if (is_callable($this->callback)) {
  917.                 call_user_func($this->callback$this);
  918.             }
  919.         }
  920.         if ($this->mode PEAR_ERROR_EXCEPTION{
  921.             trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions"E_USER_WARNING);
  922.             eval('$e = new Exception($this->message, $this->code);throw($e);');
  923.         }
  924.     }
  925.  
  926.     // }}}
  927.     // {{{ getMode()
  928.  
  929.     
  930.     /**
  931.      * Get the error mode from an error object.
  932.      *
  933.      * @return int error mode
  934.      * @access public
  935.      */
  936.     function getMode({
  937.         return $this->mode;
  938.     }
  939.  
  940.     // }}}
  941.     // {{{ getCallback()
  942.  
  943.     
  944.     /**
  945.      * Get the callback function/method from an error object.
  946.      *
  947.      * @return mixed callback function or object/method array
  948.      * @access public
  949.      */
  950.     function getCallback({
  951.         return $this->callback;
  952.     }
  953.  
  954.     // }}}
  955.     // {{{ getMessage()
  956.  
  957.  
  958.     
  959.     /**
  960.      * Get the error message from an error object.
  961.      *
  962.      * @return  string  full error message
  963.      * @access public
  964.      */
  965.     function getMessage()
  966.     {
  967.         return ($this->error_message_prefix . $this->message);
  968.     }
  969.  
  970.  
  971.     // }}}
  972.     // {{{ getCode()
  973.  
  974.     
  975.     /**
  976.      * Get error code from an error object
  977.      *
  978.      * @return int error code
  979.      * @access public
  980.      */
  981.      function getCode()
  982.      {
  983.         return $this->code;
  984.      }
  985.  
  986.     // }}}
  987.     // {{{ getType()
  988.  
  989.     
  990.     /**
  991.      * Get the name of this error/exception.
  992.      *
  993.      * @return string error/exception name (type)
  994.      * @access public
  995.      */
  996.     function getType()
  997.     {
  998.         return get_class($this);
  999.     }
  1000.  
  1001.     // }}}
  1002.     // {{{ getUserInfo()
  1003.  
  1004.     
  1005.     /**
  1006.      * Get additional user-supplied information.
  1007.      *
  1008.      * @return string user-supplied information
  1009.      * @access public
  1010.      */
  1011.     function getUserInfo()
  1012.     {
  1013.         return $this->userinfo;
  1014.     }
  1015.  
  1016.     // }}}
  1017.     // {{{ getDebugInfo()
  1018.  
  1019.     
  1020.     /**
  1021.      * Get additional debug information supplied by the application.
  1022.      *
  1023.      * @return string debug information
  1024.      * @access public
  1025.      */
  1026.     function getDebugInfo()
  1027.     {
  1028.         return $this->getUserInfo();
  1029.     }
  1030.  
  1031.     // }}}
  1032.     // {{{ getBacktrace()
  1033.  
  1034.     
  1035.     /**
  1036.      * Get the call backtrace from where the error was generated.
  1037.      * Supported with PHP 4.3.0 or newer.
  1038.      *
  1039.      * @param int $frame (optional) what frame to fetch
  1040.      * @return array Backtrace, or NULL if not available.
  1041.      * @access public
  1042.      */
  1043.     function getBacktrace($frame null)
  1044.     {
  1045.         if (defined('PEAR_IGNORE_BACKTRACE')) {
  1046.             return null;
  1047.         }
  1048.         if ($frame === null{
  1049.             return $this->backtrace;
  1050.         }
  1051.         return $this->backtrace[$frame];
  1052.     }
  1053.  
  1054.     // }}}
  1055.     // {{{ addUserInfo()
  1056.  
  1057.     
  1058.     function addUserInfo($info)
  1059.     {
  1060.         if (empty($this->userinfo)) {
  1061.             $this->userinfo = $info;
  1062.         else {
  1063.             $this->userinfo .= " ** $info";
  1064.         }
  1065.     }
  1066.  
  1067.     // }}}
  1068.     // {{{ toString()
  1069.  
  1070.     
  1071.     /**
  1072.      * Make a string representation of this object.
  1073.      *
  1074.      * @return string a string with an object summary
  1075.      * @access public
  1076.      */
  1077.     function toString({
  1078.         $modes array();
  1079.         $levels array(E_USER_NOTICE  => 'notice',
  1080.                         E_USER_WARNING => 'warning',
  1081.                         E_USER_ERROR   => 'error');
  1082.         if ($this->mode PEAR_ERROR_CALLBACK{
  1083.             if (is_array($this->callback)) {
  1084.                 $callback (is_object($this->callback[0]?
  1085.                     strtolower(get_class($this->callback[0])) :
  1086.                     $this->callback[0]'::' .
  1087.                     $this->callback[1];
  1088.             else {
  1089.                 $callback $this->callback;
  1090.             }
  1091.             return sprintf('[%s: message="%s" code=%d mode=callback '.
  1092.                            'callback=%s prefix="%s" info="%s"]',
  1093.                            strtolower(get_class($this))$this->message$this->code,
  1094.                            $callback$this->error_message_prefix,
  1095.                            $this->userinfo);
  1096.         }
  1097.         if ($this->mode PEAR_ERROR_PRINT{
  1098.             $modes['print';
  1099.         }
  1100.         if ($this->mode PEAR_ERROR_TRIGGER{
  1101.             $modes['trigger';
  1102.         }
  1103.         if ($this->mode PEAR_ERROR_DIE{
  1104.             $modes['die';
  1105.         }
  1106.         if ($this->mode PEAR_ERROR_RETURN{
  1107.             $modes['return';
  1108.         }
  1109.         return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  1110.                        'prefix="%s" info="%s"]',
  1111.                        strtolower(get_class($this))$this->message$this->code,
  1112.                        implode("|"$modes)$levels[$this->level],
  1113.                        $this->error_message_prefix,
  1114.                        $this->userinfo);
  1115.     }
  1116.  
  1117.     // }}}
  1118. }
  1119.  
  1120. /*
  1121.  * Local Variables:
  1122.  * mode: php
  1123.  * tab-width: 4
  1124.  * c-basic-offset: 4
  1125.  * End:
  1126.  */
  1127. ?>

Documentation generated on Mon, 05 Mar 2007 21:16:51 +0000 by phpDocumentor 1.3.1