Use Exceptions
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 28 Sep 2009 20:08:07 +0000 (16:08 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 28 Sep 2009 20:24:10 +0000 (16:24 -0400)
framework/Lock/lib/Horde/Lock.php
framework/Lock/lib/Horde/Lock/Exception.php [new file with mode: 0644]
framework/Lock/lib/Horde/Lock/Sql.php
framework/Lock/package.xml

index 1387de0..c54f45e 100644 (file)
@@ -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 (file)
index 0000000..5d8c12c
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+class Horde_Lock_Exception extends Exception
+{
+    /**
+     */
+    public function __construct($msg, $code = 0)
+    {
+        parent::__construct($msg, $code);
+    }
+
+}
index 046fb4f..70c7745 100644 (file)
@@ -108,16 +108,13 @@ class Horde_Lock_Sql extends Horde_Lock
     /**
      * Return an array of information about the requested lock.
      *
-     * @param string $lockid   Lock ID to look up
-     *
-     * @return mixed           Array of lock information on success; false if no
-     *                         valid lock exists; PEAR_Error on failure.
+     * @see Horde_Lock::getLockInfo
      */
     public function getLockInfo($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."));
         }
 
         $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.
index 6933712..cbda0b2 100644 (file)
@@ -33,6 +33,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
     <dir name="Horde">
      <dir name="Lock">
       <file name="Sql.php" role="php" />
+      <file name="Exception.php" role="php" />
      </dir> <!-- /lib/Horde/Lock -->
      <file name="Lock.php" role="php" />
     </dir> <!-- /lib/Horde -->
@@ -73,6 +74,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
  <phprelease>
   <filelist>
    <install name="lib/Horde/Lock/Sql.php" as="Horde/Lock/Sql.php" />
+   <install name="lib/Horde/Lock/Exception.php" as="Horde/Lock/Exception.php" />
    <install name="lib/Horde/Lock.php" as="Horde/Lock.php" />
   </filelist>
  </phprelease>