[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/ -> PEAR.php (source)

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


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics