Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / lang / pear / files / PEAR.php
blobd6700255bc7e40014def968c04ede7a3a1df2847
1 <?php
2 /**
3 * PEAR, the PHP Extension and Application Repository
5 * PEAR class and PEAR_Error class
7 * PHP versions 4 and 5
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 license@php.net so we can mail you a copy immediately.
15 * @category pear
16 * @package PEAR
17 * @author Sterling Hughes <sterling@php.net>
18 * @author Stig Bakken <ssb@php.net>
19 * @author Tomas V.V.Cox <cox@idecnet.com>
20 * @author Greg Beaver <cellog@php.net>
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,v 1.97 2006/01/06 04:47:36 cellog Exp
24 * @link http://pear.php.net/package/PEAR
25 * @since File available since Release 0.1
29 * $NetBSD: PEAR.php,v 1.1 2009/10/29 08:29:03 seb Exp $
31 * This is http://cvs.php.net/viewvc.cgi/pear-core/PEAR.php?view=co&pathrev=PEAR_1_4
33 * See $bootstrap_files definition in go-pear.php
37 /**#@+
38 * ERROR constants
40 define('PEAR_ERROR_RETURN', 1);
41 define('PEAR_ERROR_PRINT', 2);
42 define('PEAR_ERROR_TRIGGER', 4);
43 define('PEAR_ERROR_DIE', 8);
44 define('PEAR_ERROR_CALLBACK', 16);
45 /**
46 * WARNING: obsolete
47 * @deprecated
49 define('PEAR_ERROR_EXCEPTION', 32);
50 /**#@-*/
51 define('PEAR_ZE2', (function_exists('version_compare') &&
52 version_compare(zend_version(), "2-dev", "ge")));
54 if (substr(PHP_OS, 0, 3) == 'WIN') {
55 define('OS_WINDOWS', true);
56 define('OS_UNIX', false);
57 define('PEAR_OS', 'Windows');
58 } else {
59 define('OS_WINDOWS', false);
60 define('OS_UNIX', true);
61 define('PEAR_OS', 'Unix'); // blatant assumption
64 // instant backwards compatibility
65 if (!defined('PATH_SEPARATOR')) {
66 if (OS_WINDOWS) {
67 define('PATH_SEPARATOR', ';');
68 } else {
69 define('PATH_SEPARATOR', ':');
73 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
74 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
75 $GLOBALS['_PEAR_destructor_object_list'] = array();
76 $GLOBALS['_PEAR_shutdown_funcs'] = array();
77 $GLOBALS['_PEAR_error_handler_stack'] = array();
79 @ini_set('track_errors', true);
81 /**
82 * Base class for other PEAR classes. Provides rudimentary
83 * emulation of destructors.
85 * If you want a destructor in your class, inherit PEAR and make a
86 * destructor method called _yourclassname (same name as the
87 * constructor, but with a "_" prefix). Also, in your constructor you
88 * have to call the PEAR constructor: $this->PEAR();.
89 * The destructor method will be called without parameters. Note that
90 * at in some SAPI implementations (such as Apache), any output during
91 * the request shutdown (in which destructors are called) seems to be
92 * discarded. If you need to get any debug information from your
93 * destructor, use error_log(), syslog() or something similar.
95 * IMPORTANT! To use the emulated destructors you need to create the
96 * objects by reference: $obj =& new PEAR_child;
98 * @category pear
99 * @package PEAR
100 * @author Stig Bakken <ssb@php.net>
101 * @author Tomas V.V. Cox <cox@idecnet.com>
102 * @author Greg Beaver <cellog@php.net>
103 * @copyright 1997-2006 The PHP Group
104 * @license http://www.php.net/license/3_0.txt PHP License 3.0
105 * @version Release: @package_version@
106 * @link http://pear.php.net/package/PEAR
107 * @see PEAR_Error
108 * @since Class available since PHP 4.0.2
109 * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear
111 class PEAR
113 // {{{ properties
116 * Whether to enable internal debug messages.
118 * @var bool
119 * @access private
121 var $_debug = false;
124 * Default error mode for this object.
126 * @var int
127 * @access private
129 var $_default_error_mode = null;
132 * Default error options used for this object when error mode
133 * is PEAR_ERROR_TRIGGER.
135 * @var int
136 * @access private
138 var $_default_error_options = null;
141 * Default error handler (callback) for this object, if error mode is
142 * PEAR_ERROR_CALLBACK.
144 * @var string
145 * @access private
147 var $_default_error_handler = '';
150 * Which class to use for error objects.
152 * @var string
153 * @access private
155 var $_error_class = 'PEAR_Error';
158 * An array of expected errors.
160 * @var array
161 * @access private
163 var $_expected_errors = array();
165 // }}}
167 // {{{ constructor
170 * Constructor. Registers this object in
171 * $_PEAR_destructor_object_list for destructor emulation if a
172 * destructor object exists.
174 * @param string $error_class (optional) which class to use for
175 * error objects, defaults to PEAR_Error.
176 * @access public
177 * @return void
179 function PEAR($error_class = null)
181 $classname = strtolower(get_class($this));
182 if ($this->_debug) {
183 print "PEAR constructor called, class=$classname\n";
185 if ($error_class !== null) {
186 $this->_error_class = $error_class;
188 while ($classname && strcasecmp($classname, "pear")) {
189 $destructor = "_$classname";
190 if (method_exists($this, $destructor)) {
191 global $_PEAR_destructor_object_list;
192 $_PEAR_destructor_object_list[] = &$this;
193 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
194 register_shutdown_function("_PEAR_call_destructors");
195 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
197 break;
198 } else {
199 $classname = get_parent_class($classname);
204 // }}}
205 // {{{ destructor
208 * Destructor (the emulated type of...). Does nothing right now,
209 * but is included for forward compatibility, so subclass
210 * destructors should always call it.
212 * See the note in the class desciption about output from
213 * destructors.
215 * @access public
216 * @return void
218 function _PEAR() {
219 if ($this->_debug) {
220 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
224 // }}}
225 // {{{ getStaticProperty()
228 * If you have a class that's mostly/entirely static, and you need static
229 * properties, you can use this method to simulate them. Eg. in your method(s)
230 * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
231 * You MUST use a reference, or they will not persist!
233 * @access public
234 * @param string $class The calling classname, to prevent clashes
235 * @param string $var The variable to retrieve.
236 * @return mixed A reference to the variable. If not set it will be
237 * auto initialised to NULL.
239 function &getStaticProperty($class, $var)
241 static $properties;
242 return $properties[$class][$var];
245 // }}}
246 // {{{ registerShutdownFunc()
249 * Use this function to register a shutdown method for static
250 * classes.
252 * @access public
253 * @param mixed $func The function name (or array of class/method) to call
254 * @param mixed $args The arguments to pass to the function
255 * @return void
257 function registerShutdownFunc($func, $args = array())
259 // if we are called statically, there is a potential
260 // that no shutdown func is registered. Bug #6445
261 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
262 register_shutdown_function("_PEAR_call_destructors");
263 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
265 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
268 // }}}
269 // {{{ isError()
272 * Tell whether a value is a PEAR error.
274 * @param mixed $data the value to test
275 * @param int $code if $data is an error object, return true
276 * only if $code is a string and
277 * $obj->getMessage() == $code or
278 * $code is an integer and $obj->getCode() == $code
279 * @access public
280 * @return bool true if parameter is an error
282 function isError($data, $code = null)
284 if (is_a($data, 'PEAR_Error')) {
285 if (is_null($code)) {
286 return true;
287 } elseif (is_string($code)) {
288 return $data->getMessage() == $code;
289 } else {
290 return $data->getCode() == $code;
293 return false;
296 // }}}
297 // {{{ setErrorHandling()
300 * Sets how errors generated by this object should be handled.
301 * Can be invoked both in objects and statically. If called
302 * statically, setErrorHandling sets the default behaviour for all
303 * PEAR objects. If called in an object, setErrorHandling sets
304 * the default behaviour for that object.
306 * @param int $mode
307 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
308 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
309 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
311 * @param mixed $options
312 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
313 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
315 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
316 * to be the callback function or method. A callback
317 * function is a string with the name of the function, a
318 * callback method is an array of two elements: the element
319 * at index 0 is the object, and the element at index 1 is
320 * the name of the method to call in the object.
322 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
323 * a printf format string used when printing the error
324 * message.
326 * @access public
327 * @return void
328 * @see PEAR_ERROR_RETURN
329 * @see PEAR_ERROR_PRINT
330 * @see PEAR_ERROR_TRIGGER
331 * @see PEAR_ERROR_DIE
332 * @see PEAR_ERROR_CALLBACK
333 * @see PEAR_ERROR_EXCEPTION
335 * @since PHP 4.0.5
338 function setErrorHandling($mode = null, $options = null)
340 if (isset($this) && is_a($this, 'PEAR')) {
341 $setmode = &$this->_default_error_mode;
342 $setoptions = &$this->_default_error_options;
343 } else {
344 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
345 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
348 switch ($mode) {
349 case PEAR_ERROR_EXCEPTION:
350 case PEAR_ERROR_RETURN:
351 case PEAR_ERROR_PRINT:
352 case PEAR_ERROR_TRIGGER:
353 case PEAR_ERROR_DIE:
354 case null:
355 $setmode = $mode;
356 $setoptions = $options;
357 break;
359 case PEAR_ERROR_CALLBACK:
360 $setmode = $mode;
361 // class/object method callback
362 if (is_callable($options)) {
363 $setoptions = $options;
364 } else {
365 trigger_error("invalid error callback", E_USER_WARNING);
367 break;
369 default:
370 trigger_error("invalid error mode", E_USER_WARNING);
371 break;
375 // }}}
376 // {{{ expectError()
379 * This method is used to tell which errors you expect to get.
380 * Expected errors are always returned with error mode
381 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
382 * and this method pushes a new element onto it. The list of
383 * expected errors are in effect until they are popped off the
384 * stack with the popExpect() method.
386 * Note that this method can not be called statically
388 * @param mixed $code a single error code or an array of error codes to expect
390 * @return int the new depth of the "expected errors" stack
391 * @access public
393 function expectError($code = '*')
395 if (is_array($code)) {
396 array_push($this->_expected_errors, $code);
397 } else {
398 array_push($this->_expected_errors, array($code));
400 return sizeof($this->_expected_errors);
403 // }}}
404 // {{{ popExpect()
407 * This method pops one element off the expected error codes
408 * stack.
410 * @return array the list of error codes that were popped
412 function popExpect()
414 return array_pop($this->_expected_errors);
417 // }}}
418 // {{{ _checkDelExpect()
421 * This method checks unsets an error code if available
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
428 function _checkDelExpect($error_code)
430 $deleted = false;
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;
438 // clean up empty arrays
439 if (0 == count($this->_expected_errors[$key])) {
440 unset($this->_expected_errors[$key]);
443 return $deleted;
446 // }}}
447 // {{{ delExpect()
450 * This method deletes all occurences of the specified element from
451 * the expected error codes stack.
453 * @param mixed $error_code error code that should be deleted
454 * @return mixed list of error codes that were deleted or error
455 * @access public
456 * @since PHP 4.3.0
458 function delExpect($error_code)
460 $deleted = false;
462 if ((is_array($error_code) && (0 != count($error_code)))) {
463 // $error_code is a non-empty array here;
464 // we walk through it trying to unset all
465 // values
466 foreach($error_code as $key => $error) {
467 if ($this->_checkDelExpect($error)) {
468 $deleted = true;
469 } else {
470 $deleted = false;
473 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
474 } elseif (!empty($error_code)) {
475 // $error_code comes alone, trying to unset it
476 if ($this->_checkDelExpect($error_code)) {
477 return true;
478 } else {
479 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
481 } else {
482 // $error_code is empty
483 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
487 // }}}
488 // {{{ raiseError()
491 * This method is a wrapper that returns an instance of the
492 * configured error class with this object's default error
493 * handling applied. If the $mode and $options parameters are not
494 * specified, the object's defaults are used.
496 * @param mixed $message a text error message or a PEAR error object
498 * @param int $code a numeric error code (it is up to your class
499 * to define these if you want to use codes)
501 * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
502 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
503 * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
505 * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
506 * specifies the PHP-internal error level (one of
507 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
508 * If $mode is PEAR_ERROR_CALLBACK, this
509 * parameter specifies the callback function or
510 * method. In other error modes this parameter
511 * is ignored.
513 * @param string $userinfo If you need to pass along for example debug
514 * information, this parameter is meant for that.
516 * @param string $error_class The returned error object will be
517 * instantiated from this class, if specified.
519 * @param bool $skipmsg If true, raiseError will only pass error codes,
520 * the error message parameter will be dropped.
522 * @access public
523 * @return object a PEAR error object
524 * @see PEAR::setErrorHandling
525 * @since PHP 4.0.5
527 function &raiseError($message = null,
528 $code = null,
529 $mode = null,
530 $options = null,
531 $userinfo = null,
532 $error_class = null,
533 $skipmsg = false)
535 // The error is yet a PEAR error object
536 if (is_object($message)) {
537 $code = $message->getCode();
538 $userinfo = $message->getUserInfo();
539 $error_class = $message->getType();
540 $message->error_message_prefix = '';
541 $message = $message->getMessage();
544 if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
545 if ($exp[0] == "*" ||
546 (is_int(reset($exp)) && in_array($code, $exp)) ||
547 (is_string(reset($exp)) && in_array($message, $exp))) {
548 $mode = PEAR_ERROR_RETURN;
551 // No mode given, try global ones
552 if ($mode === null) {
553 // Class error handler
554 if (isset($this) && isset($this->_default_error_mode)) {
555 $mode = $this->_default_error_mode;
556 $options = $this->_default_error_options;
557 // Global error handler
558 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
559 $mode = $GLOBALS['_PEAR_default_error_mode'];
560 $options = $GLOBALS['_PEAR_default_error_options'];
564 if ($error_class !== null) {
565 $ec = $error_class;
566 } elseif (isset($this) && isset($this->_error_class)) {
567 $ec = $this->_error_class;
568 } else {
569 $ec = 'PEAR_Error';
571 if ($skipmsg) {
572 $a = new $ec($code, $mode, $options, $userinfo);
573 return $a;
574 } else {
575 $a = new $ec($message, $code, $mode, $options, $userinfo);
576 return $a;
580 // }}}
581 // {{{ throwError()
584 * Simpler form of raiseError with fewer options. In most cases
585 * message, code and userinfo are enough.
587 * @param string $message
590 function &throwError($message = null,
591 $code = null,
592 $userinfo = null)
594 if (isset($this) && is_a($this, 'PEAR')) {
595 $a = &$this->raiseError($message, $code, null, null, $userinfo);
596 return $a;
597 } else {
598 $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
599 return $a;
603 // }}}
604 function staticPushErrorHandling($mode, $options = null)
606 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
607 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
608 $def_options = &$GLOBALS['_PEAR_default_error_options'];
609 $stack[] = array($def_mode, $def_options);
610 switch ($mode) {
611 case PEAR_ERROR_EXCEPTION:
612 case PEAR_ERROR_RETURN:
613 case PEAR_ERROR_PRINT:
614 case PEAR_ERROR_TRIGGER:
615 case PEAR_ERROR_DIE:
616 case null:
617 $def_mode = $mode;
618 $def_options = $options;
619 break;
621 case PEAR_ERROR_CALLBACK:
622 $def_mode = $mode;
623 // class/object method callback
624 if (is_callable($options)) {
625 $def_options = $options;
626 } else {
627 trigger_error("invalid error callback", E_USER_WARNING);
629 break;
631 default:
632 trigger_error("invalid error mode", E_USER_WARNING);
633 break;
635 $stack[] = array($mode, $options);
636 return true;
639 function staticPopErrorHandling()
641 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
642 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
643 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
644 array_pop($stack);
645 list($mode, $options) = $stack[sizeof($stack) - 1];
646 array_pop($stack);
647 switch ($mode) {
648 case PEAR_ERROR_EXCEPTION:
649 case PEAR_ERROR_RETURN:
650 case PEAR_ERROR_PRINT:
651 case PEAR_ERROR_TRIGGER:
652 case PEAR_ERROR_DIE:
653 case null:
654 $setmode = $mode;
655 $setoptions = $options;
656 break;
658 case PEAR_ERROR_CALLBACK:
659 $setmode = $mode;
660 // class/object method callback
661 if (is_callable($options)) {
662 $setoptions = $options;
663 } else {
664 trigger_error("invalid error callback", E_USER_WARNING);
666 break;
668 default:
669 trigger_error("invalid error mode", E_USER_WARNING);
670 break;
672 return true;
675 // {{{ pushErrorHandling()
678 * Push a new error handler on top of the error handler options stack. With this
679 * you can easily override the actual error handler for some code and restore
680 * it later with popErrorHandling.
682 * @param mixed $mode (same as setErrorHandling)
683 * @param mixed $options (same as setErrorHandling)
685 * @return bool Always true
687 * @see PEAR::setErrorHandling
689 function pushErrorHandling($mode, $options = null)
691 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
692 if (isset($this) && is_a($this, 'PEAR')) {
693 $def_mode = &$this->_default_error_mode;
694 $def_options = &$this->_default_error_options;
695 } else {
696 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
697 $def_options = &$GLOBALS['_PEAR_default_error_options'];
699 $stack[] = array($def_mode, $def_options);
701 if (isset($this) && is_a($this, 'PEAR')) {
702 $this->setErrorHandling($mode, $options);
703 } else {
704 PEAR::setErrorHandling($mode, $options);
706 $stack[] = array($mode, $options);
707 return true;
710 // }}}
711 // {{{ popErrorHandling()
714 * Pop the last error handler used
716 * @return bool Always true
718 * @see PEAR::pushErrorHandling
720 function popErrorHandling()
722 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
723 array_pop($stack);
724 list($mode, $options) = $stack[sizeof($stack) - 1];
725 array_pop($stack);
726 if (isset($this) && is_a($this, 'PEAR')) {
727 $this->setErrorHandling($mode, $options);
728 } else {
729 PEAR::setErrorHandling($mode, $options);
731 return true;
734 // }}}
735 // {{{ loadExtension()
738 * OS independant PHP extension load. Remember to take care
739 * on the correct extension name for case sensitive OSes.
741 * @param string $ext The extension name
742 * @return bool Success or not on the dl() call
744 function loadExtension($ext)
746 if (!extension_loaded($ext)) {
747 // if either returns true dl() will produce a FATAL error, stop that
748 if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
749 return false;
751 if (OS_WINDOWS) {
752 $suffix = '.dll';
753 } elseif (PHP_OS == 'HP-UX') {
754 $suffix = '.sl';
755 } elseif (PHP_OS == 'AIX') {
756 $suffix = '.a';
757 } elseif (PHP_OS == 'OSX') {
758 $suffix = '.bundle';
759 } else {
760 $suffix = '.so';
762 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
764 return true;
767 // }}}
770 // {{{ _PEAR_call_destructors()
772 function _PEAR_call_destructors()
774 global $_PEAR_destructor_object_list;
775 if (is_array($_PEAR_destructor_object_list) &&
776 sizeof($_PEAR_destructor_object_list))
778 reset($_PEAR_destructor_object_list);
779 if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
780 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
782 while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
783 $classname = get_class($objref);
784 while ($classname) {
785 $destructor = "_$classname";
786 if (method_exists($objref, $destructor)) {
787 $objref->$destructor();
788 break;
789 } else {
790 $classname = get_parent_class($classname);
794 // Empty the object list to ensure that destructors are
795 // not called more than once.
796 $_PEAR_destructor_object_list = array();
799 // Now call the shutdown functions
800 if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
801 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
802 call_user_func_array($value[0], $value[1]);
807 // }}}
809 * Standard PEAR error class for PHP 4
811 * This class is supserseded by {@link PEAR_Exception} in PHP 5
813 * @category pear
814 * @package PEAR
815 * @author Stig Bakken <ssb@php.net>
816 * @author Tomas V.V. Cox <cox@idecnet.com>
817 * @author Gregory Beaver <cellog@php.net>
818 * @copyright 1997-2006 The PHP Group
819 * @license http://www.php.net/license/3_0.txt PHP License 3.0
820 * @version Release: @package_version@
821 * @link http://pear.php.net/manual/en/core.pear.pear-error.php
822 * @see PEAR::raiseError(), PEAR::throwError()
823 * @since Class available since PHP 4.0.2
825 class PEAR_Error
827 // {{{ properties
829 var $error_message_prefix = '';
830 var $mode = PEAR_ERROR_RETURN;
831 var $level = E_USER_NOTICE;
832 var $code = -1;
833 var $message = '';
834 var $userinfo = '';
835 var $backtrace = null;
837 // }}}
838 // {{{ constructor
841 * PEAR_Error constructor
843 * @param string $message message
845 * @param int $code (optional) error code
847 * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
848 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
849 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
851 * @param mixed $options (optional) error level, _OR_ in the case of
852 * PEAR_ERROR_CALLBACK, the callback function or object/method
853 * tuple.
855 * @param string $userinfo (optional) additional user/debug info
857 * @access public
860 function PEAR_Error($message = 'unknown error', $code = null,
861 $mode = null, $options = null, $userinfo = null)
863 if ($mode === null) {
864 $mode = PEAR_ERROR_RETURN;
866 $this->message = $message;
867 $this->code = $code;
868 $this->mode = $mode;
869 $this->userinfo = $userinfo;
870 if (function_exists("debug_backtrace")) {
871 if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
872 $this->backtrace = debug_backtrace();
875 if ($mode & PEAR_ERROR_CALLBACK) {
876 $this->level = E_USER_NOTICE;
877 $this->callback = $options;
878 } else {
879 if ($options === null) {
880 $options = E_USER_NOTICE;
882 $this->level = $options;
883 $this->callback = null;
885 if ($this->mode & PEAR_ERROR_PRINT) {
886 if (is_null($options) || is_int($options)) {
887 $format = "%s";
888 } else {
889 $format = $options;
891 printf($format, $this->getMessage());
893 if ($this->mode & PEAR_ERROR_TRIGGER) {
894 trigger_error($this->getMessage(), $this->level);
896 if ($this->mode & PEAR_ERROR_DIE) {
897 $msg = $this->getMessage();
898 if (is_null($options) || is_int($options)) {
899 $format = "%s";
900 if (substr($msg, -1) != "\n") {
901 $msg .= "\n";
903 } else {
904 $format = $options;
906 die(sprintf($format, $msg));
908 if ($this->mode & PEAR_ERROR_CALLBACK) {
909 if (is_callable($this->callback)) {
910 call_user_func($this->callback, $this);
913 if ($this->mode & PEAR_ERROR_EXCEPTION) {
914 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
915 eval('$e = new Exception($this->message, $this->code);throw($e);');
919 // }}}
920 // {{{ getMode()
923 * Get the error mode from an error object.
925 * @return int error mode
926 * @access public
928 function getMode() {
929 return $this->mode;
932 // }}}
933 // {{{ getCallback()
936 * Get the callback function/method from an error object.
938 * @return mixed callback function or object/method array
939 * @access public
941 function getCallback() {
942 return $this->callback;
945 // }}}
946 // {{{ getMessage()
950 * Get the error message from an error object.
952 * @return string full error message
953 * @access public
955 function getMessage()
957 return ($this->error_message_prefix . $this->message);
961 // }}}
962 // {{{ getCode()
965 * Get error code from an error object
967 * @return int error code
968 * @access public
970 function getCode()
972 return $this->code;
975 // }}}
976 // {{{ getType()
979 * Get the name of this error/exception.
981 * @return string error/exception name (type)
982 * @access public
984 function getType()
986 return get_class($this);
989 // }}}
990 // {{{ getUserInfo()
993 * Get additional user-supplied information.
995 * @return string user-supplied information
996 * @access public
998 function getUserInfo()
1000 return $this->userinfo;
1003 // }}}
1004 // {{{ getDebugInfo()
1007 * Get additional debug information supplied by the application.
1009 * @return string debug information
1010 * @access public
1012 function getDebugInfo()
1014 return $this->getUserInfo();
1017 // }}}
1018 // {{{ getBacktrace()
1021 * Get the call backtrace from where the error was generated.
1022 * Supported with PHP 4.3.0 or newer.
1024 * @param int $frame (optional) what frame to fetch
1025 * @return array Backtrace, or NULL if not available.
1026 * @access public
1028 function getBacktrace($frame = null)
1030 if (defined('PEAR_IGNORE_BACKTRACE')) {
1031 return null;
1033 if ($frame === null) {
1034 return $this->backtrace;
1036 return $this->backtrace[$frame];
1039 // }}}
1040 // {{{ addUserInfo()
1042 function addUserInfo($info)
1044 if (empty($this->userinfo)) {
1045 $this->userinfo = $info;
1046 } else {
1047 $this->userinfo .= " ** $info";
1051 // }}}
1052 // {{{ toString()
1055 * Make a string representation of this object.
1057 * @return string a string with an object summary
1058 * @access public
1060 function toString() {
1061 $modes = array();
1062 $levels = array(E_USER_NOTICE => 'notice',
1063 E_USER_WARNING => 'warning',
1064 E_USER_ERROR => 'error');
1065 if ($this->mode & PEAR_ERROR_CALLBACK) {
1066 if (is_array($this->callback)) {
1067 $callback = (is_object($this->callback[0]) ?
1068 strtolower(get_class($this->callback[0])) :
1069 $this->callback[0]) . '::' .
1070 $this->callback[1];
1071 } else {
1072 $callback = $this->callback;
1074 return sprintf('[%s: message="%s" code=%d mode=callback '.
1075 'callback=%s prefix="%s" info="%s"]',
1076 strtolower(get_class($this)), $this->message, $this->code,
1077 $callback, $this->error_message_prefix,
1078 $this->userinfo);
1080 if ($this->mode & PEAR_ERROR_PRINT) {
1081 $modes[] = 'print';
1083 if ($this->mode & PEAR_ERROR_TRIGGER) {
1084 $modes[] = 'trigger';
1086 if ($this->mode & PEAR_ERROR_DIE) {
1087 $modes[] = 'die';
1089 if ($this->mode & PEAR_ERROR_RETURN) {
1090 $modes[] = 'return';
1092 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
1093 'prefix="%s" info="%s"]',
1094 strtolower(get_class($this)), $this->message, $this->code,
1095 implode("|", $modes), $levels[$this->level],
1096 $this->error_message_prefix,
1097 $this->userinfo);
1100 // }}}
1104 * Local Variables:
1105 * mode: php
1106 * tab-width: 4
1107 * c-basic-offset: 4
1108 * End: