[Sungrazr-svn] [90] Sungrazr_Server: [NEW] Solarized refactoring of useful bits from Zend_Server

0 views
Skip to first unread message

cl...@killersoft.com

unread,
Dec 16, 2007, 9:17:34 PM12/16/07
to Sungra...@googlegroups.com
Revision
90
Author
clay
Date
2007-12-16 18:17:33 -0800 (Sun, 16 Dec 2007)

Log Message

Sungrazr_Server: [NEW] Solarized refactoring of useful bits from Zend_Server

Added Paths

Diff

Added: trunk/Sungrazr/Server/Adapter/JsonRpc.php (0 => 90)


--- trunk/Sungrazr/Server/Adapter/JsonRpc.php	                        (rev 0)
+++ trunk/Sungrazr/Server/Adapter/JsonRpc.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,75 @@
+<?php
+/**
+ * 
+ * JSON-RPC server adapter.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+
+class Sungrazr_Server_Adapter_JsonRpc extends Sungrazr_Server_Adapter {
+    
+    /**
+     * 
+     * Handle a JSON-RPC request.
+     * 
+     * @param mixed $request
+     * 
+     * @return mixed
+     * 
+     */    
+    public function handle($request = false)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Generate a server fault.
+     * 
+     * @param mixed $fault
+     * 
+     * @param int $code
+     * 
+     * @return mixed
+     * 
+     */
+    public function fault($fault = null, $code = 404)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Serialize the data in this object to a JSON-RPC response.
+     * 
+     * @return string
+     * 
+     */
+    protected function _serialize()
+    {
+        return $str;
+    }
+    
+    /**
+     * 
+     * Populate this request with data specified in the serialized request.
+     * 
+     * @return void
+     *
+     */
+    protected function _unserialize($serialized)
+    {
+        
+    }
+}
\ No newline at end of file
Property changes on: trunk/Sungrazr/Server/Adapter/JsonRpc.php
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/Sungrazr/Server/Adapter/Rest.php (0 => 90)


--- trunk/Sungrazr/Server/Adapter/Rest.php	                        (rev 0)
+++ trunk/Sungrazr/Server/Adapter/Rest.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,75 @@
+<?php
+/**
+ * 
+ * REST server adapter.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+
+class Sungrazr_Server_Adapter_Rest extends Sungrazr_Server_Adapter {
+    
+    /**
+     * 
+     * Handle a REST request.
+     * 
+     * @param mixed $request
+     * 
+     * @return mixed
+     * 
+     */    
+    public function handle($request = false)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Generate a server fault.
+     * 
+     * @param mixed $fault
+     * 
+     * @param int $code
+     * 
+     * @return mixed
+     * 
+     */
+    public function fault($fault = null, $code = 404)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Serialize the data in this object to a REST response.
+     * 
+     * @return string
+     * 
+     */
+    protected function _serialize()
+    {
+        return $str;
+    }
+    
+    /**
+     * 
+     * Populate this request with data specified in the serialized request.
+     * 
+     * @return void
+     *
+     */
+    protected function _unserialize($serialized)
+    {
+        
+    }
+}
\ No newline at end of file
Property changes on: trunk/Sungrazr/Server/Adapter/Rest.php
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/Sungrazr/Server/Adapter/XmlRpc.php (0 => 90)


--- trunk/Sungrazr/Server/Adapter/XmlRpc.php	                        (rev 0)
+++ trunk/Sungrazr/Server/Adapter/XmlRpc.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,75 @@
+<?php
+/**
+ * 
+ * XML-RPC server adapter.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+
+class Sungrazr_Server_Adapter_XmlRpc extends Sungrazr_Server_Adapter {
+    
+    /**
+     * 
+     * Handle a JSON-RPC request.
+     * 
+     * @param mixed $request
+     * 
+     * @return mixed
+     * 
+     */    
+    public function handle($request = false)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Generate a server fault.
+     * 
+     * @param mixed $fault
+     * 
+     * @param int $code
+     * 
+     * @return mixed
+     * 
+     */
+    public function fault($fault = null, $code = 404)
+    {
+        
+    }
+    
+    /**
+     * 
+     * Serialize the data in this object to a XML-RPC response.
+     * 
+     * @return string
+     * 
+     */
+    protected function _serialize()
+    {
+        return $str;
+    }
+    
+    /**
+     * 
+     * Populate this request with data specified in the serialized request.
+     * 
+     * @return void
+     *
+     */
+    protected function _unserialize($serialized)
+    {
+        
+    }
+}
\ No newline at end of file
Property changes on: trunk/Sungrazr/Server/Adapter/XmlRpc.php
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/Sungrazr/Server/Adapter.php (0 => 90)


