From d1d97a219da2535a4c932b0757573c3b2b355da8 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 28 Sep 2009 16:08:07 -0400 Subject: [PATCH] Use Exceptions --- framework/Lock/lib/Horde/Lock.php | 42 ++++++++------ framework/Lock/lib/Horde/Lock/Exception.php | 11 ++++ framework/Lock/lib/Horde/Lock/Sql.php | 89 +++++++---------------------- framework/Lock/package.xml | 2 + 4 files changed, 57 insertions(+), 87 deletions(-) create mode 100644 framework/Lock/lib/Horde/Lock/Exception.php diff --git a/framework/Lock/lib/Horde/Lock.php b/framework/Lock/lib/Horde/Lock.php index 1387de057..c54f45e8e 100644 --- a/framework/Lock/lib/Horde/Lock.php +++ b/framework/Lock/lib/Horde/Lock.php @@ -30,7 +30,7 @@ class Horde_Lock * * @param array $params Parameters for the specific Horde_Lock driver * - * @return Horde_Lock Instance of Horde_Lock on success or PEAR_Error + * @return Horde_Lock Instance of Horde_Lock */ public function __construct($params) { @@ -43,12 +43,12 @@ class Horde_Lock * * @param string $lockid Lock ID to look up * - * @return mixed Array of lock information on success; PEAR_Error - * on failure. + * @return mixed Array of lock information + * @throws Horde_Log_Exception */ public function getLockInfo($lockid) { - return PEAR::raiseError(_("No lock driver configured!")); + throw new Horde_Log_Exception(_("No lock driver configured!")); } /** @@ -62,14 +62,15 @@ class Horde_Lock * @param int $type Only return locks of the given type. * Defaults to null, or all locks * - * @return mixed Array of locks with the ID as the key and the lock details + * @return array Array of locks with the ID as the key and the lock details * as the value. If there are no current locks this will - * return an empty array. On failure a PEAR_Error object - * will be returned. + * return an empty array. + * + * @throws Horde_Log_Exception */ public function getLocks($scope = null, $principal = null, $type = null) { - return PEAR::raiseError(_("No lock driver configured!")); + throw new Horde_Log_Exception(_("No lock driver configured!")); } /** @@ -79,11 +80,12 @@ class Horde_Lock * lock. * @param int $extend Extend lock this many seconds from now. * - * @return mixed True on success; PEAR_Error on failure. + * @return boolean + * @throws Horde_Log_Exception */ public function resetLock($lockid, $extend) { - return PEAR::raiseError(_("No lock driver configured!")); + throw new Horde_Log_Exception(_("No lock driver configured!")); } /** @@ -117,12 +119,13 @@ class Horde_Lock * will be considered valid until they are * cleared or expire. * - * @return mixed A string lock ID on success; PEAR_Error on failure. + * @return mixed A string lock ID. + * @throws Horde_Log_Exception */ public function setLock($requestor, $scope, $principal, $lifetime = 1, $exclusive = HORDE_LOCK_TYPE_SHARED) { - return PEAR::raiseError(_("No lock driver configured!")); + throw new Horde_Log_Exception(_("No lock driver configured!")); } /** @@ -134,11 +137,12 @@ class Horde_Lock * @param string $lockid The lock ID as generated by a previous call * to setLock() * - * @return mixed True on success; PEAR_Error on failure. + * @return boolean + * @throws Horde_Log_Exception */ public function clearLock($lockid) { - return PEAR::raiseError(_("No lock driver configured!")); + throw new Horde_Log_Exception(_("No lock driver configured!")); } /** @@ -153,8 +157,8 @@ class Horde_Lock * @param array $params A hash containing any additional configuration or * connection parameters a subclass might need. * - * @return Horde_Lock The newly created concrete Lock instance, or a - * PEAR_Error object on error. + * @return Horde_Lock The newly created concrete Lock instance. + * @throws Horde_Log_Exception */ public function factory($driver, $params = null) { @@ -190,7 +194,7 @@ class Horde_Lock if (class_exists($class)) { $lock = new $class($params); } else { - $lock = PEAR::raiseError('Horde_Lock Driver (' . $class . ') not found' . ($include_error ? ': ' . $include_error : '') . '.'); + throw new Horde_Log_Exception('Horde_Lock Driver (' . $class . ') not found' . ($include_error ? ': ' . $include_error : '') . '.'); } return $lock; @@ -213,8 +217,8 @@ class Horde_Lock * @param array $params A hash containing any additional configuration or * connection parameters a subclass might need. * - * @return Horde_Lock The concrete Horde_Lock reference or PEAR_Error - * on failure. + * @return Horde_Lock The concrete Horde_Lock reference + * @throws Horde_Log_Exception */ public function &singleton($driver, $params = null) { diff --git a/framework/Lock/lib/Horde/Lock/Exception.php b/framework/Lock/lib/Horde/Lock/Exception.php new file mode 100644 index 000000000..5d8c12c2e --- /dev/null +++ b/framework/Lock/lib/Horde/Lock/Exception.php @@ -0,0 +1,11 @@ +_connect()), 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return PEAR::raiseError(_("Internal database error. Details have been logged for the administrator.")); + throw new Horde_Lock_Exception(_("Internal database error. Details have been logged for the administrator.")); } $now = time(); @@ -131,7 +128,7 @@ class Horde_Lock_Sql extends Horde_Lock __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->query($sql, $values); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } $locks = array(); @@ -148,23 +145,13 @@ class Horde_Lock_Sql extends Horde_Lock * Return a list of valid locks with the option to limit the results * by principal, scope and/or type. * - * @param string $scope The scope of the lock. Typically the name of - * the application requesting the lock or some - * other identifier used to group locks together. - * @param string $principal Principal for which to check for locks - * @param int $type Only return locks of the given type. - * Defaults to null, or all locks - * - * @return mixed Array of locks with the ID as the key and the lock details - * as the value. If there are no current locks this will - * return an empty array. On failure a PEAR_Error object - * will be returned. + * @see Horde_Lock::getLocks */ public function getLocks($scope = null, $principal = null, $type = null) { if (is_a(($result = $this->_connect()), 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return PEAR::raiseError(_("Internal database error. Details have been logged for the administrator.")); + throw new Horde_Lock_Exception(_("Internal database error. Details have been logged for the administrator.")); } $now = time(); @@ -192,7 +179,7 @@ class Horde_Lock_Sql extends Horde_Lock __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->query($sql, $values); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } $locks = array(); @@ -209,18 +196,13 @@ class Horde_Lock_Sql extends Horde_Lock /** * Extend the valid lifetime of a valid lock to now + $newtimeout. * - * @param string $lockid Lock ID to reset. Must be a valid, non-expired - * lock. - * @param int $extend Extend lock this many seconds from now. - * - * @return mixed True on success; false on expired lock; - * PEAR_Error on failure. + * @see Horde_Lock::resetLock */ public function resetLock($lockid, $extend) { if (is_a(($result = $this->_connect()), 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return PEAR::raiseError(_("Internal database error. Details have been logged for the administrator.")); + throw new Horde_Lock_Exception(_("Internal database error. Details have been logged for the administrator.")); } $now = time(); @@ -237,13 +219,12 @@ class Horde_Lock_Sql extends Horde_Lock $sql, __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_write_db->query($sql, $values); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } - Horde::logMessage(sprintf('Lock %s reset successfully.', $lockid), __FILE__, __LINE__, PEAR_LOG_INFO); return true; } elseif (is_a($lockinfo, 'PEAR_Error')) { - return $lockinfo; + throw new Horde_Lock_Exception($lockinfo->getMessage()); } else { // $lockinfo is false indicating the lock is no longer valid. return false; @@ -256,44 +237,19 @@ class Horde_Lock_Sql extends Horde_Lock * that the calling application has done all necessary security checks * before requesting a lock be granted. * - * @param string $requestor User ID of the lock requestor. - * @param string $scope The scope of the lock. Typically the name of - * the application requesting the lock or some - * other identifier used to group locks together. - * @param string $principal A principal on which a lock should be granted. - * The format can be any string but is suggested - * to be in URI form. - * @param int $lifetime Time (in seconds) for which the lock will be - * considered valid. - * @param string type One of HORDE_LOCK_TYPE_SHARED or - * HORDE_LOCK_TYPE_EXCLUSIVE. - * - An exclusive lock will be enforced strictly - * and must be interpreted to mean that the - * resource can not be modified. Only one - * exclusive lock per principal is allowed. - * - A shared lock is one that notifies other - * potential lock requestors that the resource - * is in use. This lock can be overridden - * (cleared or replaced with a subsequent - * call to setLock()) by other users. Multiple - * users may request (and will be granted) a - * shared lock on a given principal. All locks - * will be considered valid until they are - * cleared or expire. - * - * @return mixed A string lock ID on success; PEAR_Error on failure. + * @see Horde_Lock::setLock */ public function setLock($requestor, $scope, $principal, $lifetime = 1, $type = HORDE_LOCK_TYPE_SHARED) { if (is_a(($result = $this->_connect()), 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return PEAR::raiseError(_("Internal database error. Details have been logged for the administrator.")); + throw new Horde_Lock_Exception(_("Internal database error. Details have been logged for the administrator.")); } $oldlocks = $this->getLocks($scope, $principal, HORDE_LOCK_TYPE_EXCLUSIVE); if (is_a($oldlocks, 'PEAR_Error')) { - return $oldlocks; + throw new Horde_Lock_Exception($oldlocks->getMessage()); } if (count($oldlocks) != 0) { @@ -314,7 +270,7 @@ class Horde_Lock_Sql extends Horde_Lock __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_write_db->query($sql, $values); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } Horde::logMessage(sprintf('Lock %s set successfully by %s in scope %s on "%s"', $lockid, $requestor, $scope, $principal), __FILE__, __LINE__, PEAR_LOG_INFO); @@ -327,20 +283,17 @@ class Horde_Lock_Sql extends Horde_Lock * that the calling application has done all necessary security checks * before requesting a lock be cleared. * - * @param string $lockid The lock ID as generated by a previous call - * to setLock() - * - * @return mixed True on success; PEAR_Error on failure. + * @see Horde_Lock::clearLock */ public function clearLock($lockid) { if (is_a(($result = $this->_connect()), 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return PEAR::raiseError(_("Internal database error. Details have been logged for the administrator.")); + throw new Horde_Lock_Exception(_("Internal database error. Details have been logged for the administrator.")); } if (empty($lockid)) { - return PEAR::raiseError(_("Must supply a valid lock ID.")); + throw new Horde_Lock_Exception(_("Must supply a valid lock ID.")); } // Since we're trying to clear the lock we don't care @@ -353,7 +306,7 @@ class Horde_Lock_Sql extends Horde_Lock __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_write_db->query($sql, $values); if (is_a($result, 'PEAR_Error')) { - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } Horde::logMessage(sprintf('Lock %s cleared successfully.', $lockid), __FILE__, __LINE__, PEAR_LOG_INFO); @@ -375,7 +328,7 @@ class Horde_Lock_Sql extends Horde_Lock 'Lock SQL', array('driver' => 'lock')); if (is_a($result, 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; + throw new Horde_Lock_Exception($result->getMessage()); } require_once 'DB.php'; @@ -386,7 +339,7 @@ class Horde_Lock_Sql extends Horde_Lock ); if (is_a($this->_write_db, 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $this->_write_db; + throw new Horde_Lock_Exception($this->_write_db->getMessage()); } // Set DB portability options. @@ -407,7 +360,7 @@ class Horde_Lock_Sql extends Horde_Lock ); if (is_a($this->_db, 'PEAR_Error')) { Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $this->_db; + throw new Horde_Lock_Exception($this->_db->getMessage()); } // Set DB portability options. diff --git a/framework/Lock/package.xml b/framework/Lock/package.xml index 6933712e6..cbda0b2a3 100644 --- a/framework/Lock/package.xml +++ b/framework/Lock/package.xml @@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -73,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0