From: Stig Bakken Date: Wed, 13 Sep 2000 07:39:54 +0000 (+0000) Subject: PEAR: X-Git-Tag: php-4.0.3RC1~149 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=63aec84825de5654f3ccedc74466e73654c6ff3f;p=php PEAR: * show what's going on when PEAR files are installed * allow multiple modes (or'ed) in PEAR_Error @PEAR: allow multiple modes in PEAR_Error (Stig) --- diff --git a/pear/Makefile.in b/pear/Makefile.in index 125f467d1f..f39f2b1d79 100644 --- a/pear/Makefile.in +++ b/pear/Makefile.in @@ -41,15 +41,15 @@ PEAR_FILES = \ install-data-local: PEAR.php @if $(mkinstalldirs) $(peardir); then \ for i in $(PEAR_SUBDIRS); do \ - $(mkinstalldirs) $(peardir)/$$i; \ + (set -x;$(mkinstalldirs) $(peardir)/$$i); \ done; \ for i in $(PEAR_FILES); do \ dir=`echo $$i|sed 's%[^/][^/]*$$%%'`; \ - $(INSTALL_DATA) $(srcdir)/$$i $(peardir)/$$dir; \ + (set -x;$(INSTALL_DATA) $(srcdir)/$$i $(peardir)/$$dir); \ done; \ for i in PEAR.php; do \ dir=`echo $$i|sed 's%[^/][^/]*$$%%'`; \ - $(INSTALL_DATA) $(builddir)/$$i $(peardir)/$$dir; \ + (set -x;$(INSTALL_DATA) $(builddir)/$$i $(peardir)/$$dir); \ done; \ else \ cat $(srcdir)/install-pear.txt; \ diff --git a/pear/PEAR.php.in b/pear/PEAR.php.in index dbee200037..92f48986c2 100644 --- a/pear/PEAR.php.in +++ b/pear/PEAR.php.in @@ -20,11 +20,11 @@ // $Id$ // -define('PEAR_ERROR_RETURN', 0); -define('PEAR_ERROR_PRINT', 1); -define('PEAR_ERROR_TRIGGER', 2); -define('PEAR_ERROR_DIE', 3); -define('PEAR_ERROR_CALLBACK', 4); +define('PEAR_ERROR_RETURN', 1); +define('PEAR_ERROR_PRINT', 2); +define('PEAR_ERROR_TRIGGER', 4); +define('PEAR_ERROR_DIE', 8); +define('PEAR_ERROR_CALLBACK', 16); define('PHP_BINDIR', '@prefix@/bin'); define('PEAR_INSTALL_DIR', '@PEAR_INSTALLDIR@'); @@ -177,44 +177,45 @@ class PEAR_Error */ function PEAR_Error($message = 'unknown error', $code = 0, - $mode = PEAR_ERROR_RETURN, - $level = E_USER_NOTICE) + $mode = null, + $options = null) { + if ($mode === null) { + $mode = PEAR_ERROR_RETURN; + } $this->message = $message; $this->code = $code; $this->mode = $mode; - if ($mode == PEAR_ERROR_CALLBACK) { + if ($mode & PEAR_ERROR_CALLBACK) { $this->level = E_USER_NOTICE; - $this->callback = $level; + $this->callback = $options; } else { - $this->level = $level; - $this->callback = false; + if ($options === null) { + $options = E_USER_NOTICE; + } + $this->level = $options; + $this->callback = null; + } + if ($this->mode & PEAR_ERROR_PRINT) { + print $this->getMessage(); + } + if ($this->mode & PEAR_ERROR_TRIGGER) { + trigger_error($this->getMessage(), $this->level); } - switch ($this->mode) { - case PEAR_ERROR_PRINT: - print $this->getMessage(); - break; - case PEAR_ERROR_TRIGGER: - trigger_error($this->getMessage(), $this->level); - break; - case PEAR_ERROR_DIE: - die($this->getMessage()); - break; - case PEAR_ERROR_CALLBACK: - if (is_string($this->callback) && strlen($this->callback)) { - call_user_func($this->callback, $this); - } elseif (is_array($this->callback) && - sizeof($this->callback) == 2 && - is_object($this->callback[0]) && - is_string($this->callback[1]) && - strlen($this->callback[1])) { - call_user_method($this->callback[1], $this->callback[0], - $this); - } - break; - case PEAR_ERROR_RETURN: - default: - break; + if ($this->mode & PEAR_ERROR_DIE) { + die($this->getMessage()); + } + if ($this->mode & PEAR_ERROR_CALLBACK) { + if (is_string($this->callback) && strlen($this->callback)) { + call_user_func($this->callback, $this); + } elseif (is_array($this->callback) && + sizeof($this->callback) == 2 && + is_object($this->callback[0]) && + is_string($this->callback[1]) && + strlen($this->callback[1])) { + call_user_method($this->callback[1], $this->callback[0], + $this); + } } } @@ -283,19 +284,43 @@ class PEAR_Error * @return string a string with an object "summary" */ function toString() { - $modes = array(PEAR_ERROR_RETURN => "return", - PEAR_ERROR_PRINT => "print", - PEAR_ERROR_TRIGGER => "trigger", - PEAR_ERROR_DIE => "die", - PEAR_ERROR_CALLBACK => "callback"); + $modes = array(); $levels = array(E_USER_NOTICE => "notice", E_USER_WARNING => "warning", E_USER_ERROR => "error"); - return sprintf("[%s: message=\"%s\" code=%d mode=%s level=%s prefix=\"%s\" prepend=\"%s\" append=\"%s\"]", + if ($this->mode & PEAR_ERROR_CALLBACK) { + if (is_array($this->callback)) { + $callback = get_class($this->callback[0]) . "::" . + $this->callback[1]; + } else { + $callback = $this->callback; + } + return sprintf('[%s: message="%s" code=%d mode=callback '. + 'callback=%s prefix="%s" prepend="%s" append="%s"]', + get_class($this), $this->message, $this->code, + $callback, $this->error_message_prefix, + $this->error_prepend, $this->error_append); + } + if ($this->mode & PEAR_ERROR_CALLBACK) { + $modes[] = "callback"; + } + if ($this->mode & PEAR_ERROR_PRINT) { + $modes[] = "print"; + } + if ($this->mode & PEAR_ERROR_TRIGGER) { + $modes[] = "trigger"; + } + if ($this->mode & PEAR_ERROR_DIE) { + $modes[] = "die"; + } + if ($this->mode & PEAR_ERROR_RETURN) { + $modes[] = "return"; + } + return sprintf('[%s: message="%s" code=%d mode=%s level=%s prefix="%s" prepend="%s" append="%s"]', get_class($this), $this->message, $this->code, - $modes[$this->mode], $levels[$this->level], - $this->error_message_prefix, $this->error_prepend, - $this->error_append); + implode("|", $modes), $levels[$this->level], + $this->error_message_prefix, + $this->error_prepend, $this->error_append); } // }}} diff --git a/pear/tests/pear_error.phpt b/pear/tests/pear_error.phpt index 851900cfe9..6fe45f4eeb 100644 --- a/pear/tests/pear_error.phpt +++ b/pear/tests/pear_error.phpt @@ -13,6 +13,18 @@ require_once "PEAR.php"; error_reporting(4095); +function errorhandler(&$obj) { + print "errorhandler function called, obj=".$obj->toString()."\n"; +} + +class errorclass { + function errorhandler(&$obj) { + print "errorhandler method called, obj=".$obj->toString()."\n"; + } +} + +$eo = new errorclass; + print "default PEAR_Error: "; $err = new PEAR_Error; print $err->toString() . "\n"; @@ -23,24 +35,42 @@ $str = "not an error"; var_dump(PEAR::isError($str)); print "Now trying a bunch of variations...\n"; + print "different message: "; $err = new PEAR_Error("test error"); print $err->toString() . "\n"; + print "different message,code: "; $err = new PEAR_Error("test error", -42); print $err->toString() . "\n"; + print "mode=print: "; $err = new PEAR_Error("test error", -42, PEAR_ERROR_PRINT); print $err->toString() . "\n"; + +print "mode=callback(function): "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_CALLBACK, "errorhandler"); + +print "mode=callback(method): "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_CALLBACK, + array(&$eo, "errorhandler")); + +print "mode=print&trigger: "; +$err = new PEAR_Error("test error", -42, PEAR_ERROR_PRINT|PEAR_ERROR_TRIGGER); +print $err->toString() . "\n"; + print "mode=trigger: "; $err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER); print $err->toString() . "\n"; + print "mode=trigger,level=notice: "; $err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_NOTICE); print $err->toString() . "\n"; + print "mode=trigger,level=warning: "; $err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_WARNING); print $err->toString() . "\n"; + print "mode=trigger,level=error: "; $err = new PEAR_Error("test error", -42, PEAR_ERROR_TRIGGER, E_USER_ERROR); print $err->toString() . "\n"; @@ -56,14 +86,19 @@ Now trying a bunch of variations... different message: [pear_error: message="test error" code=0 mode=return level=notice prefix="" prepend="" append=""] different message,code: [pear_error: message="test error" code=-42 mode=return level=notice prefix="" prepend="" append=""] mode=print: test error[pear_error: message="test error" code=-42 mode=print level=notice prefix="" prepend="" append=""] +mode=callback(function): errorhandler function called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorhandler prefix="" prepend="" append=""] +mode=callback(method): errorhandler method called, obj=[pear_error: message="test error" code=-42 mode=callback callback=errorclass::errorhandler prefix="" prepend="" append=""] +mode=print&trigger: test error
+Notice: test error in PEAR.php on line 203
+[pear_error: message="test error" code=-42 mode=print|trigger level=notice prefix="" prepend="" append=""] mode=trigger:
-Notice: test error in PEAR.php on line 198
+Notice: test error in PEAR.php on line 203
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] mode=trigger,level=notice:
-Notice: test error in PEAR.php on line 198
+Notice: test error in PEAR.php on line 203
[pear_error: message="test error" code=-42 mode=trigger level=notice prefix="" prepend="" append=""] mode=trigger,level=warning:
-Warning: test error in PEAR.php on line 198
+Warning: test error in PEAR.php on line 203
[pear_error: message="test error" code=-42 mode=trigger level=warning prefix="" prepend="" append=""] mode=trigger,level=error:
-Fatal error: test error in PEAR.php on line 198
+Fatal error: test error in PEAR.php on line 203
diff --git a/pear/tests/pear_error_callback.phpt b/pear/tests/pear_error_callback.phpt deleted file mode 100644 index 4524c64607..0000000000 --- a/pear/tests/pear_error_callback.phpt +++ /dev/null @@ -1,32 +0,0 @@ ---TEST-- -PEAR_Error in callback mode ---SKIPIF-- ---FILE-- -toString(); - print "\n"; -} -class myclass { - function error_method($obj) { - print "this is myclass::error_method reporting: "; - print $obj->toString(); - print "\n"; - } -} -$obj = new myclass; -new PEAR_Error("errortest1", 0, PEAR_ERROR_CALLBACK, "error_function"); -new PEAR_Error("errortest2", 0, PEAR_ERROR_CALLBACK, - array(&$obj, "error_method")); - - -?> ---GET-- ---POST-- ---EXPECT-- -this is error_function reporting: [pear_error: message="errortest1" code=0 mode=callback level=notice prefix="" prepend="" append=""] -this is myclass::error_method reporting: [pear_error: message="errortest2" code=0 mode=callback level=notice prefix="" prepend="" append=""]