--- trunk/Sungrazr/Server/Adapter.php	                        (rev 0)
+++ trunk/Sungrazr/Server/Adapter.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,216 @@
+<?php
+/**
+ * 
+ * Abstract server adapter.
+ * 
+ * Heavily modified and refactored from Zend_Server and related classes.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+
+/**
+ * 
+ * Abstract server adapter.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ */
+abstract class Sungrazr_Server_Adapter extends Solar_Base implements Serializable {
+    
+    /**
+     * 
+     * User-provided configuration.
+     * 
+     * Config keys are ...
+     * 
+     * @var array
+     * 
+     */
+    protected $_Sungrazr_Server_Adapter = array(
+    );
+    
+    /**
+     * 
+     * Sungrazer_Server_Api object handle.
+     * 
+     * @var Sungrazr_Server_Api
+     * 
+     */
+    protected $_api;
+
+    /**
+     * 
+     * Constructor.
+     * 
+     * @param array $config User-provided configuration values.
+     * 
+     */
+    public function __construct($config = null)
+    {
+        // basic construction
+        parent::__construct($config);
+        
+        // API object to perform introspection and validation of requests.
+        $this->_api = Solar::factory('Sungrazr_Server_Api');
+    }
+    
+    /**
+     * 
+     * Attach a class to a server.
+     * 
+     * @param mixed $class The class to add to the server API.
+     * 
+     * @param string $namespace Optionally specify a namespace for the methods
+     * of the added class. If empty, no namespace will be used. Default is 
+     * 'auto', which automatically sets the namespace as the lowercased final
+     * segment of the class name.
+     *
+     */
+    public function addClass($class, $namespace = 'auto') 
+    {
+        $this->_api->addClass($class, $namespace);
+    }
+
+    /**
+     * 
+     * Generate a server fault.
+     * 
+     * @param mixed $fault A description of the fault. Will be serialized by
+     * the server adapter before it is returned.
+     * 
+     * @param int $code Fault code
+     * 
+     * @return mixed
+     * 
+     */
+    abstract public function fault($fault = null, $code = 404);
+
+    /**
+     * 
+     * Handle a request.
+     * 
+     * Dispatches server request to appropriate method and returns a response.
+     * 
+     * @param mixed $request
+     * 
+     * @return mixed
+     * 
+     */
+    abstract public function handle($request = false);
+    
+    /**
+     * 
+     * Return a list of methods the server supports, by name.
+     * 
+     * @see http://scripts.incutio.com/xmlrpc/introspection.html
+     * 
+     * @see http://xmlrpc-c.sourceforge.net/introspection.html
+     * 
+     * @return array
+     * 
+     */
+    public function listMethods()
+    {
+        
+    }
+    
+    /**
+     * 
+     * Return a description of the argument format a particular method 
+     * expects.
+     * 
+     * The result is an array of strings. The first element tells the type
+     * of the method's result. The rest (if any) tell the types of the
+     * method's parameters, in order.
+     * 
+     * @see http://scripts.incutio.com/xmlrpc/introspection.html
+     * 
+     * @see http://xmlrpc-c.sourceforge.net/introspection.html
+     * 
+     * @return array
+     * 
+     */
+    public function methodSignature($method = null)
+    {
+        
+    }
+
+    /**
+     * 
+     * Returns a text description of a particular method.
+     * 
+     * The returned string is intended for human use. The server may give as
+     * much or as little detail as it wants, including an empty string.
+     * 
+     * The string may contain HTML.
+     * 
+     * @see http://scripts.incutio.com/xmlrpc/introspection.html
+     * 
+     * @see http://xmlrpc-c.sourceforge.net/introspection.html
+     * 
+     * @return string
+     * 
+     */
+    public function methodHelp($method = null)
+    {
+        
+    }
+
+    /**
+     * 
+     * Serializable: Returns a string representation of the instance.
+     * 
+     * @return string
+     * 
+     */
+    public function serialize()
+    {
+        return $this->_serialize();
+    }
+    
+    /**
+     * 
+     * Serializable: Populates the instance with serialized data.
+     * 
+     * @return void
+     * 
+     */
+    public function unserialize($serialized)
+    {
+        $this->_unserialize($serialized);
+    }
+    
+    /**
+     * 
+     * serialize() Implementation details for the Serializable interface.
+     * 
+     * @return string
+     * 
+     */
+    abstract protected function _serialize();
+    
+    /**
+     * 
+     * unserialize() Implementation details for the Serializable interface.
+     * 
+     * @param mixed $serialized Value to unserialize.
+     * 
+     * @return void
+     * 
+     */    
+    abstract protected function _unserialize($serialized);
+    
+}
\ No newline at end of file
Property changes on: trunk/Sungrazr/Server/Adapter.php
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/Sungrazr/Server/Api.php (0 => 90)


