cvs: php-src /pear/PEAR ErrorStack.php

From: Date: Sun, 23 May 2004 06:06:10 +0000
Subject: cvs: php-src /pear/PEAR ErrorStack.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-core+get-339@lists.php.net to get a copy of this message
cellog Sun May 23 02:06:10 2004 EDT Modified files: /php-src/pear/PEAR ErrorStack.php Log: Fix #1395: PEAR_ErrorStack::staticCallback needs to instantiate the stack Fix up default callback, so that it can be set for each package as well as for all packages. Callbacks now have 3 levels of control, which may be a bit too complex, default callbacks might have to go for debugging reasons. Either that or some kind of origination information should be appended to the error array describing which callbacks were called, so debugging is at least possible http://cvs.php.net/diff.php/php-src/pear/PEAR/ErrorStack.php?r1=1.5&r2=1.6&ty=u Index: php-src/pear/PEAR/ErrorStack.php diff -u php-src/pear/PEAR/ErrorStack.php:1.5 php-src/pear/PEAR/ErrorStack.php:1.6 --- php-src/pear/PEAR/ErrorStack.php:1.5 Thu May 20 23:43:58 2004 +++ php-src/pear/PEAR/ErrorStack.php Sun May 23 02:06:10 2004 @@ -17,7 +17,7 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: ErrorStack.php,v 1.5 2004/05/21 03:43:58 cellog Exp $ +// $Id: ErrorStack.php,v 1.6 2004/05/23 06:06:10 cellog Exp $ /** * Error Stack Implementation @@ -61,23 +61,41 @@ /** * Global error callback (default) * - * This is only used + * This is only used if set to non-false. * is the default callback for + * all packages, whereas specific packages may set a default callback + * for all instances, regardless of whether they are a singleton or not. + * + * To exclude non-singletons, only set the local callback for the singleton * @see PEAR_ErrorStack::setDefaultCallback() * @access private * @global array $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'] */ -$GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'] = false; +$GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'] = array( + '*' => false, +); /** * Global Log object (default) * - * This is only used + * This is only used if set to non-false. Use to set a default log object for + * all stacks, regardless of instantiation order or location * @see PEAR_ErrorStack::setDefaultLogger() * @access private * @global array $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] */ $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] = false; +/** + * Global Overriding Callback + * + * This callback will override any error callbacks that specific loggers have set. + * Use with EXTREME caution + * @see PEAR_ErrorStack::staticPushCallback() + * @access private + * @global array $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_LOGGER'] + */ +$GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array(); + /**#@+ * One of four possible return values from the error Callback * @see PEAR_ErrorStack::_errorCallback() @@ -350,14 +368,16 @@ * This method sets the callback that can be used to generate error * messages for a singleton * @param array|string Callback function/method + * @param string Package name, or false for all packages * @static */ - function setDefaultCallback($callback = false) + function setDefaultCallback($callback = false, $package = false) { if (!is_callable($callback)) { $callback = false; } - $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'] = $callback; + $package = $package ? $package : '*'; + $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'][$package] = $callback; } /** @@ -412,7 +432,10 @@ } /** - * Set an error Callback for every package error stack + * Set a temporary overriding error callback for every package error stack + * + * Use this to temporarily disable all existing callbacks (can be used + * to emulate the @ operator, for instance) * @see PEAR_ERRORSTACK_PUSHANDLOG, PEAR_ERRORSTACK_PUSH, PEAR_ERRORSTACK_LOG * @see staticPopCallback(), pushCallback() * @param string|array $cb @@ -420,21 +443,20 @@ */ function staticPushCallback($cb) { - foreach($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'] as $package => $unused) { - $GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package]->pushCallback($cb); - } + array_push($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'], $cb); } /** - * Remove a callback from every error callback stack + * Remove a temporary overriding error callback * @see staticPushCallback() * @return array|string|false * @static */ function staticPopCallback() { - foreach($GLOBALS['_PEAR_ERRORSTACK_SINGLETON'] as $package => $unused) { - $GLOBALS['_PEAR_ERRORSTACK_SINGLETON'][$package]->popCallback(); + array_pop($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK']); + if (!is_array($GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'])) { + $GLOBALS['_PEAR_ERRORSTACK_OVERRIDE_CALLBACK'] = array(); } } @@ -523,9 +545,24 @@ $err['repackage'] = $repackage; } $push = $log = true; - $callback = $this->popCallback(); + // try the overriding callback first + $callback = $this->staticPopCallback(); + if ($callback) { + $this->staticPushCallback($callback); + } + if (!is_callable($callback)) { + // try the local callback next + $callback = $this->popCallback(); + if (is_callable($callback)) { + $this->pushCallback($callback); + } else { + // try the default callback + $callback = isset($GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'][$this->_package]) ? + $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'][$this->_package] : + $GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK']['*']; + } + } if (is_callable($callback)) { - $this->pushCallback($callback); switch(call_user_func($callback, $err)){ case PEAR_ERRORSTACK_IGNORE: return $err; @@ -538,19 +575,6 @@ break; // anything else returned has the same effect as pushandlog } - } elseif (is_callable($GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'])) { - switch(call_user_func($GLOBALS['_PEAR_ERRORSTACK_DEFAULT_CALLBACK'], $err)){ - case PEAR_ERRORSTACK_IGNORE: - return $err; - break; - case PEAR_ERRORSTACK_PUSH: - $log = false; - break; - case PEAR_ERRORSTACK_LOG: - $push = false; - break; - // anything else returned has the same effect as pushandlog - } } if ($push) { array_unshift($this->_errors, $err);

« previous php.pear.core (#339) next »