pushApp/popApp cleanups
authorMichael M Slusarz <slusarz@curecanti.org>
Wed, 3 Feb 2010 01:15:52 +0000 (18:15 -0700)
committerMichael M Slusarz <slusarz@curecanti.org>
Wed, 3 Feb 2010 01:56:05 +0000 (18:56 -0700)
framework/Block/lib/Horde/Block/Collection.php
horde/lib/Api.php
horde/services/images/view.php

index 4b1f1f9..2510c1f 100644 (file)
@@ -86,37 +86,38 @@ class Horde_Block_Collection
                 continue;
             }
 
-            try {
-                $pushed = $GLOBALS['registry']->pushApp($app);
-            } catch (Horde_Exception $e) {
-                continue;
-            }
-
             $blockdir = $GLOBALS['registry']->get('fileroot', $app) . '/lib/Block';
-            $dh = @opendir($blockdir);
-            if (is_resource($dh)) {
-                while (($file = readdir($dh)) !== false) {
-                    if (substr($file, -4) == '.php') {
-                        $block_name = null;
-                        $block_type = null;
-                        if (is_readable($blockdir . '/' . $file)) {
-                            include_once $blockdir . '/' . $file;
-                        }
-                        if (!is_null($block_type) && !is_null($this->_type) &&
-                            $block_type != $this->_type) {
-                            continue;
-                        }
-                        if (!empty($block_name)) {
-                            $this->_blocks[$app][substr($file, 0, -4)]['name'] = $block_name;
-                        }
+            if (file_exists($blockdir)) {
+                try {
+                    $pushed = $GLOBALS['registry']->pushApp($app);
+                } catch (Horde_Exception $e) {
+                    continue;
+                }
+
+                $d = dir($blockdir);
+                while (($file = $d->read()) !== false) {
+                    if (substr($file, -4) != '.php') {
+                        continue;
+                    }
+
+                    $block_name = $block_type = null;
+
+                    if (is_readable($blockdir . '/' . $file)) {
+                        include_once $blockdir . '/' . $file;
+                    }
+
+                    if (!empty($block_name) &&
+                        (is_null($block_type) ||
+                         is_null($this->_type) ||
+                         ($block_type == $this->_type))) {
+                        $this->_blocks[$app][substr($file, 0, -4)]['name'] = $block_name;
                     }
                 }
-                closedir($dh);
-            }
+                $d->close();
 
-            // Don't pop an application if we didn't have to push one.
-            if ($pushed) {
-                $GLOBALS['registry']->popApp($app);
+                if ($pushed) {
+                    $GLOBALS['registry']->popApp($app);
+                }
             }
         }
 
@@ -134,28 +135,35 @@ class Horde_Block_Collection
 
     /**
      * TODO
+     *
+     * @throws Horde_Exception
      */
     public function getBlock($app, $name, $params = null, $row = null,
                              $col = null)
     {
-        if ($GLOBALS['registry']->get('status', $app) == 'inactive' ||
-            ($GLOBALS['registry']->get('status', $app) == 'admin' &&
+        if (($GLOBALS['registry']->get('status', $app) == 'inactive') ||
+            (($GLOBALS['registry']->get('status', $app) == 'admin') &&
              !Horde_Auth::isAdmin())) {
-            $error = PEAR::raiseError(sprintf(_("%s is not activated."), $GLOBALS['registry']->get('name', $app)));
-            return $error;
+            throw new Horde_Exception(sprintf('%s is not activated.', $GLOBALS['registry']->get('name', $app)));
         }
 
         $path = $GLOBALS['registry']->get('fileroot', $app) . '/lib/Block/' . $name . '.php';
         if (is_readable($path)) {
             include_once $path;
         }
+
         $class = 'Horde_Block_' . $app . '_' . $name;
         if (!class_exists($class)) {
-            $error = PEAR::raiseError(sprintf(_("%s not found."), $class));
-            return $error;
+            throw new Horde_Exception(sprintf('%s not found.', $class));
+        }
+
+        $pushed = $GLOBALS['registry']->pushApp($app);
+        $ob = new $class($params, $row, $col);
+        if ($pushed) {
+            $GLOBALS['registry']->popApp($app);
         }
 
-        return new $class($params, $row, $col);
+        return $ob;
     }
 
     /**
index 55b65d5..501d83a 100644 (file)
@@ -95,27 +95,6 @@ class Horde_Api extends Horde_Registry_Api
     /* Blocks. */
 
     /**
-     * Helper method to return an instance of the Horde_Block class. This
-     * should not be exposed directly in the API; it is used by
-     * blockTitle() and BlockContent().
-     *
-     * @param string $app    Block application.
-     * @param string $name   Block name.
-     * @param array $params  Block parameters.
-     *
-     * @return Horde_Block  The Horde_Block instance.
-     * @throws Horde_Exception
-     */
-    protected function _block($app, $name, $params = array())
-    {
-        $GLOBALS['registry']->pushApp($app);
-        $result = Horde_Block_Collection::getBlock($app, $name, $params);
-        $GLOBALS['registry']->popApp($app);
-
-        return $result;
-    }
-
-    /**
      * Returns a Horde_Block's title.
      *
      * @param string $app    Block application.
@@ -126,11 +105,12 @@ class Horde_Api extends Horde_Registry_Api
      */
     public function blockTitle($app, $name, $params = array())
     {
-        $block = $this->_block($app, $name, $params);
-        if (is_a($block, 'PEAR_Error')) {
-            return $block->getMessage();
+        try {
+            $block = Horde_Block_Collection::getBlock($app, $name, $params);
+            return $block->getTitle();
+        } catch (Horde_Exception $e) {
+            return $e->getMessage();
         }
-        return $block->getTitle();
     }
 
     /**
@@ -144,11 +124,12 @@ class Horde_Api extends Horde_Registry_Api
      */
     public function blockContent($app, $name, $params = array())
     {
-        $block = $this->_block($app, $name, $params);
-        if (is_a($block, 'PEAR_Error')) {
-            return $block->getMessage();
+        try {
+            $block = Horde_Block_Collection::getBlock($app, $name, $params);
+            return $block->getContent();
+        } catch (Horde_Exception $e) {
+            return $e->getMessage();
         }
-        return $block->getContent();
     }
 
     /**
@@ -159,12 +140,7 @@ class Horde_Api extends Horde_Registry_Api
      */
     public function blocks()
     {
-        $collection = Horde_Block_Collection::singleton();
-        if (is_a($collection, 'PEAR_Error')) {
-            return $collection;
-        } else {
-            return $collection->getBlocksList();
-        }
+        return Horde_Block_Collection::singleton()->getBlocksList();
     }
 
     /* User data. */
index 3b2a4b7..a8b621b 100644 (file)
@@ -29,7 +29,7 @@ $action = strtolower(Horde_Util::getFormData('a'));
 switch ($source) {
 case 'vfs':
     /* Change app if needed to get the right VFS config. */
-    $changed_conf = $registry->pushApp($app_conf);
+    $pushed = $registry->pushApp($app_conf);
 
     /* Getting a file from Horde's VFS. */
     $vfs = VFS::singleton($conf['vfs']['type'], Horde::getDriverConfig('vfs', $conf['vfs']['type']));
@@ -41,7 +41,7 @@ case 'vfs':
     }
 
     /* Return the original app if changed previously. */
-    if ($changed_conf) {
+    if ($pushed) {
         $registry->popApp($app_conf);
     }
     break;