--- trunk/Sungrazr/Server/Api.php	                        (rev 0)
+++ trunk/Sungrazr/Server/Api.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,222 @@
+<?php
+/**
+ * 
+ * Reflection-based server introspection.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @subpackage Sungrazr_Server_Api
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+class Sungrazr_Server_Api extends Solar_Docs_Apiref {
+    
+    /**
+     * 
+     * User-provided configuration.
+     * 
+     * Config keys are ...
+     * 
+     * `life`
+     * : (int) The lifetime of each cache entry in seconds.
+     * 
+     * @var array
+     * 
+     */
+    protected $_Sungrazr_Server_Api = array(
+        'phpdoc' => null,
+        'cache' => array(
+            'adapter'   => 'Solar_Cache_Adapter_File',
+            'active'    => true,
+            'life'      => 86400,
+        ),        
+    );
+
+    /**
+     * 
+     * PHP's Magic Methods, these are ignored.
+     * 
+     * @var array
+     * 
+     */
+    protected $_magic_methods = array(
+        '__construct',
+        '__destruct',
+        '__get',
+        '__set',
+        '__call',
+        '__sleep',
+        '__wakeup',
+        '__isset',
+        '__unset',
+        '__tostring',
+        '__clone',
+        '__set_state',
+    );
+    
+    /**
+     * 
+     * Cache keys generated by the _fetchCachedClass() method, which 
+     * are susequently used to add a parsed class to the cache.
+     * 
+     * @param array
+     * 
+     */    
+    protected $_class_cache_keys = array();
+
+    /**
+     * 
+     * Solar_Cache object for caching parsed class information.
+     * 
+     * @param Solar_Cache_Adapter
+     * 
+     */
+    protected $_cache;
+
+    /**
+     * 
+     * Constructor.
+     * 
+     * @param array $config User-provided configuration values.
+     * 
+     */
+    public function __construct($config = null)
+    {
+        // set the phpdoc parser to Sungrazr extension
+        $this->_Sungrazr_Server_Api['phpdoc'] = Solar::factory(
+            'Sungrazr_Docs_Phpdoc'
+        );
+        
+        // basic construction
+        parent::__construct($config);     
+        
+        // set up class cache
+        if (! empty($this->_config['cache'])) {
+            $this->_cache = Solar::factory('Solar_Cache', 
+                $this->_config['cache']
+            );
+        }
+    }
+    
+    /**
+     * 
+     * Performs class/object reflection for server introspection.
+     * 
+     * Introspection is performed by leveraging the reflection/introspection
+     * capabilities found in Solar_Docs_Apiref. Since such introspection is
+     * computationally expensive, aggressive caching (24 hours by default) is 
+     * done using the output of hash_file() on the class file as keys. 
+     * 
+     * @param mixed $class The class to add to the server API. If an object is 
+     * passed, the class will be picked out using get_class().
+     * 
+     * @param string $namespace Optionally specify a namespace for the methods
+     * of the added class. If empty, no namespace will be used. Default is 
+     * 'auto', which automatically sets the namespace as the lowercased final
+     * segment of the class name.
+     * 
+     * @return bool
+     * 
+     */
+    public function addClass($class, $namespace = 'auto')
+    {
+        if (is_object($class)) {
+            $class = get_class($class);
+        }
+        
+        // fetch from cache if possible
+        if ($cached = $this->_fetchCachedClass($class)) {
+            $this->api[$class] = $cached;
+            return true;
+        }
+        
+        $added = parent::addClass($class);
+        
+        if (! $added) {
+            return false;
+        }
+        
+        // drop Solar_Base descendants and magic methods
+        foreach ($this->api[$class]['methods'] as $method => $info) {
+            if ($info['from'] == 'Solar_Base' || 
+                in_array($method, $this->_magic_methods)) {
+                
+                unset($this->api[$class]['methods'][$method]);
+                
+            }
+        }
+        
+        foreach ($this->api[$class]['properties'] as $property => $info) {
+            if ($info['from'] == 'Solar_Base' || $info['access'] != 'public') {
+                
+                unset($this->api[$class]['properties'][$property]);
+                
+            }
+        }
+        
+        // cache result
+        $this->_addCachedClass($class);
+                
+        return true;
+        
+    }
+    
+    /**
+     * 
+     * Fetch cached API reference output.
+     * 
+     * Attempt to retrieve the Apiref-parsed array for the class. Since
+     * parsing is expensive, avoid retrieving any more often than necessary
+     * by using hash_file() to generate a cache key for the named class.
+     * 
+     * This causes us to hit the disk, but is less expensive than the full
+     * Apiref-parsing operation w/reflection.
+     * 
+     * @param string $class Class name to retrieve from cache.
+     * 
+     */
+    protected function _fetchCachedClass($class)
+    {
+        // nothing to do if a cache adapter is not configured
+        if (empty($this->_cache)) {
+            return false;
+        }
+        
+        $path = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
+        if ($full_path = Solar::fileExists($path)) {
+            // get hash of file in this location
+            $this->_class_cache_keys[$class] = hash_file('md5', $full_path);
+        }
+        
+        return $this->_cache->fetch($this->_class_cache_keys[$class]);
+    }
+    
+    /**
+     * 
+     * Add a class API reference to the cache.
+     * 
+     * @param string $class Name of class API to cache.
+     * 
+     */
+    protected function _addCachedClass($class)
+    {
+        // nothing to do if a cache adapter is not configured
+        if (empty($this->_cache)) {
+            return false;
+        }
+        
+        return $this->_cache->add(
+            $this->_class_cache_keys[$class],
+            $this->api[$class]
+        );
+    }
+}
\ No newline at end of file
Property changes on: trunk/Sungrazr/Server/Api.php
___________________________________________________________________
Name: svn:keywords
   + Id

