cvs: pear-core /PEAR Exception.php

From: Date: Thu, 02 Sep 2004 03:32:23 +0000
Subject: cvs: pear-core /PEAR Exception.php
Groups: php.pear.cvs php.pear.core 
Request: Send a blank email to pear-cvs+get-23572@lists.php.net to get a copy of this message
cellog Wed Sep 1 23:32:23 2004 EDT Modified files: /pear-core/PEAR Exception.php Log: add placeholder for grabbing ErrorData from an exception that implements this option. Removes the need for an interface, and defines how to get the information for those exceptions that do without interfering with barebones. add a new PEAR_EXCEPTION_ constant for a class-specific callback. This will be useful if you want a callback to be triggered only for a particular package's exception classes, or even a sub-category of exception classes. http://cvs.php.net/diff.php/pear-core/PEAR/Exception.php?r1=1.11&r2=1.12&ty=u Index: pear-core/PEAR/Exception.php diff -u pear-core/PEAR/Exception.php:1.11 pear-core/PEAR/Exception.php:1.12 --- pear-core/PEAR/Exception.php:1.11 Wed Sep 1 22:57:56 2004 +++ pear-core/PEAR/Exception.php Wed Sep 1 23:32:23 2004 @@ -16,13 +16,15 @@ // | Authors: Tomas V.V.Cox <cox@idecnet.com> | // | Hans Lellelid <hans@velum.net> | // | Bertrand Mansion <bmansion@mamasam.com> | +// | Greg Beaver <cellog@php.net> | // +----------------------------------------------------------------------+ // -// $Id: Exception.php,v 1.11 2004/09/02 02:57:56 cellog Exp $ +// $Id: Exception.php,v 1.12 2004/09/02 03:32:23 cellog Exp $ -define('PEAR_OBSERVER_PRINT', -2); -define('PEAR_OBSERVER_TRIGGER', -4); -define('PEAR_OBSERVER_DIE', -8); +define('PEAR_OBSERVER_PRINT', -2); +define('PEAR_OBSERVER_TRIGGER', -4); +define('PEAR_OBSERVER_DIE', -8); +define('PEAR_OBSERVER_CLASSCALLBACK', -16); /** * Base PEAR_Exception Class @@ -35,6 +37,7 @@ * - Added more context info available (like class, method or cause) * - cause can be a PEAR_Exception or an array of mixed * PEAR_Exceptions/PEAR_ErrorStack warnings + * - callbacks for specific exception classes and their children * * 2) Ideas: * @@ -87,7 +90,7 @@ * * @since PHP 5 * @package PEAR - * @version $Revision: 1.11 $ + * @version $Revision: 1.12 $ * @author Tomas V.V.Cox <cox@idecnet.com> * @author Hans Lellelid <hans@velum.net> * @author Bertrand Mansion <bmansion@mamasam.com> @@ -121,7 +124,7 @@ $this->cause = null; } parent::__construct($message, $code); - $this->_signal(); + $this->signal(); } /** @@ -142,7 +145,10 @@ unset(self::$_observers[$label]); } - private function _signal() + /** + * Additional signal handling can be added or overridden by child classes + */ + protected function signal() { foreach (self::$_observers as $func) { if (is_callable($func)) { @@ -151,18 +157,28 @@ } settype($func, 'array'); switch ($func[0]) { - case PEAR_EXCEPTION_PRINT: + case PEAR_EXCEPTION_PRINT : $f = (isset($func[1])) ? $func[1] : '%s'; printf($f, $this->getMessage()); break; - case PEAR_EXCEPTION_TRIGGER: + case PEAR_EXCEPTION_TRIGGER : $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE; trigger_error($this->getMessage(), $f); break; - case PEAR_EXCEPTION_DIE: + case PEAR_EXCEPTION_DIE : $f = (isset($func[1])) ? $func[1] : '%s'; die(printf($f, $this->getMessage())); break; + case PEAR_OBSERVER_CLASSCALLBACK : + $f = (isset($func[1])) ? $func[1] : false; + if (is_array($f) && isset($f['class']) && isset($f['callback'])) { + if ($this instanceof $f['class']) { + if (is_callable($f['callback'])) { + call_user_func($f['callback'], $this); + } + } + } + break; default: trigger_error('invalid observer type', E_USER_WARNING); } @@ -170,6 +186,25 @@ } /** + * Return specific error information that can be used for more detailed + * error messages or translation. + * + * This method may be overridden in child exception classes in order + * to add functionality not present in PEAR_Exception and is a placeholder + * to define API + * + * The returned array must be an associative array of parameter => value like so: + * <pre> + * array('name' => $name, 'context' => array(...)) + * </pre> + * @return array + */ + public function getErrorData() + { + return array(); + } + + /** * Returns the exception that caused this exception to be thrown * @access public * @return Exception|array The context of the exception @@ -308,7 +343,7 @@ . $cause['message'] . ' in ' . $cause['file'] . ' on line ' . $cause['line'] . "\n"; } - return $causeMsg.$this->getTraceAsString(); + return $causeMsg . $this->getTraceAsString(); } }

« previous php.pear.cvs (#23572) next »