Implement new API framework.
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 4 Aug 2009 00:10:31 +0000 (18:10 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 4 Aug 2009 03:06:03 +0000 (21:06 -0600)
See Horde_Registry_Api:: for a brief rundown on what the new API files
need.

framework/Core/lib/Horde/Registry.php
framework/Core/lib/Horde/Registry/Api.php [new file with mode: 0644]
framework/Core/package.xml

index 060bd1a..b389c4d 100644 (file)
@@ -325,6 +325,8 @@ class Horde_Registry
 
     /**
      * Fills the registry's API cache with the available services and types.
+     *
+     * @throws Horde_Exception
      */
     protected function _loadApiCache()
     {
@@ -341,17 +343,38 @@ class Horde_Registry
             $status[] = 'admin';
         }
 
-        $this->_cache['api'] = $this->_cache['type'] = array();
+        $this->_cache['api'] = array();
+
+        /* Initialize complex types. */
+        $this->_cache['type'] = array(
+            'hash' => array(
+                array('item' => '{urn:horde}hashItem')
+            ),
+            'hashHash' => array(
+                array('item' => '{urn:horde}hashHashItem')
+            ),
+            'hashItem' => array(
+                'key' => 'string',
+                'value' => 'string'
+            ),
+            'hashHashItem' => array(
+                'key' => 'string',
+                'value' => '{urn:horde}hash'
+            ),
+            'stringArray' => array(
+                array('item' => 'string')
+            )
+        );
 
         foreach (array_keys($this->applications) as $app) {
-            $_services = $_types = null;
-            $api = $this->get('fileroot', $app) . '/lib/api.php';
-            if (is_readable($api)) {
-                include_once $api;
+            if (!in_array($this->applications[$app]['status'], $status)) {
+                continue;
             }
-            $this->_cache['api'][$app] = $_services;
-            if (!is_null($_types)) {
-                foreach ($_types as $type => $params) {
+
+            $api = $this->_getApiOb($app);
+            $this->_cache['api'][$app] = $api->services;
+            if (!empty($api->types)) {
+                foreach ($api->types as $type => $params) {
                     /* Prefix non-Horde types with the application name. */
                     $prefix = ($app == 'horde') ? '' : "${app}_";
                     $this->_cache['type'][$prefix . $type] = $params;
@@ -369,6 +392,32 @@ class Horde_Registry
     }
 
     /**
+     * Retrieve the API object for a given application.
+     *
+     * @param string $app  The application to load.
+     *
+     * @return Horde_Registry_Api  The API object.
+     * @throws Horde_Exception
+     */
+    protected function _getApiOb($app)
+    {
+        if (isset($this->_cache['apiob'][$app])) {
+            return $this->_cache['apiob'][$app];
+        }
+
+        /* Can't autoload here, since the application may not have been
+         * initialized yet. */
+        $classname = $app . '_Api';
+        if (!@include_once $this->get('fileroot', $app) . '/lib/Api.php') {
+            throw new Horde_Exception('Application ' . $app . ' is missing its API file.');
+        }
+
+        $this->_cache['apiob'][$app] = new $classname;
+
+        return $this->_cache['apiob'][$app];
+    }
+
+    /**
      * Return a list of the installed and registered applications.
      *
      * @param array $filter   An array of the statuses that should be
@@ -584,15 +633,11 @@ class Horde_Registry
         }
 
         /* Load the API now. */
-        $api = $this->get('fileroot', $app) . '/lib/api.php';
-        if (is_readable($api)) {
-            include_once $api;
-        }
+        $api = $this->_getApiOb($app);
 
         /* Make sure that the function actually exists. */
-        $function = '_' . $app . '_' . str_replace('/', '_', $call);
-        if (!function_exists($function)) {
-            throw new Horde_Exception('The function implementing ' . $call . ' (' . $function . ') is not defined in ' . $app . '\'s API.');
+        if (!method_exists($api, $call)) {
+            throw new Horde_Exception('The function implementing ' . $call . ' is not defined in ' . $app . '\'s API.');
         }
 
         $checkPerms = isset($this->_cache['api'][$app][$call]['checkperms'])
@@ -605,7 +650,7 @@ class Horde_Registry
         $pushed = $this->pushApp($app, array('check_perms' => $checkPerms));
 
         try {
-            $result = call_user_func_array($function, $args);
+            $result = call_user_func_array(array($api, $call), $args);
         } catch (Horde_Exception $e) {
             $result = $e;
         }
@@ -1039,13 +1084,37 @@ class Horde_Registry
      */
     public function getVersion($app = null)
     {
-        if (is_null($app)) {
+        if (empty($app)) {
             $app = $this->getApp();
         }
 
-        require_once $this->get('fileroot', $app) . '/lib/version.php';
+        try {
+            $api = $this->_getApiOb($app);
+            return $api->version;
+        } catch (Horde_Exception $e) {
+            return 'unknown';
+        }
+    }
 
-        return constant(strtoupper($app) . '_VERSION');
+    /**
+     * Does the given application have a mobile view?
+     *
+     * @param string $app  The application to check.
+     *
+     * @return boolean  Whether app has mobile view.
+     */
+    public function hasMobileView($app = null)
+    {
+        if (empty($app)) {
+            $app = $this->getApp();
+        }
+
+        try {
+            $api = $this->_getApiOb($app);
+            return $api->mobileView;
+        } catch (Horde_Exception $e) {
+            return false;
+        }
     }
 
     /**
diff --git a/framework/Core/lib/Horde/Registry/Api.php b/framework/Core/lib/Horde/Registry/Api.php
new file mode 100644 (file)
index 0000000..8464c76
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+/**
+ * Template class for application API files.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author  Michael Slusarz <slusarz@horde.org>
+ * @package Core
+ */
+class Horde_Registry_Api
+{
+    /**
+     * Does this application support a mobile view?
+     *
+     * @var boolean
+     */
+    public $mobileView = false;
+
+    /**
+     * The application's version.
+     *
+     * @var string
+     */
+    public $version = 'unknown';
+
+    /**
+     * The services provided by this application.
+     * TODO: Describe structure.
+     *
+     * @var array
+     */
+    public $services = array(
+        'perms' => array(
+            'args' => array(),
+            'type' => '{urn:horde}hashHash'
+        )
+    );
+
+    /**
+     * TODO
+     * TODO: Describe structure.
+     *
+     * @var array
+     */
+    public $types = array();
+
+    /* Reserved functions. */
+
+    /**
+     * Returns a list of available permissions.
+     *
+     * @return array  The permissions list.
+     *                TODO: Describe structure.
+     */
+    public function perms()
+    {
+        return array();
+    }
+}
index f7d6ec5..cf47b34 100644 (file)
@@ -37,7 +37,8 @@ Application Framework.
   <api>beta</api>
  </stability>
  <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL</license>
- <notes>* Moved Horde_Exception to Exception package.
+ <notes>* Added Horde_Registry_Api:: template class.
+ * Moved Horde_Exception to Exception package.
  * Renamed Menu:: as Horde_Menu::.
  * Renamed Help:: as Horde_Help::.
  * Removed Text::/Horde_Text::.
@@ -56,6 +57,7 @@ Application Framework.
      <file name="Menu.php" role="php" />
      <file name="Registry.php" role="php" />
      <dir name="Registry">
+      <file name="Api.php" role="php" />
       <file name="Caller.php" role="php" />
      </dir> <!-- /lib/Horde/Registry -->
      <dir name="Script">
@@ -125,6 +127,7 @@ Application Framework.
    <install name="lib/Horde/Help.php" as="Horde/Help.php" />
    <install name="lib/Horde/Menu.php" as="Horde/Menu.php" />
    <install name="lib/Horde/Registry.php" as="Horde/Registry.php" />
+   <install name="lib/Horde/Registry/Api.php" as="Horde/Registry/Api.php" />
    <install name="lib/Horde/Registry/Caller.php" as="Horde/Registry/Caller.php" />
    <install name="lib/Horde/Script/Files.php" as="Horde/Script/Files.php" />
    <install name="lib/Horde.php" as="Horde.php" />