Added: trunk/Sungrazr/Server.php (0 => 90)


--- trunk/Sungrazr/Server.php	                        (rev 0)
+++ trunk/Sungrazr/Server.php	2007-12-17 02:17:33 UTC (rev 90)
@@ -0,0 +1,66 @@
+<?php
+/**
+ * 
+ * Factory class for server adapters.
+ * 
+ * Heavily modified and refactored from Zend_Server and related classes.
+ * 
+ * Developed for, and then donated by, Mashery.com <http://mashery.com>.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ * @author Clay Loveless <cl...@killersoft.com>
+ * 
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * 
+ * @version SVN: $Id$
+ * 
+ */
+
+/**
+ * 
+ * Factory class for server adapters.
+ * 
+ * @category Sungrazr
+ * 
+ * @package Sungrazr_Server
+ * 
+ */
+class Sungrazr_Server extends Solar_Base {
+    
+    /**
+     * 
+     * User-provided configuration.
+     * 
+     * Keys are ...
+     * 
+     * `adapter`
+     * : (string) The adapter class for the factory, default 
+     * 'Sungrazr_Server_Adapter_Rest'.
+     * 
+     * @var array
+     * 
+     */
+    protected $_Sungrazr_Server = array(
+        'adapter' => 'Sungrazr_Server_Adapter_Rest',
+    );
+    
+    /**
+     * 
+     * Factory method to create server adapter objects.
+     * 
+     * @return Sungrazr_Server_Adapter
+     * 
+     */
+    public function solarFactory()
+    {
+        // bring in the config and get the adapter class.
+        $config = $this->_config;
+        $class = $config['adapter'];
+        unset($config['adapter']);
+                
+        return Solar::factory($class, $config);
+    }
+}
Property changes on: trunk/Sungrazr/Server.php
___________________________________________________________________
Name: svn:keywords
   + Id
Reply all
Reply to author
Forward
0 new messages