/wp-content/plugins/backwpup/sdk/Aws/Guzzle/Service/Description/ServiceDescription.php

https://bitbucket.org/cesarmedrano/cesarmedrano · PHP · 329 lines · 159 code · 47 blank · 123 comment · 11 complexity · fe74c54c6aeb8919144a1d31b97a17fa MD5 · raw file

  1. <?php
  2. namespace Guzzle\Service\Description;
  3. use Guzzle\Common\Exception\InvalidArgumentException;
  4. /**
  5. * A ServiceDescription stores service information based on a service document
  6. */
  7. class ServiceDescription implements ServiceDescriptionInterface
  8. {
  9. /**
  10. * @var array Array of {@see OperationInterface} objects
  11. */
  12. protected $operations = array();
  13. /**
  14. * @var array Array of API models
  15. */
  16. protected $models = array();
  17. /**
  18. * @var string Name of the API
  19. */
  20. protected $name;
  21. /**
  22. * @var string API version
  23. */
  24. protected $apiVersion;
  25. /**
  26. * @var string Summary of the API
  27. */
  28. protected $description;
  29. /**
  30. * @var array Any extra API data
  31. */
  32. protected $extraData = array();
  33. /**
  34. * @var ServiceDescriptionLoader Factory used in factory method
  35. */
  36. protected static $descriptionLoader;
  37. /**
  38. * @var string baseUrl/basePath
  39. */
  40. protected $baseUrl;
  41. /**
  42. * {@inheritdoc}
  43. * @param string|array $config File to build or array of operation information
  44. * @param array $options Service description factory options
  45. *
  46. * @return self
  47. */
  48. public static function factory($config, array $options = array())
  49. {
  50. // @codeCoverageIgnoreStart
  51. if (!self::$descriptionLoader) {
  52. self::$descriptionLoader = new ServiceDescriptionLoader();
  53. }
  54. // @codeCoverageIgnoreEnd
  55. return self::$descriptionLoader->load($config, $options);
  56. }
  57. /**
  58. * Create a new ServiceDescription
  59. *
  60. * @param array $config Array of configuration data
  61. */
  62. public function __construct(array $config = array())
  63. {
  64. $this->fromArray($config);
  65. }
  66. /**
  67. * Serialize the service description
  68. *
  69. * @return string
  70. */
  71. public function serialize()
  72. {
  73. $result = array(
  74. 'name' => $this->name,
  75. 'apiVersion' => $this->apiVersion,
  76. 'baseUrl' => $this->baseUrl,
  77. 'description' => $this->description
  78. ) + $this->extraData;
  79. $result['operations'] = array();
  80. foreach ($this->getOperations() as $name => $operation) {
  81. $result['operations'][$operation->getName() ?: $name] = $operation->toArray();
  82. }
  83. if (!empty($this->models)) {
  84. $result['models'] = array();
  85. foreach ($this->models as $id => $model) {
  86. $result['models'][$id] = $model instanceof Parameter ? $model->toArray(): $model;
  87. }
  88. }
  89. return json_encode(array_filter($result));
  90. }
  91. /**
  92. * Unserialize the service description
  93. *
  94. * @param string|array $json JSON data
  95. */
  96. public function unserialize($json)
  97. {
  98. $this->operations = array();
  99. $this->fromArray(json_decode($json, true));
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getBaseUrl()
  105. {
  106. return $this->baseUrl;
  107. }
  108. /**
  109. * Set the baseUrl of the description
  110. *
  111. * @param string $baseUrl Base URL of each operation
  112. *
  113. * @return self
  114. */
  115. public function setBaseUrl($baseUrl)
  116. {
  117. $this->baseUrl = $baseUrl;
  118. return $this;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getOperations()
  124. {
  125. foreach (array_keys($this->operations) as $name) {
  126. $this->getOperation($name);
  127. }
  128. return $this->operations;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function hasOperation($name)
  134. {
  135. return isset($this->operations[$name]);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getOperation($name)
  141. {
  142. // Lazily retrieve and build operations
  143. if (!isset($this->operations[$name])) {
  144. return null;
  145. }
  146. if (!($this->operations[$name] instanceof Operation)) {
  147. $this->operations[$name] = new Operation($this->operations[$name], $this);
  148. }
  149. return $this->operations[$name];
  150. }
  151. /**
  152. * Add a operation to the service description
  153. *
  154. * @param OperationInterface $operation Operation to add
  155. *
  156. * @return self
  157. */
  158. public function addOperation(OperationInterface $operation)
  159. {
  160. $this->operations[$operation->getName()] = $operation->setServiceDescription($this);
  161. return $this;
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function getModel($id)
  167. {
  168. if (!isset($this->models[$id])) {
  169. return null;
  170. }
  171. if (!($this->models[$id] instanceof Parameter)) {
  172. $this->models[$id] = new Parameter($this->models[$id], $this);
  173. }
  174. return $this->models[$id];
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function getModels()
  180. {
  181. // Ensure all models are converted into parameter objects
  182. foreach (array_keys($this->models) as $id) {
  183. $this->getModel($id);
  184. }
  185. return $this->models;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function hasModel($id)
  191. {
  192. return isset($this->models[$id]);
  193. }
  194. /**
  195. * Add a model to the service description
  196. *
  197. * @param Parameter $model Model to add
  198. *
  199. * @return self
  200. */
  201. public function addModel(Parameter $model)
  202. {
  203. $this->models[$model->getName()] = $model;
  204. return $this;
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function getApiVersion()
  210. {
  211. return $this->apiVersion;
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function getName()
  217. {
  218. return $this->name;
  219. }
  220. /**
  221. * {@inheritdoc}
  222. */
  223. public function getDescription()
  224. {
  225. return $this->description;
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function getData($key)
  231. {
  232. return isset($this->extraData[$key]) ? $this->extraData[$key] : null;
  233. }
  234. /**
  235. * {@inheritdoc}
  236. */
  237. public function setData($key, $value)
  238. {
  239. $this->extraData[$key] = $value;
  240. return $this;
  241. }
  242. /**
  243. * Initialize the state from an array
  244. *
  245. * @param array $config Configuration data
  246. * @throws InvalidArgumentException
  247. */
  248. protected function fromArray(array $config)
  249. {
  250. // Keep a list of default keys used in service descriptions that is later used to determine extra data keys
  251. $defaultKeys = array('name', 'models', 'apiVersion', 'baseUrl', 'description');
  252. // Pull in the default configuration values
  253. foreach ($defaultKeys as $key) {
  254. if (isset($config[$key])) {
  255. $this->{$key} = $config[$key];
  256. }
  257. }
  258. // Account for the Swagger name for Guzzle's baseUrl
  259. if (isset($config['basePath'])) {
  260. $this->baseUrl = $config['basePath'];
  261. }
  262. // Ensure that the models and operations properties are always arrays
  263. $this->models = (array) $this->models;
  264. $this->operations = (array) $this->operations;
  265. // We want to add operations differently than adding the other properties
  266. $defaultKeys[] = 'operations';
  267. // Create operations for each operation
  268. if (isset($config['operations'])) {
  269. foreach ($config['operations'] as $name => $operation) {
  270. if (!($operation instanceof Operation) && !is_array($operation)) {
  271. throw new InvalidArgumentException('Invalid operation in service description: '
  272. . gettype($operation));
  273. }
  274. $this->operations[$name] = $operation;
  275. }
  276. }
  277. // Get all of the additional properties of the service description and store them in a data array
  278. foreach (array_diff(array_keys($config), $defaultKeys) as $key) {
  279. $this->extraData[$key] = $config[$key];
  280. }
  281. }
  282. }