PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/google/service/Google_ServiceResource.php

https://gitlab.com/x33n/platform
PHP | 195 lines | 131 code | 23 blank | 41 comment | 35 complexity | 54857a6a062e71ca1eaec9ed100a1e40 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * Implements the actual methods/resources of the discovered Google API using magic function
  19. * calling overloading (__call()), which on call will see if the method name (plus.activities.list)
  20. * is available in this service, and if so construct an apiHttpRequest representing it.
  21. *
  22. * @author Chris Chabot <chabotc@google.com>
  23. * @author Chirag Shah <chirags@google.com>
  24. *
  25. */
  26. class Google_ServiceResource {
  27. // Valid query parameters that work, but don't appear in discovery.
  28. private $stackParameters = array(
  29. 'alt' => array('type' => 'string', 'location' => 'query'),
  30. 'boundary' => array('type' => 'string', 'location' => 'query'),
  31. 'fields' => array('type' => 'string', 'location' => 'query'),
  32. 'trace' => array('type' => 'string', 'location' => 'query'),
  33. 'userIp' => array('type' => 'string', 'location' => 'query'),
  34. 'userip' => array('type' => 'string', 'location' => 'query'),
  35. 'file' => array('type' => 'complex', 'location' => 'body'),
  36. 'data' => array('type' => 'string', 'location' => 'body'),
  37. 'mimeType' => array('type' => 'string', 'location' => 'header'),
  38. 'uploadType' => array('type' => 'string', 'location' => 'query'),
  39. 'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
  40. );
  41. /** @var Google_Service $service */
  42. private $service;
  43. /** @var string $serviceName */
  44. private $serviceName;
  45. /** @var string $resourceName */
  46. private $resourceName;
  47. /** @var array $methods */
  48. private $methods;
  49. public function __construct($service, $serviceName, $resourceName, $resource) {
  50. $this->service = $service;
  51. $this->serviceName = $serviceName;
  52. $this->resourceName = $resourceName;
  53. $this->methods = isset($resource['methods']) ? $resource['methods'] : array($resourceName => $resource);
  54. }
  55. /**
  56. * @param $name
  57. * @param $arguments
  58. * @return Google_HttpRequest|array
  59. * @throws Google_Exception
  60. */
  61. public function __call($name, $arguments) {
  62. if (! isset($this->methods[$name])) {
  63. throw new Google_Exception("Unknown function: {$this->serviceName}->{$this->resourceName}->{$name}()");
  64. }
  65. $method = $this->methods[$name];
  66. $parameters = $arguments[0];
  67. // postBody is a special case since it's not defined in the discovery document as parameter, but we abuse the param entry for storing it
  68. $postBody = null;
  69. if (isset($parameters['postBody'])) {
  70. if (is_object($parameters['postBody'])) {
  71. $this->stripNull($parameters['postBody']);
  72. }
  73. // Some APIs require the postBody to be set under the data key.
  74. if (is_array($parameters['postBody']) && 'latitude' == $this->serviceName) {
  75. if (!isset($parameters['postBody']['data'])) {
  76. $rawBody = $parameters['postBody'];
  77. unset($parameters['postBody']);
  78. $parameters['postBody']['data'] = $rawBody;
  79. }
  80. }
  81. $postBody = is_array($parameters['postBody']) || is_object($parameters['postBody'])
  82. ? json_encode($parameters['postBody'])
  83. : $parameters['postBody'];
  84. unset($parameters['postBody']);
  85. if (isset($parameters['optParams'])) {
  86. $optParams = $parameters['optParams'];
  87. unset($parameters['optParams']);
  88. $parameters = array_merge($parameters, $optParams);
  89. }
  90. }
  91. if (!isset($method['parameters'])) {
  92. $method['parameters'] = array();
  93. }
  94. $method['parameters'] = array_merge($method['parameters'], $this->stackParameters);
  95. foreach ($parameters as $key => $val) {
  96. if ($key != 'postBody' && ! isset($method['parameters'][$key])) {
  97. throw new Google_Exception("($name) unknown parameter: '$key'");
  98. }
  99. }
  100. if (isset($method['parameters'])) {
  101. foreach ($method['parameters'] as $paramName => $paramSpec) {
  102. if (isset($paramSpec['required']) && $paramSpec['required'] && ! isset($parameters[$paramName])) {
  103. throw new Google_Exception("($name) missing required param: '$paramName'");
  104. }
  105. if (isset($parameters[$paramName])) {
  106. $value = $parameters[$paramName];
  107. $parameters[$paramName] = $paramSpec;
  108. $parameters[$paramName]['value'] = $value;
  109. unset($parameters[$paramName]['required']);
  110. } else {
  111. unset($parameters[$paramName]);
  112. }
  113. }
  114. }
  115. // Discovery v1.0 puts the canonical method id under the 'id' field.
  116. if (! isset($method['id'])) {
  117. $method['id'] = $method['rpcMethod'];
  118. }
  119. // Discovery v1.0 puts the canonical path under the 'path' field.
  120. if (! isset($method['path'])) {
  121. $method['path'] = $method['restPath'];
  122. }
  123. $servicePath = $this->service->servicePath;
  124. // Process Media Request
  125. $contentType = false;
  126. if (isset($method['mediaUpload'])) {
  127. $media = Google_MediaFileUpload::process($postBody, $parameters);
  128. if ($media) {
  129. $contentType = isset($media['content-type']) ? $media['content-type']: null;
  130. $postBody = isset($media['postBody']) ? $media['postBody'] : null;
  131. $servicePath = $method['mediaUpload']['protocols']['simple']['path'];
  132. $method['path'] = '';
  133. }
  134. }
  135. $url = Google_REST::createRequestUri($servicePath, $method['path'], $parameters);
  136. $httpRequest = new Google_HttpRequest($url, $method['httpMethod'], null, $postBody);
  137. if ($postBody) {
  138. $contentTypeHeader = array();
  139. if (isset($contentType) && $contentType) {
  140. $contentTypeHeader['content-type'] = $contentType;
  141. } else {
  142. $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
  143. $contentTypeHeader['content-length'] = Google_Utils::getStrLen($postBody);
  144. }
  145. $httpRequest->setRequestHeaders($contentTypeHeader);
  146. }
  147. $httpRequest = Google_Client::$auth->sign($httpRequest);
  148. if (Google_Client::$useBatch) {
  149. return $httpRequest;
  150. }
  151. // Terminate immediatly if this is a resumable request.
  152. if (isset($parameters['uploadType']['value'])
  153. && 'resumable' == $parameters['uploadType']['value']) {
  154. return $httpRequest;
  155. }
  156. return Google_REST::execute($httpRequest);
  157. }
  158. public function useObjects() {
  159. return (isset($GLOBALS['googleApiConfig']['use_objects']) && $GLOBALS['googleApiConfig']['use_objects']);
  160. }
  161. protected function stripNull(&$o) {
  162. $o = (array) $o;
  163. foreach ($o as $k => $v) {
  164. if ($v === null || strstr($k, "\0*\0__")) {
  165. unset($o[$k]);
  166. }
  167. elseif (is_object($v) || is_array($v)) {
  168. $this->stripNull($o[$k]);
  169. }
  170. }
  171. }
  172. }