From 26329535416f135d489c66cf8291538c247c4f74 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 4 Mar 2008 14:01:22 +0000 Subject: [PATCH] - removed NException, ability of catching PHP error moved to DibiDriverException --- dibi/Nette/IDebuggable.php | 28 ++++++ dibi/Nette/NException.php | 132 ------------------------- dibi/Nette/{NObject.php => Object.php} | 35 +++++-- dibi/dibi.php | 10 +- dibi/drivers/postgre.php | 18 ++-- dibi/drivers/sqlite.php | 6 +- dibi/libs/DibiConnection.php | 5 +- dibi/libs/DibiException.php | 53 +++++++++- 8 files changed, 125 insertions(+), 162 deletions(-) create mode 100644 dibi/Nette/IDebuggable.php delete mode 100644 dibi/Nette/NException.php rename dibi/Nette/{NObject.php => Object.php} (86%) diff --git a/dibi/Nette/IDebuggable.php b/dibi/Nette/IDebuggable.php new file mode 100644 index 00000000..8aa3a0c3 --- /dev/null +++ b/dibi/Nette/IDebuggable.php @@ -0,0 +1,28 @@ +cause === NULL) { - $this->cause = $cause; - } else { - throw new InvalidStateException('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 string - */ - public function __toString() - { - return parent::__toString() . ($this->cause === NULL ? '' : "\nCaused by " . $this->cause->__toString()); - } - - - - /** - * Enables converting all PHP errors to exceptions. - * - * @param Exception class to be thrown - * @return void - */ - public static function catchError($class = __CLASS__) - { - self::$oldHandler = set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL); - self::$handlerClass = $class; - } - - - - /** - * Disables converting errors to exceptions. - * - * @return void - */ - public static function restore() - { - if (self::$oldHandler !== NULL) { - set_error_handler(self::$oldHandler); - self::$oldHandler = NULL; - } else { - restore_error_handler(); - } - } - - - - /** - * Internal error handler. - */ - public static function _errorHandler($code, $message, $file, $line, $context) - { - self::restore(); - - if (ini_get('html_errors')) { - $message = strip_tags($message); - } - - throw new self::$handlerClass($message, $code); - } - -} diff --git a/dibi/Nette/NObject.php b/dibi/Nette/Object.php similarity index 86% rename from dibi/Nette/NObject.php rename to dibi/Nette/Object.php index 8005566e..58bec3dc 100644 --- a/dibi/Nette/NObject.php +++ b/dibi/Nette/Object.php @@ -1,19 +1,19 @@ $m because user could bypass property setter by: $x = & $obj->property; $x = 'new value'; $val = $this->$m(); return $val; @@ -232,7 +247,7 @@ abstract class NObject { static $cache; if (!isset($cache[$c])) { - // get_class_methods returns private, protected and public methods of NObject (doesn't matter) + // get_class_methods returns private, protected and public methods of Object (doesn't matter) // and ONLY PUBLIC methods of descendants (perfect!) // but returns static methods too (nothing doing...) // and is much faster than reflection diff --git a/dibi/dibi.php b/dibi/dibi.php index f179f2e4..faa4c36e 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -32,8 +32,8 @@ if (version_compare(PHP_VERSION, '5.1.0', '<')) { // nette libraries if (!class_exists('NotImplementedException', FALSE)) { require_once dirname(__FILE__) . '/Nette/exceptions.php'; } -if (!class_exists('NObject', FALSE)) { require_once dirname(__FILE__) . '/Nette/NObject.php'; } -if (!class_exists('NException', FALSE)) { require_once dirname(__FILE__) . '/Nette/NException.php'; } +if (!class_exists('NObject', FALSE)) { require_once dirname(__FILE__) . '/Nette/Object.php'; } +if (!interface_exists('IDebuggable', FALSE)) { require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; } // dibi libraries require_once dirname(__FILE__) . '/libs/interfaces.php'; @@ -164,7 +164,7 @@ class dibi */ public static function connect($config = array(), $name = 0) { - if (is_array($config)) { + if (is_array($config) || $config instanceof IMap) { $config['name'] = $name; } else { $config .= '&name=' . urlencode($name); @@ -404,9 +404,9 @@ class dibi /** - * Experimental; will be used in PHP 5.3. + * Replacement for majority of dibi::methods() in future. */ - public static function __callStatic($name, $args) + protected static function __callStatic($name, $args) { return call_user_func_array(array(self::getConnection(), $name), $args); } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 792de393..eec53b6f 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -88,22 +88,26 @@ class DibiPostgreDriver extends NObject implements IDibiDriver } } - DibiDriverException::catchError(); + DibiDriverException::tryError(); if (isset($config['persistent'])) { - $this->connection = @pg_connect($string, PGSQL_CONNECT_FORCE_NEW); + $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); } else { - $this->connection = @pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); + $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); + } + if (DibiDriverException::catchError($msg)) { + throw new DibiDriverException($msg, 0); } - DibiDriverException::restore(); if (!is_resource($this->connection)) { throw new DibiDriverException('Connecting error.'); } if (isset($config['charset'])) { - DibiDriverException::catchError(); - @pg_set_client_encoding($this->connection, $config['charset']); - DibiDriverException::restore(); + DibiDriverException::tryError(); + pg_set_client_encoding($this->connection, $config['charset']); + if (DibiDriverException::catchError($msg)) { + throw new DibiDriverException($msg, 0); + } } if (isset($config['schema'])) { diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 34944681..4286441c 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -127,13 +127,15 @@ class DibiSqliteDriver extends NObject implements IDibiDriver */ public function query($sql) { - DibiDriverException::catchError(); + DibiDriverException::tryError(); if ($this->buffered) { $this->resultset = sqlite_query($this->connection, $sql); } else { $this->resultset = sqlite_unbuffered_query($this->connection, $sql); } - DibiDriverException::restore(); + if (DibiDriverException::catchError($msg)) { + throw new DibiDriverException($msg, sqlite_last_error($this->connection), $sql); + } return is_resource($this->resultset); } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 5b07be54..c1033e52 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -58,7 +58,7 @@ class DibiConnection extends NObject /** * Creates object and (optionally) connects to a database. * - * @param array|string connection parameters + * @param array|string|IMap connection parameters * @throws DibiException */ public function __construct($config) @@ -66,6 +66,9 @@ class DibiConnection extends NObject // DSN string if (is_string($config)) { parse_str($config, $config); + + } elseif ($config instanceof IMap) { + $config = $config->toArray(); } if (!isset($config['driver'])) { diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index b3263e22..313f605f 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -27,7 +27,7 @@ * @package dibi * @version $Revision$ $Date$ */ -class DibiException extends NException +class DibiException extends Exception { } @@ -42,8 +42,11 @@ class DibiException extends NException * @package dibi * @version $Revision$ $Date$ */ -class DibiDriverException extends DibiException +class DibiDriverException extends DibiException implements IDebuggable { + /** @var string */ + private static $errorMsg; + /** @var string */ private $sql; @@ -84,12 +87,52 @@ class DibiDriverException extends DibiException + /********************* error catching ****************d*g**/ + + + /** - * @see NException::catchError (this is Late static binding fix + * Starts catching potential errors/warnings + * + * @return void */ - public static function catchError($class = __CLASS__) + public static function tryError() { - parent::catchError($class); + set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL); + self::$errorMsg = NULL; + } + + + + /** + * Returns catched error/warning message. + * + * @param string catched message + * @return bool + */ + public static function catchError(& $message) + { + restore_error_handler(); + $message = self::$errorMsg; + self::$errorMsg = NULL; + return $message !== NULL; + } + + + + /** + * Internal error handler. Do not call directly. + */ + public static function _errorHandler($code, $message) + { + restore_error_handler(); + + if (ini_get('html_errors')) { + $message = strip_tags($message); + $message = html_entity_decode($message); + } + + self::$errorMsg = $message; } } \ No newline at end of file