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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Wed Feb 5 20:11:26 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
+ initial commit

1 joko 1.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    
498     // {{{ _PEAR_call_destructors()
499    
500     function _PEAR_call_destructors()
501     {
502     global $_PEAR_destructor_object_list;
503     if (is_array($_PEAR_destructor_object_list) &&
504     sizeof($_PEAR_destructor_object_list))
505     {
506     reset($_PEAR_destructor_object_list);
507     while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
508     $classname = get_class($objref);
509     while ($classname) {
510     $destructor = "_$classname";
511     if (method_exists($objref, $destructor)) {
512     $objref->$destructor();
513     break;
514     } else {
515     $classname = get_parent_class($classname);
516     }
517     }
518     }
519     // Empty the object list to ensure that destructors are
520     // not called more than once.
521     $_PEAR_destructor_object_list = array();
522     }
523     }
524    
525     // }}}
526    
527     class PEAR_Error
528     {
529     // {{{ properties
530    
531     var $error_message_prefix = '';
532     var $mode = PEAR_ERROR_RETURN;
533     var $level = E_USER_NOTICE;
534     var $code = -1;
535     var $message = '';
536     var $userinfo = '';
537    
538     // Wait until we have a stack-groping function in PHP.
539     //var $file = '';
540     //var $line = 0;
541    
542    
543     // }}}
544     // {{{ constructor
545    
546     /**
547     * PEAR_Error constructor
548     *
549     * @param $message error message
550     *
551     * @param $code (optional) error code
552     *
553     * @param $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
554     * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or
555     * PEAR_ERROR_CALLBACK
556     *
557     * @param $level (optional) error level, _OR_ in the case of
558     * PEAR_ERROR_CALLBACK, the callback function or object/method
559     * tuple.
560     *
561     * @access public
562     *
563     */
564     function PEAR_Error($message = 'unknown error', $code = null,
565     $mode = null, $options = null, $userinfo = null)
566     {
567     if ($mode === null) {
568     $mode = PEAR_ERROR_RETURN;
569     }
570     $this->message = $message;
571     $this->code = $code;
572     $this->mode = $mode;
573     $this->userinfo = $userinfo;
574     if ($mode & PEAR_ERROR_CALLBACK) {
575     $this->level = E_USER_NOTICE;
576     $this->callback = $options;
577     } else {
578     if ($options === null) {
579     $options = E_USER_NOTICE;
580     }
581     $this->level = $options;
582     $this->callback = null;
583     }
584     if ($this->mode & PEAR_ERROR_PRINT) {
585     if (is_null($options) || is_int($options)) {
586     $format = "%s";
587     } else {
588     $format = $options;
589     }
590     printf($format, $this->getMessage());
591     }
592     if ($this->mode & PEAR_ERROR_TRIGGER) {
593     trigger_error($this->getMessage(), $this->level);
594     }
595     if ($this->mode & PEAR_ERROR_DIE) {
596     $msg = $this->getMessage();
597     if (is_null($options) || is_int($options)) {
598     $format = "%s";
599     if (substr($msg, -1) != "\n") {
600     $msg .= "\n";
601     }
602     } else {
603     $format = $options;
604     }
605     die(sprintf($format, $msg));
606     }
607     if ($this->mode & PEAR_ERROR_CALLBACK) {
608     if (is_string($this->callback) && strlen($this->callback)) {
609     call_user_func($this->callback, $this);
610     } elseif (is_array($this->callback) &&
611     sizeof($this->callback) == 2 &&
612     is_object($this->callback[0]) &&
613     is_string($this->callback[1]) &&
614     strlen($this->callback[1])) {
615     @call_user_method($this->callback[1], $this->callback[0],
616     $this);
617     }
618     }
619     }
620    
621     // }}}
622     // {{{ getMode()
623    
624     /**
625     * Get the error mode from an error object.
626     *
627     * @return int error mode
628     * @access public
629     */
630     function getMode() {
631     return $this->mode;
632     }
633    
634     // }}}
635     // {{{ getCallback()
636    
637     /**
638     * Get the callback function/method from an error object.
639     *
640     * @return mixed callback function or object/method array
641     * @access public
642     */
643     function getCallback() {
644     return $this->callback;
645     }
646    
647     // }}}
648     // {{{ getMessage()
649    
650    
651     /**
652     * Get the error message from an error object.
653     *
654     * @return string full error message
655     * @access public
656     */
657     function getMessage ()
658     {
659     return ($this->error_message_prefix . $this->message);
660     }
661    
662    
663     // }}}
664     // {{{ getCode()
665    
666     /**
667     * Get error code from an error object
668     *
669     * @return int error code
670     * @access public
671     */
672     function getCode()
673     {
674     return $this->code;
675     }
676    
677     // }}}
678     // {{{ getType()
679    
680     /**
681     * Get the name of this error/exception.
682     *
683     * @return string error/exception name (type)
684     * @access public
685     */
686     function getType ()
687     {
688     return get_class($this);
689     }
690    
691     // }}}
692     // {{{ getUserInfo()
693    
694     /**
695     * Get additional user-supplied information.
696     *
697     * @return string user-supplied information
698     * @access public
699     */
700     function getUserInfo ()
701     {
702     return $this->userinfo;
703     }
704    
705     // }}}
706     // {{{ getDebugInfo()
707    
708     /**
709     * Get additional debug information supplied by the application.
710     *
711     * @return string debug information
712     * @access public
713     */
714     function getDebugInfo ()
715     {
716     return $this->getUserInfo();
717     }
718    
719     // }}}
720     // {{{ addUserInfo()
721    
722     function addUserInfo($info)
723     {
724     if (empty($this->userinfo)) {
725     $this->userinfo = $info;
726     } else {
727     $this->userinfo .= " ** $info";
728     }
729     }
730    
731     // }}}
732     // {{{ toString()
733    
734     /**
735     * Make a string representation of this object.
736     *
737     * @return string a string with an object summary
738     * @access public
739     */
740     function toString() {
741     $modes = array();
742     $levels = array(E_USER_NOTICE => 'notice',
743     E_USER_WARNING => 'warning',
744     E_USER_ERROR => 'error');
745     if ($this->mode & PEAR_ERROR_CALLBACK) {
746     if (is_array($this->callback)) {
747     $callback = get_class($this->callback[0]) . '::' .
748     $this->callback[1];
749     } else {
750     $callback = $this->callback;
751     }
752     return sprintf('[%s: message="%s" code=%d mode=callback '.
753     'callback=%s prefix="%s" info="%s"]',
754     get_class($this), $this->message, $this->code,
755     $callback, $this->error_message_prefix,
756     $this->userinfo);
757     }
758     if ($this->mode & PEAR_ERROR_CALLBACK) {
759     $modes[] = 'callback';
760     }
761     if ($this->mode & PEAR_ERROR_PRINT) {
762     $modes[] = 'print';
763     }
764     if ($this->mode & PEAR_ERROR_TRIGGER) {
765     $modes[] = 'trigger';
766     }
767     if ($this->mode & PEAR_ERROR_DIE) {
768     $modes[] = 'die';
769     }
770     if ($this->mode & PEAR_ERROR_RETURN) {
771     $modes[] = 'return';
772     }
773     return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
774     'prefix="%s" info="%s"]',
775     get_class($this), $this->message, $this->code,
776     implode("|", $modes), $levels[$this->level],
777     $this->error_message_prefix,
778     $this->userinfo);
779     }
780    
781     // }}}
782     }
783    
784     register_shutdown_function("_PEAR_call_destructors");
785    
786     /*
787     * Local Variables:
788     * mode: php
789     * tab-width: 4
790     * c-basic-offset: 4
791     * End:
792     */
793     ?>

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