[Sungrazr-svn] [93] Sungrazr_Server_Adapter: [CHG] Continued fleshing out base server adapter.

0 views
Skip to first unread message

cl...@killersoft.com

unread,
Dec 17, 2007, 2:54:38 PM12/17/07
to Sungra...@googlegroups.com
Revision
93
Author
clay
Date
2007-12-17 11:54:37 -0800 (Mon, 17 Dec 2007)

Log Message

Sungrazr_Server_Adapter: [CHG] Continued fleshing out base server adapter.

- Added support for the calls to server specials

- Added a default response method that leverages Serializable

- Implemented server.listMethods() special

- Whitespace and doc cleanup

- Support for custom headers in response

- Config option for returning response or echoing it out

Modified Paths

Diff

Modified: trunk/Sungrazr/Server/Adapter.php (92 => 93)


--- trunk/Sungrazr/Server/Adapter.php	2007-12-17 19:51:07 UTC (rev 92)
+++ trunk/Sungrazr/Server/Adapter.php	2007-12-17 19:54:37 UTC (rev 93)
@@ -36,10 +36,20 @@
      * 
      * Config keys are ...
      * 
+     * `return`
+     * : (bool) Whether or not to return a response. Defaults to **FALSE**. 
+     * When **TRUE**, the handle() method will not send output but will
+     * instead return the response that would otherwise have been sent.
+     * 
+     * `headers`
+     * : (array) Associative array of headers to return with every response.
+     * 
      * @var array
      * 
      */
     protected $_Sungrazr_Server_Adapter = array(
+        'return'    => false,
+        'headers'   => array()
     );
     
     /**
@@ -50,9 +60,37 @@
      * 
      */
     protected $_api;
-
+    
     /**
      * 
+     * HTTP headers to send with the response.
+     * 
+     * @var array
+     * 
+     */
+    protected $_headers = array();
+    
+    /**
+     * 
+     * Array of introspective API information, indexed by callable method
+     * names instead of internal class names.
+     * 
+     * @var array
+     * 
+     */
+    protected $_server_api = array();
+        
+    /**
+     * 
+     * Special introspective methods provided by the server.
+     * 
+     * @var array
+     * 
+     */
+    protected $_server_specials = array();
+    
+    /**
+     * 
      * Constructor.
      * 
      * @param array $config User-provided configuration values.
@@ -63,8 +101,19 @@
         // basic construction
         parent::__construct($config);
         
+        // make extra-sure that no headers are output prematurely
+        ob_start();
+        
         // API object to perform introspection and validation of requests.
         $this->_api = Solar::factory('Sungrazr_Server_Api');
+        
+        // define special methods
+        $this->_server_specials = array(
+            'system.listMethods'        => array(&$this, 'listMethods'),
+            'system.describe'           => array(&$this, 'listMethods'),
+            'system.methodSignature'    => array(&$this, 'methodSignature'),
+            'system.methodHelp'         => array(&$this, 'methodHelp'),            
+        );        
     }
     
     /**
@@ -82,6 +131,10 @@
     public function addClass($class, $namespace = 'auto') 
     {
         $this->_api->addClass($class, $namespace);
+        $this->_server_api = array_merge(
+            $this->_server_api,
+            $this->_api->api[$class]['server_api']
+        );
     }
 
     /**
@@ -100,6 +153,18 @@
 
     /**
      * 
+     * Return the internal API information.
+     * 
+     * @return Sungrazr_Server_Api
+     * 
+     */
+    public function getApi()
+    {
+        return $this->_api;
+    }
+
+    /**
+     * 
      * Handle a request.
      * 
      * Dispatches server request to appropriate method and returns a response.
@@ -109,7 +174,7 @@
      * @return mixed
      * 
      */
-    abstract public function handle($request = false);
+    abstract public function handle(Solar_Request $request);
     
     /**
      * 
@@ -124,7 +189,12 @@
      */
     public function listMethods()
     {
-        
+        $methods = array_keys($this->_server_api);
+        foreach ($this->_server_specials as $method => $callable) {
+            $methods[] = $method;
+        }
+        sort($methods);
+        return $methods;
     }
     
     /**
@@ -185,13 +255,57 @@
      * 
      * Serializable: Populates the instance with serialized data.
      * 
-     * @return void
+     * @return bool
      * 
      */
     public function unserialize($serialized)
     {
-        $this->_unserialize($serialized);
+        return $this->_unserialize($serialized);
     }
+        
+    /**
+     * 
+     * Common response method leveraging Serializable interface.
+     * 
+     * @return string
+     * 
+     */
+    protected function _response()
+    {
+        $response = $this->serialize();
+        
+        if (! $this->_config['return']) {
+            
+            // merge headers
+            $this->_headers = array_merge(
+                $this->_config['headers'],
+                $this->_headers
+            );
+            
+            // append to Server header
+            $server_class = get_class($this);
+            if (! empty($this->_headers['Server'])) {
+                $this->_headers['Server'] = rtrim($this->_headers['Server']) .
+                    ' (' . $server_class . ')';
+            } else {
+                $this->_headers['Server'] = $server_class;
+            }
+            
+            if (! empty($this->_headers) && ! headers_sent()) {
+                foreach ($this->_headers as $header => $value) {
+                    $h = trim($header);
+                    $v = trim($value);
+                    if (! empty($v)) {
+                        $h .= ': ' . $v;
+                    }
+                    header($h);
+                }
+            }
+            echo $response;
+            return;
+        }
+        return $response;        
+    }
     
     /**
      * 
@@ -208,7 +322,7 @@
      * 
      * @param mixed $serialized Value to unserialize.
      * 
-     * @return void
+     * @return bool
      * 
      */    
     abstract protected function _unserialize($serialized);
Reply all
Reply to author
Forward
0 new messages