Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add PEAR2_Exception

  • Loading branch information...
commit c530176fb6c4126a0efd169535eccc6f69005ead 1 parent a812bfb
@saltybeagle saltybeagle authored
View
2  vendor/.configsnapshots/configsnapshot-2011-09-09 10-24-33.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<pearconfig version="1.0"><php_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/php</php_dir><ext_dir>/usr/local/lib/php/extensions/no-debug-non-zts-20090626/</ext_dir><cfg_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/cfg</cfg_dir><doc_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/docs</doc_dir><bin_dir>/usr/local/bin</bin_dir><data_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/data</data_dir><www_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/www</www_dir><test_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/tests</test_dir><src_dir>/Users/bbieber/Documents/workspace/Pyrus_Developer/vendor/src</src_dir><php_bin>/usr/local/bin/php</php_bin><php_ini>/usr/local/lib/php.ini</php_ini><php_prefix></php_prefix><php_suffix></php_suffix></pearconfig>
View
BIN  vendor/.pear2registry
Binary file not shown
View
110 vendor/.xmlregistry/packages/pear2.php.net/PEAR2_Exception/0.2.0-info.xml
@@ -0,0 +1,110 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd" packagerversion="2.0.0">
+ <name>PEAR2_Exception</name>
+ <channel>pear2.php.net</channel>
+ <summary>PEAR2\Exception is the standard exception class for PEAR2.
+</summary>
+ <description>In addition to supporting the default signature of the built-in
+Exception class, PEAR2\Exception also supports passing a
+cause of an exception, which can be another exception object
+or a PEAR2_MultiErrors object. The error messages of the causes
+are used when constructing the error message displayed when
+converting PEAR2\Exception to a string.
+
+PEAR2\Exception should always be extended by your own custom exception
+class for the package.</description>
+ <lead>
+ <name>Gregory Beaver</name>
+ <user>cellog</user>
+ <email>cellog@php.net</email>
+ <active>yes</active>
+ </lead>
+ <lead>
+ <name>Tomas V.V. Cox</name>
+ <user>cox</user>
+ <email>cox@idecnet.com</email>
+ <active>no</active>
+ </lead>
+ <lead>
+ <name>Bertrand Mansion</name>
+ <user>mansion</user>
+ <email>bmansion@mamasam.com</email>
+ <active>no</active>
+ </lead>
+ <developer>
+ <name>Hans Lellelid</name>
+ <user>hlellelid</user>
+ <email>hans@velum.net</email>
+ <active>no</active>
+ </developer>
+ <date>2011-09-09</date>
+ <time>10:24:33</time>
+ <version>
+ <release>0.2.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
+ <notes>- Covert namespace to PEAR2
+- Removed getCause() as it just uses the built in getPrevious() that was added in PHP 5.3
+- Removed getUniqueId as it's used anywhere
+- Removed toHtml, toText and __toString (which only handled calling the former two).
+ People should implement their owny decorator calls.
+- Removed the built in outputs of the observer functions. Those where the class constants
+ OBSERVER_PRINT, OBSERVER_TRIGGER, OBSERVER_DIE
+ This can be achieved by writing a custom callback function, using the appropriate printf(),
+ trigger_error and die() to achieve the same result.
+- Removed getErroClass and getErrorMethod as it can be easily gotten from getTrace/getTraceSafe first array element
+- Add the proper LICENSE file and move Exception.php to the NEW BSD license as the package.xml already defines
+- Removed __toString, toText and toHtml. For those wanting to get pretty outputs they can use this approach:
+
+ $causes = array();
+ $this-&gt;getCauseMessage($causes);
+ foreach ($causes as $i =&gt; $cause) {
+ var_dump($cause);
+ }
+
+ And combine that with getTraceSafe() or getTraceAsString() if needed.
+
+ Put all that in a function and use set_exception_handler('exceptionHandler'); or similar to process
+ all Exceptions.
+
+ Example code or see the removed code at http://svn.php.net/viewvc/pear2/Exception/trunk/src/Exception.php?r1=295348&amp;r2=295352 :
+
+ function exceptionHandler($e)
+ {
+ if ($e instanceof \PEAR2\Exception) {
+ $causes = array();
+ $e-&gt;getCauseMessage($causes);
+ $causeMsg = '';
+ foreach ($causes as $i =&gt; $cause) {
+ $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
+ . $cause['message'] . &quot;\n&quot;;
+ }
+ echo $causeMsg;
+ } else {
+ echo $e-&gt;getMessage(), &quot;\n&quot;;
+ }
+ }
+
+ set_exception_handler('exceptionHandler');</notes>
+ <contents>
+ <dir name="/">
+ <file role="php" name="php/PEAR2/Exception.php" md5sum="a8fa05c1c637bbc2c3fd29403799be3f"/>
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>2.0.0a1</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease/>
+</package>
View
196 vendor/php/PEAR2/Exception.php
@@ -0,0 +1,196 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
+/**
+ * PEAR2_Exception
+ *
+ * PHP versions 4 and 5
+ *
+ * @category pear
+ * @package PEAR2_Exception
+ * @author Tomas V. V. Cox <cox@idecnet.com>
+ * @author Hans Lellelid <hans@velum.net>
+ * @author Bertrand Mansion <bmansion@mamasam.com>
+ * @author Greg Beaver <cellog@php.net>
+ * @copyright 1997-2007 The PHP Group
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://pear2.php.net/PEAR2_Exception
+ * @since File available since Release 0.1.0
+ */
+
+
+/**
+ * Base PEAR2_Exception Class
+ *
+ * 1) Features:
+ *
+ * - Nestable exceptions (throw new PEAR2_Exception($msg, $prev_exception))
+ * - Definable triggers, shot when exceptions occur
+ * - Added more context info available (like class, method or cause)
+ * - cause can be a PEAR2_Exception or an array of mixed
+ * PEAR2_Exceptions or a \PEAR2\MultiErrors
+ * - callbacks for specific exception classes and their children
+ *
+ * 2) Usage example
+ *
+ * <code>
+ * namespace PEAR2;
+ * class PEAR2_MyPackage_Exception extends Exception {}
+ *
+ * class Test
+ * {
+ * function foo()
+ * {
+ * throw new PEAR2_MyPackage_Exception('Error Message', 4);
+ * }
+ * }
+ *
+ * function myLogger($exception)
+ * {
+ * echo 'Logger: ' . $exception->getMessage() . "\n";
+ * }
+ *
+ * // each time a exception is thrown the 'myLogger' will be called
+ * // (its use is completely optional)
+ * Exception::addObserver('\PEAR2\myLogger');
+ * $test = new Test;
+ * try {
+ * $test->foo();
+ * } catch (\Exception $e) {
+ * print $e;
+ * }
+ * </code>
+ *
+ * @category pear
+ * @package PEAR
+ * @author Tomas V.V.Cox <cox@idecnet.com>
+ * @author Hans Lellelid <hans@velum.net>
+ * @author Bertrand Mansion <bmansion@mamasam.com>
+ * @author Greg Beaver <cellog@php.net>
+ * @copyright 1997-2007 The PHP Group
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License
+ * @version Release: @package_version@
+ * @link http://pear2.php.net/PEAR2_Exception
+ * @since Class available since Release 0.1.0
+ *
+ */
+namespace PEAR2;
+abstract class Exception extends \Exception
+{
+ private static $_observers = array();
+ private $_trace;
+
+ /**
+ * Supported signatures:
+ * - PEAR2_Exception(string $message);
+ * - PEAR2_Exception(string $message, int $code);
+ * - PEAR2_Exception(string $message, Exception $cause);
+ * - PEAR2_Exception(string $message, Exception $cause, int $code);
+ * - PEAR2_Exception(string $message, PEAR2\MultiErrors $cause);
+ * - PEAR2_Exception(string $message, PEAR2\MultiErrors $cause, int $code);
+ * @param string exception message
+ * @param int|Exception|PEAR2\MultiErrors|null exception cause
+ * @param int|null exception code or null
+ */
+ public function __construct($message, $p2 = null, $p3 = null)
+ {
+ $code = $cause = null;
+ if (is_int($p2)) {
+ $code = $p2;
+ } elseif (is_object($p2)) {
+ if (!($p2 instanceof \Exception)) {
+ throw new \Exception('exception cause must be Exception, or PEAR2\MultiErrors');
+ }
+
+ $code = $p3;
+ $cause = $p2;
+ }
+
+ if (!is_string($message)) {
+ throw new \Exception('exception message must be a string, was ' . gettype($message));
+ }
+
+ parent::__construct($message, $code, $cause);
+
+ foreach (self::$_observers as $func) {
+ if (is_callable($func)) {
+ call_user_func($func, $this);
+ }
+ }
+ }
+
+ /**
+ * @param mixed $callback - A valid php callback, see php func is_callable()
+ * - A PEAR2_Exception::OBSERVER_* constant
+ * - An array(const PEAR2_Exception::OBSERVER_*,
+ * mixed $options)
+ * @param string $label The name of the observer. Use this if you want
+ * to remove it later with removeObserver()
+ */
+ public static function addObserver($callback, $label = 'default')
+ {
+ self::$_observers[$label] = $callback;
+ }
+
+ public static function removeObserver($label = 'default')
+ {
+ unset(self::$_observers[$label]);
+ }
+
+ /**
+ * Function must be public to call on caused exceptions
+ * @param array
+ */
+ public function getCauseMessage(array &$causes)
+ {
+ $trace = $this->getTraceSafe();
+ $cause = array(
+ 'class' => get_class($this),
+ 'message' => $this->message,
+ 'file' => 'unknown',
+ 'line' => 'unknown'
+ );
+
+ if (isset($trace[0]) && isset($trace[0]['file'])) {
+ $cause['file'] = $trace[0]['file'];
+ $cause['line'] = $trace[0]['line'];
+ }
+
+ $causes[] = $cause;
+ if ($this->getPrevious() instanceof self) {
+ $this->getPrevious()->getCauseMessage($causes);
+ } elseif ($this->getPrevious() instanceof \PEAR2\MultiErrors) {
+ foreach ($this->getPrevious() as $cause) {
+ if ($cause instanceof self) {
+ $cause->getCauseMessage($causes);
+ } elseif ($cause instanceof \Exception) {
+ $causes[] = array(
+ 'class' => get_class($cause),
+ 'message' => $cause->getMessage(),
+ 'file' => $cause->getFile(),
+ 'line' => $cause->getLine()
+ );
+ }
+ }
+ } elseif ($this->getPrevious() instanceof \Exception) {
+ $causes[] = array(
+ 'class' => get_class($this->getPrevious()),
+ 'message' => $this->getPrevious()->getMessage(),
+ 'file' => $this->getPrevious()->getFile(),
+ 'line' => $this->getPrevious()->getLine()
+ );
+ }
+ }
+
+ public function getTraceSafe()
+ {
+ if (!isset($this->_trace)) {
+ $this->_trace = $this->getTrace();
+ if (empty($this->_trace)) {
+ $backtrace = debug_backtrace();
+ $this->_trace = array($backtrace[count($backtrace)-1]);
+ }
+ }
+
+ return $this->_trace;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.