From 58ed8d34f46cc7eee69ba8a98a898115cc4cbab9 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 18 Nov 2007 02:29:11 +0000 Subject: [PATCH] DibiException is NException descendant --- dibi/drivers/pdo.php | 23 ++++--------------- dibi/libs/DibiException.php | 2 +- dibi/libs/NException.php | 46 +++++++++++++++++++++++++++++++++++++ dibi/libs/NObject.php | 7 +++++- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index f8cfa702..095f1239 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -71,7 +71,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface try { $this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); } catch (PDOException $e) { - throw $this->convertException($e); + throw new DibiDriverException($e->getMessage(), $e->getCode()); } if (!$this->connection) { @@ -106,7 +106,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface try { $this->resultset = $this->connection->query($sql); } catch (PDOException $e) { - throw $this->convertException($e); + throw new DibiDriverException($e->getMessage(), $e->getCode(), $sql); } return $this->resultset instanceof PDOStatement; } @@ -146,7 +146,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface try { $this->connection->beginTransaction(); } catch (PDOException $e) { - throw $this->convertException($e); + throw new DibiDriverException($e->getMessage(), $e->getCode()); } } @@ -161,7 +161,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface try { $this->connection->commit(); } catch (PDOException $e) { - throw $this->convertException($e); + throw new DibiDriverException($e->getMessage(), $e->getCode()); } } @@ -176,7 +176,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface try { $this->connection->rollBack(); } catch (PDOException $e) { - throw $this->convertException($e); + throw new DibiDriverException($e->getMessage(), $e->getCode()); } } @@ -316,17 +316,4 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface function getDibiReflection() {} - - - /** - * Disconnects from a database - * - * @param PDOException - * @return DibiDriverException - */ - private function convertException($e) - { - return new DibiDriverException($e->getMessage(), $e->getCode()); - } - } diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index a224c8b0..254cfa46 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -27,7 +27,7 @@ * @package dibi * @version $Revision$ $Date$ */ -class DibiException extends Exception +class DibiException extends NException { } diff --git a/dibi/libs/NException.php b/dibi/libs/NException.php index 5b89e8ec..c87bab25 100644 --- a/dibi/libs/NException.php +++ b/dibi/libs/NException.php @@ -30,6 +30,9 @@ */ class NException extends Exception { + /** @var Exception */ + private $cause; + /** @var callback */ private static $oldHandler; @@ -37,6 +40,49 @@ class NException extends Exception private static $handlerClass; + + + /** + * Initializes the cause of this throwable to the specified value + * + * @param Exception + * @return void + */ + public function initCause(Exception $cause) + { + if ($this->cause === NULL) { + $this->cause = $cause; + } else { + throw new BadMethodCallException('Cause was already assigned'); + } + } + + + + /** + * Gets the Exception instance that caused the current exception + * + * @return Exception + */ + public function getCause() + { + return $this->cause; + } + + + + /** + * Returns string represenation of exception + * + * @return void + */ + public function __toString() + { + return parent::__toString() . ($this->cause === NULL ? '' : "\nCaused by " . $this->cause->__toString()); + } + + + /** * Enables converting all PHP errors to exceptions * diff --git a/dibi/libs/NObject.php b/dibi/libs/NObject.php index 5e6b0caf..8c74caff 100644 --- a/dibi/libs/NObject.php +++ b/dibi/libs/NObject.php @@ -198,7 +198,7 @@ abstract class NObject /** - * Does method exist? Case sensitive, filters protected & private, doesn't recognize static methods (works good since 5.0.4) + * Does public method exist? (case sensitive) * * @param string class name * @param string method name @@ -208,6 +208,11 @@ abstract class NObject { static $cache; if (!isset($cache[$c])) { + // get_class_methods returns private, protected and public methods of NObject (doesn't matter) + // only only public methods of descendants (perfect!) + // but returns static methods too (nothing doing...) + // and is much faster than reflection + // (works good since 5.0.4) $cache[$c] = array_flip(get_class_methods($c)); } return isset($cache[$c][$m]);