Skip to content

Instantly share code, notes, and snippets.

@cotto
Created September 27, 2011 18:32
Show Gist options
  • Save cotto/1245851 to your computer and use it in GitHub Desktop.
Save cotto/1245851 to your computer and use it in GitHub Desktop.
diff --git a/resources/comment_resource.inc b/resources/comment_resource.inc
index 72bd2bd..10071f1 100644
--- a/resources/comment_resource.inc
+++ b/resources/comment_resource.inc
@@ -215,9 +215,7 @@ function _comment_resource_index($page, $fields, $parameters) {
function _comment_resource_create($comment) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($comment['comment']) && !isset($comment['subject'])) {
- $comment = $comment['comment'];
- }
+ $comment = _services_arg_value($comment, 'comment');
if (empty($comment['nid'])) {
return services_error(t('A nid must be provided'));
@@ -277,11 +275,9 @@ function _comment_resource_retrieve($cid) {
* Unique identifier for the comment (cid) or FALSE if there was a problem.
*/
function _comment_resource_update($cid, $comment) {
- $comment['cid'] = $cid;
// Adds backwards compatability with regression fixed in #1083242
- if (isset($comment['data']) && !isset($comment['subject'])) {
- $comment = $comment['data'];
- }
+ $comment = _services_arg_value($comment, 'data');
+ $comment['cid'] = $cid;
$old_comment = (array) _comment_load($cid);
if (empty($old_comment)) {
@@ -361,12 +357,8 @@ function _comment_resource_count_new($nid, $since = 0) {
* Access check callback for comment controllers.
*/
function _comment_resource_access($op = 'view', $args = array()) {
- // Adds backwards compatability with regression fixed in #1083242
- if (is_array($args[0])) {
- if (key_exists('comment', $args[0]) || key_exists('data', $args[0])) {
- $args[0] = $args[0]['comment'] ? $args[0]['comment'] : $args[0]['data'];
- }
- }
+ // Add backwards compatability with regression fixed in #1083242
+ $args = _services_access_value($args, array('comment', 'data'));
if ($op == 'view' && !isset($args[0])) {
return user_access('access comments');
diff --git a/resources/file_resource.inc b/resources/file_resource.inc
index d4f24be..23fba7c 100644
--- a/resources/file_resource.inc
+++ b/resources/file_resource.inc
@@ -154,10 +154,7 @@ function _file_resource_index($page, $fields, $parameters) {
* Unique identifier for the file (fid) or errors if there was a problem.
*/
function _file_resource_create($file) {
- // Adds backwards compatability with regression fixed in #1083242
- if (isset($file['file'])) {
- $file = $file['file'];
- }
+ $file = _services_arg_value($file, 'file');
global $user;
@@ -322,11 +319,7 @@ function _file_resource_delete($fid) {
*/
function _file_resource_access($op = 'view', $args = array()) {
// Adds backwards compatability with regression fixed in #1083242
- if (is_array($args[0])) {
- if (key_exists('file', $args[0]) && count($args[0]) == 1) {
- $args[0] = $args[0]['file'];
- }
- }
+ $args[0] = _services_access_value($args[0], 'file');
global $user;
if (user_access('administer files')) {
return TRUE;
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index 825ac13..aebfb3f 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
@@ -246,9 +246,7 @@ function _node_resource_retrieve($nid) {
*/
function _node_resource_create($node) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($node['node'])) {
- $node = $node['node'];
- }
+ $node = _services_arg_value($node, 'node');
if (!isset($node['name'])) {
// Assign username to the node from $user created at auth step.
@@ -330,9 +328,7 @@ function _node_resource_validate_type($node) {
*/
function _node_resource_update($nid, $node) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($node['node'])) {
- $node = $node['node'];
- }
+ $node = _services_arg_value($node, 'node');
// Validate the node. If there is validation error Exception will be thrown
// so code below won't be executed.
@@ -465,11 +461,7 @@ function _node_resource_load_node_files($nid, $include_file_contents) {
*/
function _node_resource_access($op = 'view', $args = array()) {
// Adds backwards compatability with regression fixed in #1083242
- if (is_array($args[0])) {
- if (key_exists('node', $args[0])) {
- $args[0] = $args[0]['node'];
- }
- }
+ $args[0] = _services_access_value($args[0], 'node');
// Make sure we have an object or this all fails, some servers can
// mess up the types.
if (is_array($args[0])) {
@@ -492,4 +484,4 @@ function _node_resource_access($op = 'view', $args = array()) {
else {
return services_error('Node id: '. $args[0]->nid .' could not be found', 404);
}
-}
\ No newline at end of file
+}
diff --git a/resources/taxonomy_resource.inc b/resources/taxonomy_resource.inc
index 14c1780..3c12c64 100644
--- a/resources/taxonomy_resource.inc
+++ b/resources/taxonomy_resource.inc
@@ -404,9 +404,7 @@ function _taxonomy_term_resource_retrieve($tid) {
*/
function _taxonomy_term_resource_create($term) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($term['term'])) {
- $term = $term['term'];
- }
+ $term = _services_arg_value($term, 'term');
return taxonomy_save_term($term);
}
@@ -425,9 +423,7 @@ function _taxonomy_term_resource_create($term) {
*/
function _taxonomy_term_resource_update($tid, $term) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($term['term'])) {
- $term = $term['term'];
- }
+ $term = _services_arg_value($term, 'term');
$term['tid'] = $tid;
return _taxonomy_term_resource_create($term);
@@ -493,9 +489,7 @@ function _taxonomy_vocabulary_resource_retrieve($vid) {
*/
function _taxonomy_vocabulary_resource_create($vocabulary) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($vocabulary['vocabulary'])) {
- $vocabulary = $vocabulary['vocabulary'];
- }
+ $vocabulary = _services_arg_value($vocabulary, 'vocabulary');
taxonomy_save_vocabulary($vocabulary);
return $vocabulary;
}
@@ -514,9 +508,7 @@ function _taxonomy_vocabulary_resource_create($vocabulary) {
*/
function _taxonomy_vocabulary_resource_update($vid, $vocabulary) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($vocabulary['vocabulary'])) {
- $vocabulary = $vocabulary['vocabulary'];
- }
+ $vocabulary = _services_arg_value($vocabulary, 'vocabulary');
$vocabulary['vid'] = $vid;
return _taxonomy_vocabulary_resource_create($vocabulary);
diff --git a/resources/user_resource.inc b/resources/user_resource.inc
index 75e48d6..e84eb3d 100644
--- a/resources/user_resource.inc
+++ b/resources/user_resource.inc
@@ -214,9 +214,7 @@ function _user_resource_retrieve($uid) {
*/
function _user_resource_create($account) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($account['account'])) {
- $account = $account['account'];
- }
+ $account = _services_arg_value($account, 'account');
// Load the required includes for saving profile information
// with drupal_execute().
@@ -262,9 +260,7 @@ function _user_resource_create($account) {
*/
function _user_resource_update($uid, $account) {
// Adds backwards compatability with regression fixed in #1083242
- if (isset($account['data'])) {
- $account = $account['data'];
- }
+ $account = _services_arg_value($account, 'data');
$account['uid'] = $uid;
$user = user_load($uid);
@@ -443,11 +439,8 @@ function _user_resource_index($page, $fields, $parameters) {
*/
function _user_resource_access($op = 'view', $args = array()) {
// Adds backwards compatability with regression fixed in #1083242
- if (is_array($args[0])) {
- if (key_exists('account', $args[0]) || key_exists('data', $args[0])) {
- $args[0] = $args[0]['account'] ? $args[0]['account'] : $args[0]['data'];
- }
- }
+ $args[0] = _services_access_value($args[0], array('account', 'data'));
+
global $user;
switch ($op) {
case 'view':
diff --git a/services.runtime.inc b/services.runtime.inc
index 92c94bc..b70904f 100644
--- a/services.runtime.inc
+++ b/services.runtime.inc
@@ -182,10 +182,10 @@ function services_controller_execute($controller, $args = array(), $options = ar
$result = call_user_func_array($callable, array($args, $controller, $result));
}
}
-
+
if (session_save_session($user) === FALSE) {
$user = $original_user;
- session_save_session($old_state);
+ session_save_session($old_state);
}
if ($server_info->debug) {
watchdog('services', 'results: <pre>@results</pre>', array('@results' => print_r($result, TRUE)), WATCHDOG_DEBUG);
@@ -451,3 +451,49 @@ function services_session_unload($backup) {
session_save_session(TRUE);
}
+
+
+/**
+ * Extract arguments for a services method callback, preserving backwards compatibility with #1083242.
+ *
+ * @param array $data
+ * original argument passed to a resource method callback
+ * @param string $field
+ * name of the field where arguments should be checked for
+ * @return array
+ */
+
+// Adds backwards compatability with regression fixed in #1083242
+function _services_arg_value($data, $field) {
+ if (isset($data[$field]) && count($data) == 1 && is_array($data[$field])) {
+ return $data[$field];
+ }
+ return $data;
+}
+
+
+/**
+ * Extract arguments for a services method access callback, preserving backwards compatibility with #1083242.
+ *
+ * @param string $data
+ * original argument passed to a resource method callback
+ * @param mixed $fields
+ * name of the field(s) where arguments should be checked for, either as a string or as an array of strings
+ * @return array
+ */
+
+// Adds backwards compatability with regression fixed in #1083242
+function _services_access_value($data, $fields) {
+
+ if (!is_array($fields)) {
+ $fields = array($fields);
+ }
+
+ foreach ($fields as $field) {
+ if (is_array($data) && isset($data[$field]) && count($data) == 1) {
+ return $data[$field];
+ }
+ }
+ return $data;
+}
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment