? rest_server_no_zend-2.patch
Index: rest_server.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.info,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 rest_server.info
--- rest_server.info	5 Jan 2008 05:39:54 -0000	1.1.2.1
+++ rest_server.info	7 Mar 2009 16:26:11 -0000
@@ -2,4 +2,4 @@
 name = REST Server
 description = Provides a REST server.
 package = "Services - servers"
-dependancies = zend services
+dependancies = services
Index: rest_server.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/rest_server/Attic/rest_server.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 rest_server.module
--- rest_server.module	5 Jan 2008 05:39:54 -0000	1.1.2.1
+++ rest_server.module	7 Mar 2009 16:26:11 -0000
@@ -2,58 +2,136 @@
 // $Id: rest_server.module,v 1.1.2.1 2008/01/05 05:39:54 robloach Exp $
 
 /**
- * Implementation of hook_server_info()
+ * Implementation of hook_server_info().
  */
 function rest_server_server_info() {
   return array(
     '#name' => 'REST',
     '#path' => 'rest',
   );
-} // function rest_server_server_info
+} 
 
 /**
  * Implementation of hook_server()
  */
 function rest_server_server() {
-  // Initiate the server and the request
-  if (zend_initialize('Zend_Rest_Server')) {
-    $request = $_REQUEST;
-    $server = new Zend_Rest_Server();
-    
-    // Check for which service we're calling
-    foreach (services_get_all() as $method) {
-      if ($method['#method'] == $request['method']) {
-        $request['method'] = $method['#callback'];
-        $server->addFunction($method['#callback']);
-        
-        // Special use cases for arguments of certain types
-        foreach ($method['#args'] as $argument) {
-          $argname = $argument['#name'];
-          switch ($argument['#type']) {
-            case 'array' :
-              if (empty($request[$argname])) {
-                $request[$argname] = array();
-              }
-              else {
-                $request[$argname] = explode(',', $request[$argname]);
-              }
-              break;
-          }
+  $request = $_REQUEST;
+  $args = array();
+
+  // Check for which service we're calling
+  $method_name = $request['method'];
+  foreach (services_get_all() as $method) {
+    if ($method['#method'] == $request['method']) {
+      // Special use cases for arguments of certain types
+      foreach ($method['#args'] as $argument) {
+        $argname = $argument['#name'];
+        switch ($argument['#type']) {
+          case 'array' :
+            if (empty($request[$argname])) {
+              $args[] = array();
+            }
+            else {
+              $args[] = explode(',', $request[$argname]);
+            }
+            break;
+          case 'string':
+            if(empty($request[$argname])) {
+              $args[] = '';
+            }
+            else {
+              $args[] = $request[$argname];
+            }
+            break;
+          default:
+            // parameters of other types
+            $args[] = $request[$argname];
+            break;
         }
-        
-        // Found method to call, so exit
-        break;
       }
+      break;
     }
-    try {
-      echo $server->handle($request);
+  }
+
+  try {
+    $response = services_method_call($method_name, $args);
+
+    // If 'status' is set, an error ocurred.
+    if (is_array($response) && isset($response['status'])) {
+      $result = $response;
     }
-    catch (Exception $e) {
-      echo $e->getMessage();
+    else {
+      $result = array(
+        'status' => 'success',
+        'response' => $response,
+      );
     }
   }
-  else {
-    drupal_set_message(t('The <em>REST Server</em> requires proper configuration of the <a href="@url" target="_blank">Zend Framework</a>. Please <a href="@configure">configure it</a> correctly and try again.', array('@url' => 'http://framework.zend.com', '@configure' => url('admin/settings/zend'))), 'error');
-    drupal_goto(variable_get('site_frontpage', 'node'));
+  catch (Exception $e) {
+    $result = rest_server_server_error($e->getMessage());
   }
-} // function rest_server_server
+
+  drupal_set_header('Content-type: text/xml');
+  print rest_server_to_xml($result);
+}
+
+/**
+ * Implementation of hook_server_error
+ *
+ * @param string $message
+ * @return array
+ */
+function rest_server_server_error($message) {
+  return array(
+    'status' => 'error',
+    'message' => $message,
+  );
+}
+
+/**
+ * Convert data array to xml representation.
+ *
+ * @param $data
+ *   Array or object with data to convert.
+ * @param $xml
+ *   SimpleXML object, used in recursive calls.
+ * @param $depth
+ *   Node depth, used in recursive calls.
+ * 
+ * @return
+ *   XML string.
+ */
+function rest_server_to_xml($data, $xml = NULL, $depth = 0)  {
+  if (!isset($xml)) {
+    $xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><rest generator="xml_writer" version="1.0" />');
+  }
+
+  foreach($data as $key => $value) {
+    if (is_array($value)|| is_object($value)) {
+      $child = is_numeric($key) ? _rest_server_item_node($xml, $key) : $xml->addChild($key);
+      rest_server_to_xml($value, $child, $depth + 1);
+    }
+    else {
+      $value = htmlentities($value);
+      if (is_numeric($key)) {
+        _rest_server_item_node($xml, $key, $value);
+      }
+      else {
+        $xml->addChild($key, $value);
+      }
+    }
+  }
+
+  if ($depth == 0) {
+    return $xml->asXML();
+  }
+}
+
+/**
+ * Helper function to generate 'item' nodes for arrays with numeric keys.
+ * E.g. <item id="0">.
+ */
+function _rest_server_item_node($parent, $key, $value = NULL) {
+  $item = isset($value) ? $parent->addChild('item', $value) : $parent->addChild('item');
+  $item->addAttribute('id', $key);
+  return $item;
+}
