aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTom Needham <tom@owncloud.com>2012-07-30 13:39:06 +0000
committerTom Needham <tom@owncloud.com>2012-07-30 13:39:06 +0000
commitc7c16ac49b661d5087cd64612bce1da5630424b0 (patch)
tree709db1bb30d05d78eef9c0dd393e16e2f9c43d81
parente47a8a9f0937051c17d5f95652098b53610f8cb6 (diff)
downloadnextcloud-server-c7c16ac49b661d5087cd64612bce1da5630424b0.tar.gz
nextcloud-server-c7c16ac49b661d5087cd64612bce1da5630424b0.zip
Improve merging of api responses
-rw-r--r--lib/api.php47
1 files changed, 35 insertions, 12 deletions
diff --git a/lib/api.php b/lib/api.php
index 17663b53b84..02c3f77e5c2 100644
--- a/lib/api.php
+++ b/lib/api.php
@@ -58,23 +58,17 @@ class OC_API {
self::loadRoutes();
$name = $parameters['_name'];
- $response = array();
// Loop through registered actions
foreach(self::$actions[$name] as $action){
+ $app = $action['app'];
if(is_callable($action['action'])){
- $action_response = call_user_func($action['action'], $parameters);
- if(is_array($action_response)){
- // Merge with previous
- $response = array_merge($response, $action_response);
- } else {
- // TODO - Something failed, do we return an error code, depends on other action responses
- }
+ $responses[] = array('app' => $app, 'response' => call_user_func($action['action'], $parameters));
} else {
- // Action not callable
- // log
- // TODO - Depending on other action responses, do we return a 501?
+ $responses[] = array('app' => $app, 'response' => 501);
}
}
+ // Merge the responses
+ $response = self::mergeResponses($responses);
// Send the response
if(isset($parameters['_format'])){
self::respond($response, $parameters['_format']);
@@ -84,6 +78,35 @@ class OC_API {
}
/**
+ * intelligently merges the different responses
+ * @param array $responses
+ * @return array the final merged response
+ */
+ private static function mergeResponses($responses){
+ $finalresponse = array();
+ $numresponses = count($responses);
+
+ // TODO - This is only a temporary merge. If keys match and value is another array we want to compare deeper in the array
+ foreach($responses as $response){
+ if(is_int($response) && empty($finalresponse)){
+ $finalresponse = $response;
+ continue;
+ }
+ if(is_array($response)){
+ // Shipped apps win
+ if(OC_App::isShipped($response['app'])){
+ $finalresponse = array_merge($finalresponse, $response);
+ } else {
+ $finalresponse = array_merge($response, $finalresponse);
+ }
+ }
+ }
+ // END TODO
+
+ return $finalresponse;
+ }
+
+ /**
* loads the api routes
*/
private static function loadRoutes(){
@@ -107,4 +130,4 @@ class OC_API {
// TODO respond in the correct format
}
- } \ No newline at end of file
+} \ No newline at end of file