/[cvs]/nfo/php/libs/net.php.pear/PEAR.php
ViewVC logotype

Contents of /nfo/php/libs/net.php.pear/PEAR.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Wed Feb 5 20:12:34 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +8 -1 lines
+ function &getStaticProperty

1 <?php
2 //
3 // +----------------------------------------------------------------------+
4 // | PHP Version 4 |
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2002 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 at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.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@fast.no> |
18 // | Tomas V.V.Cox <cox@idecnet.com> |
19 // +----------------------------------------------------------------------+
20 //
21 // Id: PEAR.php,v 1.32.2.2 2002/04/09 19:04:07 ssb 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 if (substr(PHP_OS, 0, 3) == 'WIN') {
31 define('OS_WINDOWS', true);
32 define('OS_UNIX', false);
33 define('PEAR_OS', 'Windows');
34 } else {
35 define('OS_WINDOWS', false);
36 define('OS_UNIX', true);
37 define('PEAR_OS', 'Unix'); // blatant assumption
38 }
39
40 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
41 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
42 $GLOBALS['_PEAR_default_error_callback'] = '';
43 $GLOBALS['_PEAR_destructor_object_list'] = array();
44
45 //
46 // Tests needed: - PEAR inheritance
47 //
48
49 /**
50 * Base class for other PEAR classes. Provides rudimentary
51 * emulation of destructors.
52 *
53 * If you want a destructor in your class, inherit PEAR and make a
54 * destructor method called _yourclassname (same name as the
55 * constructor, but with a "_" prefix). Also, in your constructor you
56 * have to call the PEAR constructor: $this->PEAR();.
57 * The destructor method will be called without parameters. Note that
58 * at in some SAPI implementations (such as Apache), any output during
59 * the request shutdown (in which destructors are called) seems to be
60 * discarded. If you need to get any debug information from your
61 * destructor, use error_log(), syslog() or something similar.
62 *
63 * @since PHP 4.0.2
64 * @author Stig Bakken <ssb@fast.no>
65 */
66 class PEAR
67 {
68 // {{{ properties
69
70 /**
71 * Whether to enable internal debug messages.
72 *
73 * @var bool
74 * @access private
75 */
76 var $_debug = false;
77
78 /**
79 * Default error mode for this object.
80 *
81 * @var int
82 * @access private
83 */
84 var $_default_error_mode = null;
85
86 /**
87 * Default error options used for this object when error mode
88 * is PEAR_ERROR_TRIGGER.
89 *
90 * @var int
91 * @access private
92 */
93 var $_default_error_options = null;
94
95 /**
96 * Default error handler (callback) for this object, if error mode is
97 * PEAR_ERROR_CALLBACK.
98 *
99 * @var string
100 * @access private
101 */
102 var $_default_error_handler = '';
103
104 /**
105 * Which class to use for error objects.
106 *
107 * @var string
108 * @access private
109 */
110 var $_error_class = 'PEAR_Error';
111
112 /**
113 * An array of expected errors.
114 *
115 * @var array
116 * @access private
117 */
118 var $_expected_errors = array();
119
120 // }}}
121
122 // {{{ constructor
123
124 /**
125 * Constructor. Registers this object in
126 * $_PEAR_destructor_object_list for destructor emulation if a
127 * destructor object exists.
128 *
129 * @param string (optional) which class to use for error objects,
130 * defaults to PEAR_Error.
131 * @access public
132 * @return void
133 */
134 function PEAR($error_class = null)
135 {
136 $classname = get_class($this);
137 if ($this->_debug) {
138 print "PEAR constructor called, class=$classname\n";
139 }
140 if ($error_class !== null) {
141 $this->_error_class = $error_class;
142 }
143 while ($classname) {
144 $destructor = "_$classname";
145 if (method_exists($this, $destructor)) {
146 global $_PEAR_destructor_object_list;
147 $_PEAR_destructor_object_list[] = &$this;
148 break;
149 } else {
150 $classname = get_parent_class($classname);
151 }
152 }
153 }
154
155 // }}}
156 // {{{ destructor
157
158 /**
159 * Destructor (the emulated type of...). Does nothing right now,
160 * but is included for forward compatibility, so subclass
161 * destructors should always call it.
162 *
163 * See the note in the class desciption about output from
164 * destructors.
165 *
166 * @access public
167 * @return void
168 */
169 function _PEAR() {
170 if ($this->_debug) {
171 printf("PEAR destructor called, class=%s\n", get_class($this));
172 }
173 }
174
175 // }}}
176 // {{{ isError()
177
178 /**
179 * Tell whether a value is a PEAR error.
180 *
181 * @param mixed the value to test
182 * @access public
183 * @return bool true if parameter is an error
184 */
185 function isError($data) {
186 return (bool)(is_object($data) &&
187 (get_class($data) == 'pear_error' ||
188 is_subclass_of($data, 'pear_error')));
189 }
190
191 // }}}
192 // {{{ setErrorHandling()
193
194 /**
195 * Sets how errors generated by this DB object should be handled.
196 * Can be invoked both in objects and statically. If called
197 * statically, setErrorHandling sets the default behaviour for all
198 * PEAR objects. If called in an object, setErrorHandling sets
199 * the default behaviour for that object.
200 *
201 * @param int $mode
202 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
203 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
204 * PEAR_ERROR_CALLBACK.
205 *
206 * @param mixed $options
207 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
208 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
209 *
210 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
211 * to be the callback function or method. A callback
212 * function is a string with the name of the function, a
213 * callback method is an array of two elements: the element
214 * at index 0 is the object, and the element at index 1 is
215 * the name of the method to call in the object.
216 *
217 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
218 * a printf format string used when printing the error
219 * message.
220 *
221 * @access public
222 * @return void
223 * @see PEAR_ERROR_RETURN
224 * @see PEAR_ERROR_PRINT
225 * @see PEAR_ERROR_TRIGGER
226 * @see PEAR_ERROR_DIE
227 * @see PEAR_ERROR_CALLBACK
228 *
229 * @since PHP 4.0.5
230 */
231
232 function setErrorHandling($mode = null, $options = null)
233 {
234 if (isset($this)) {
235 $setmode = &$this->_default_error_mode;
236 $setoptions = &$this->_default_error_options;
237 //$setcallback = &$this->_default_error_callback;
238 } else {
239 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
240 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
241 //$setcallback = &$GLOBALS['_PEAR_default_error_callback'];
242 }
243
244 switch ($mode) {
245 case PEAR_ERROR_RETURN:
246 case PEAR_ERROR_PRINT:
247 case PEAR_ERROR_TRIGGER:
248 case PEAR_ERROR_DIE:
249 case null:
250 $setmode = $mode;
251 $setoptions = $options;
252 break;
253
254 case PEAR_ERROR_CALLBACK:
255 $setmode = $mode;
256 if ((is_string($options) && function_exists($options)) ||
257 (is_array($options) && method_exists(@$options[0], @$options[1])))
258 {
259 $setoptions = $options;
260 } else {
261 trigger_error("invalid error callback", E_USER_WARNING);
262 }
263 break;
264
265 default:
266 trigger_error("invalid error mode", E_USER_WARNING);
267 break;
268 }
269 }
270
271 // }}}
272 // {{{ expectError()
273
274 /**
275 * This method is used to tell which errors you expect to get.
276 * Expected errors are always returned with error mode
277 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
278 * and this method pushes a new element onto it. The list of
279 * expected errors are in effect until they are popped off the
280 * stack with the popExpect() method.
281 *
282 * @param mixed a single error code or an array of error codes
283 * to expect
284 *
285 * @return int the new depth of the "expected errors" stack
286 */
287 function expectError($code = "*")
288 {
289 if (is_array($code)) {
290 array_push($this->_expected_errors, $code);
291 } else {
292 array_push($this->_expected_errors, array($code));
293 }
294 return sizeof($this->_expected_errors);
295 }
296
297 // }}}
298 // {{{ popExpect()
299
300 /**
301 * This method pops one element off the expected error codes
302 * stack.
303 *
304 * @return array the list of error codes that were popped
305 */
306 function popExpect()
307 {
308 return array_pop($this->_expected_errors);
309 }
310
311 // }}}
312 // {{{ raiseError()
313
314 /**
315 * This method is a wrapper that returns an instance of the
316 * configured error class with this object's default error
317 * handling applied. If the $mode and $options parameters are not
318 * specified, the object's defaults are used.
319 *
320 * @param $message a text error message or a PEAR error object
321 *
322 * @param $code a numeric error code (it is up to your class
323 * to define these if you want to use codes)
324 *
325 * @param $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
326 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
327 * PEAR_ERROR_CALLBACK.
328 *
329 * @param $options If $mode is PEAR_ERROR_TRIGGER, this parameter
330 * specifies the PHP-internal error level (one of
331 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
332 * If $mode is PEAR_ERROR_CALLBACK, this
333 * parameter specifies the callback function or
334 * method. In other error modes this parameter
335 * is ignored.
336 *
337 * @param $userinfo If you need to pass along for example debug
338 * information, this parameter is meant for that.
339 *
340 * @param $error_class The returned error object will be instantiated
341 * from this class, if specified.
342 *
343 * @param $skipmsg If true, raiseError will only pass error codes,
344 * the error message parameter will be dropped.
345 *
346 * @access public
347 * @return object a PEAR error object
348 * @see PEAR::setErrorHandling
349 * @since PHP 4.0.5
350 */
351 function &raiseError($message = null,
352 $code = null,
353 $mode = null,
354 $options = null,
355 $userinfo = null,
356 $error_class = null,
357 $skipmsg = false)
358 {
359 // The error is yet a PEAR error object
360 if (is_object($message)) {
361 $code = $message->getCode();
362 $userinfo = $message->getUserInfo();
363 $error_class = $message->getType();
364 $message = $message->getMessage();
365 }
366
367 if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
368 if ($exp[0] == "*" ||
369 (is_int(reset($exp)) && in_array($code, $exp)) ||
370 (is_string(reset($exp)) && in_array($message, $exp))) {
371 $mode = PEAR_ERROR_RETURN;
372 }
373 }
374
375 if ($mode === null) {
376 if (isset($this) && isset($this->_default_error_mode)) {
377 $mode = $this->_default_error_mode;
378 } else {
379 $mode = $GLOBALS['_PEAR_default_error_mode'];
380 }
381 }
382
383 if ($mode == PEAR_ERROR_TRIGGER && $options === null) {
384 if (isset($this)) {
385 if (isset($this->_default_error_options)) {
386 $options = $this->_default_error_options;
387 }
388 } else {
389 $options = $GLOBALS['_PEAR_default_error_options'];
390 }
391 }
392
393 if ($mode == PEAR_ERROR_CALLBACK) {
394 if (!is_string($options) &&
395 !(is_array($options) && sizeof($options) == 2 &&
396 is_object($options[0]) && is_string($options[1])))
397 {
398 if (isset($this) && isset($this->_default_error_options)) {
399 $options = $this->_default_error_options;
400 } else {
401 $options = $GLOBALS['_PEAR_default_error_options'];
402 }
403 }
404 } else {
405 if ($options === null) {
406 if (isset($this)) {
407 if (isset($this->_default_error_options)) {
408 $options = $this->_default_error_options;
409 }
410 } else {
411 $options = $GLOBALS['_PEAR_default_error_options'];
412 }
413 }
414 }
415 if ($error_class !== null) {
416 $ec = $error_class;
417 } elseif (isset($this) && isset($this->_error_class)) {
418 $ec = $this->_error_class;
419 } else {
420 $ec = 'PEAR_Error';
421 }
422 if ($skipmsg) {
423 return new $ec($code, $mode, $options, $userinfo);
424 } else {
425 return new $ec($message, $code, $mode, $options, $userinfo);
426 }
427 }
428
429 // }}}
430 // {{{ pushErrorHandling()
431
432 /**
433 * Push a new error handler on top of the error handler options stack. With this
434 * you can easily override the actual error handler for some code and restore
435 * it later with popErrorHandling.
436 *
437 * @param $mode mixed (same as setErrorHandling)
438 * @param $options mixed (same as setErrorHandling)
439 *
440 * @return bool Always true
441 *
442 * @see PEAR::setErrorHandling
443 */
444 function pushErrorHandling($mode, $options = null)
445 {
446 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
447 if (!is_array($stack)) {
448 if (isset($this)) {
449 $def_mode = &$this->_default_error_mode;
450 $def_options = &$this->_default_error_options;
451 // XXX Used anywhere?
452 //$def_callback = &$this->_default_error_callback;
453 } else {
454 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
455 $def_options = &$GLOBALS['_PEAR_default_error_options'];
456 // XXX Used anywhere?
457 //$def_callback = &$GLOBALS['_PEAR_default_error_callback'];
458 }
459 $stack = array();
460 $stack[] = array($def_mode, $def_options);
461 }
462
463 if (isset($this)) {
464 $this->setErrorHandling($mode, $options);
465 } else {
466 PEAR::setErrorHandling($mode, $options);
467 }
468 $stack[] = array($mode, $options);
469 return true;
470 }
471
472 // }}}
473 // {{{ popErrorHandling()
474
475 /**
476 * Pop the last error handler used
477 *
478 * @return bool Always true
479 *
480 * @see PEAR::pushErrorHandling
481 */
482 function popErrorHandling()
483 {
484 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
485 array_pop($stack);
486 list($mode, $options) = $stack[sizeof($stack) - 1];
487 if (isset($this)) {
488 $this->setErrorHandling($mode, $options);
489 } else {
490 PEAR::setErrorHandling($mode, $options);
491 }
492 return true;
493 }
494
495 // }}}
496
497 function &getStaticProperty($class, $var)
498 {
499 static $properties;
500 return $properties[$class][$var];
501 }
502
503 }
504
505 // {{{ _PEAR_call_destructors()
506
507 function _PEAR_call_destructors()
508 {
509 global $_PEAR_destructor_object_list;
510 if (is_array($_PEAR_destructor_object_list) &&
511 sizeof($_PEAR_destructor_object_list))
512 {
513 reset($_PEAR_destructor_object_list);
514 while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
515 $classname = get_class($objref);
516 while ($classname) {
517 $destructor = "_$classname";
518 if (method_exists($objref, $destructor)) {
519 $objref->$destructor();
520 break;
521 } else {
522 $classname = get_parent_class($classname);
523 }
524 }
525 }
526 // Empty the object list to ensure that destructors are
527 // not called more than once.
528 $_PEAR_destructor_object_list = array();
529 }
530 }
531
532 // }}}
533
534 class PEAR_Error
535 {
536 // {{{ properties
537
538 var $error_message_prefix = '';
539 var $mode = PEAR_ERROR_RETURN;
540 var $level = E_USER_NOTICE;
541 var $code = -1;
542 var $message = '';
543 var $userinfo = '';
544
545 // Wait until we have a stack-groping function in PHP.
546 //var $file = '';
547 //var $line = 0;
548
549
550 // }}}
551 // {{{ constructor
552
553 /**
554 * PEAR_Error constructor
555 *
556 * @param $message error message
557 *
558 * @param $code (optional) error code
559 *
560 * @param $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
561 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or
562 * PEAR_ERROR_CALLBACK
563 *
564 * @param $level (optional) error level, _OR_ in the case of
565 * PEAR_ERROR_CALLBACK, the callback function or object/method
566 * tuple.
567 *
568 * @access public
569 *
570 */
571 function PEAR_Error($message = 'unknown error', $code = null,
572 $mode = null, $options = null, $userinfo = null)
573 {
574 if ($mode === null) {
575 $mode = PEAR_ERROR_RETURN;
576 }
577 $this->message = $message;
578 $this->code = $code;
579 $this->mode = $mode;
580 $this->userinfo = $userinfo;
581 if ($mode & PEAR_ERROR_CALLBACK) {
582 $this->level = E_USER_NOTICE;
583 $this->callback = $options;
584 } else {
585 if ($options === null) {
586 $options = E_USER_NOTICE;
587 }
588 $this->level = $options;
589 $this->callback = null;
590 }
591 if ($this->mode & PEAR_ERROR_PRINT) {
592 if (is_null($options) || is_int($options)) {
593 $format = "%s";
594 } else {
595 $format = $options;
596 }
597 printf($format, $this->getMessage());
598 }
599 if ($this->mode & PEAR_ERROR_TRIGGER) {
600 trigger_error($this->getMessage(), $this->level);
601 }
602 if ($this->mode & PEAR_ERROR_DIE) {
603 $msg = $this->getMessage();
604 if (is_null($options) || is_int($options)) {
605 $format = "%s";
606 if (substr($msg, -1) != "\n") {
607 $msg .= "\n";
608 }
609 } else {
610 $format = $options;
611 }
612 die(sprintf($format, $msg));
613 }
614 if ($this->mode & PEAR_ERROR_CALLBACK) {
615 if (is_string($this->callback) && strlen($this->callback)) {
616 call_user_func($this->callback, $this);
617 } elseif (is_array($this->callback) &&
618 sizeof($this->callback) == 2 &&
619 is_object($this->callback[0]) &&
620 is_string($this->callback[1]) &&
621 strlen($this->callback[1])) {
622 @call_user_method($this->callback[1], $this->callback[0],
623 $this);
624 }
625 }
626 }
627
628 // }}}
629 // {{{ getMode()
630
631 /**
632 * Get the error mode from an error object.
633 *
634 * @return int error mode
635 * @access public
636 */
637 function getMode() {
638 return $this->mode;
639 }
640
641 // }}}
642 // {{{ getCallback()
643
644 /**
645 * Get the callback function/method from an error object.
646 *
647 * @return mixed callback function or object/method array
648 * @access public
649 */
650 function getCallback() {
651 return $this->callback;
652 }
653
654 // }}}
655 // {{{ getMessage()
656
657
658 /**
659 * Get the error message from an error object.
660 *
661 * @return string full error message
662 * @access public
663 */
664 function getMessage ()
665 {
666 return ($this->error_message_prefix . $this->message);
667 }
668
669
670 // }}}
671 // {{{ getCode()
672
673 /**
674 * Get error code from an error object
675 *
676 * @return int error code
677 * @access public
678 */
679 function getCode()
680 {
681 return $this->code;
682 }
683
684 // }}}
685 // {{{ getType()
686
687 /**
688 * Get the name of this error/exception.
689 *
690 * @return string error/exception name (type)
691 * @access public
692 */
693 function getType ()
694 {
695 return get_class($this);
696 }
697
698 // }}}
699 // {{{ getUserInfo()
700
701 /**
702 * Get additional user-supplied information.
703 *
704 * @return string user-supplied information
705 * @access public
706 */
707 function getUserInfo ()
708 {
709 return $this->userinfo;
710 }
711
712 // }}}
713 // {{{ getDebugInfo()
714
715 /**
716 * Get additional debug information supplied by the application.
717 *
718 * @return string debug information
719 * @access public
720 */
721 function getDebugInfo ()
722 {
723 return $this->getUserInfo();
724 }
725
726 // }}}
727 // {{{ addUserInfo()
728
729 function addUserInfo($info)
730 {
731 if (empty($this->userinfo)) {
732 $this->userinfo = $info;
733 } else {
734 $this->userinfo .= " ** $info";
735 }
736 }
737
738 // }}}
739 // {{{ toString()
740
741 /**
742 * Make a string representation of this object.
743 *
744 * @return string a string with an object summary
745 * @access public
746 */
747 function toString() {
748 $modes = array();
749 $levels = array(E_USER_NOTICE => 'notice',
750 E_USER_WARNING => 'warning',
751 E_USER_ERROR => 'error');
752 if ($this->mode & PEAR_ERROR_CALLBACK) {
753 if (is_array($this->callback)) {
754 $callback = get_class($this->callback[0]) . '::' .
755 $this->callback[1];
756 } else {
757 $callback = $this->callback;
758 }
759 return sprintf('[%s: message="%s" code=%d mode=callback '.
760 'callback=%s prefix="%s" info="%s"]',
761 get_class($this), $this->message, $this->code,
762 $callback, $this->error_message_prefix,
763 $this->userinfo);
764 }
765 if ($this->mode & PEAR_ERROR_CALLBACK) {
766 $modes[] = 'callback';
767 }
768 if ($this->mode & PEAR_ERROR_PRINT) {
769 $modes[] = 'print';
770 }
771 if ($this->mode & PEAR_ERROR_TRIGGER) {
772 $modes[] = 'trigger';
773 }
774 if ($this->mode & PEAR_ERROR_DIE) {
775 $modes[] = 'die';
776 }
777 if ($this->mode & PEAR_ERROR_RETURN) {
778 $modes[] = 'return';
779 }
780 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
781 'prefix="%s" info="%s"]',
782 get_class($this), $this->message, $this->code,
783 implode("|", $modes), $levels[$this->level],
784 $this->error_message_prefix,
785 $this->userinfo);
786 }
787
788 // }}}
789 }
790
791 register_shutdown_function("_PEAR_call_destructors");
792
793 /*
794 * Local Variables:
795 * mode: php
796 * tab-width: 4
797 * c-basic-offset: 4
798 * End:
799 */
800 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed