Description.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle;
  3. use GuzzleHttp\Psr7\Uri;
  4. /**
  5. * Represents a Guzzle service description
  6. */
  7. class Description implements DescriptionInterface
  8. {
  9. /** @var array Array of {@see OperationInterface} objects */
  10. private $operations = [];
  11. /** @var array Array of API models */
  12. private $models = [];
  13. /** @var string Name of the API */
  14. private $name;
  15. /** @var string API version */
  16. private $apiVersion;
  17. /** @var string Summary of the API */
  18. private $description;
  19. /** @var array Any extra API data */
  20. private $extraData = [];
  21. /** @var Uri baseUri/basePath */
  22. private $baseUri;
  23. /** @var SchemaFormatter */
  24. private $formatter;
  25. /**
  26. * @param array $config Service description data
  27. * @param array $options Custom options to apply to the description
  28. * - formatter: Can provide a custom SchemaFormatter class
  29. *
  30. * @throws \InvalidArgumentException
  31. */
  32. public function __construct(array $config, array $options = [])
  33. {
  34. // Keep a list of default keys used in service descriptions that is
  35. // later used to determine extra data keys.
  36. static $defaultKeys = ['name', 'models', 'apiVersion', 'description'];
  37. // Pull in the default configuration values
  38. foreach ($defaultKeys as $key) {
  39. if (isset($config[$key])) {
  40. $this->{$key} = $config[$key];
  41. }
  42. }
  43. // Set the baseUri
  44. // Account for the old style of using baseUrl
  45. if (isset($config['baseUrl'])) {
  46. $config['baseUri'] = $config['baseUrl'];
  47. }
  48. $this->baseUri = isset($config['baseUri']) ? new Uri($config['baseUri']) : new Uri();
  49. // Ensure that the models and operations properties are always arrays
  50. $this->models = (array) $this->models;
  51. $this->operations = (array) $this->operations;
  52. // We want to add operations differently than adding the other properties
  53. $defaultKeys[] = 'operations';
  54. // Create operations for each operation
  55. if (isset($config['operations'])) {
  56. foreach ($config['operations'] as $name => $operation) {
  57. if (!is_array($operation)) {
  58. throw new \InvalidArgumentException('Operations must be arrays');
  59. }
  60. $this->operations[$name] = $operation;
  61. }
  62. }
  63. // Get all of the additional properties of the service description and
  64. // store them in a data array
  65. foreach (array_diff(array_keys($config), $defaultKeys) as $key) {
  66. $this->extraData[$key] = $config[$key];
  67. }
  68. // Configure the schema formatter
  69. if (isset($options['formatter'])) {
  70. $this->formatter = $options['formatter'];
  71. } else {
  72. static $defaultFormatter;
  73. if (!$defaultFormatter) {
  74. $defaultFormatter = new SchemaFormatter();
  75. }
  76. $this->formatter = $defaultFormatter;
  77. }
  78. }
  79. /**
  80. * Get the basePath/baseUri of the description
  81. *
  82. * @return Uri
  83. */
  84. public function getBaseUri()
  85. {
  86. return $this->baseUri;
  87. }
  88. /**
  89. * Get the API operations of the service
  90. *
  91. * @return Operation[] Returns an array of {@see Operation} objects
  92. */
  93. public function getOperations()
  94. {
  95. return $this->operations;
  96. }
  97. /**
  98. * Check if the service has an operation by name
  99. *
  100. * @param string $name Name of the operation to check
  101. *
  102. * @return bool
  103. */
  104. public function hasOperation($name)
  105. {
  106. return isset($this->operations[$name]);
  107. }
  108. /**
  109. * Get an API operation by name
  110. *
  111. * @param string $name Name of the command
  112. *
  113. * @return Operation
  114. * @throws \InvalidArgumentException if the operation is not found
  115. */
  116. public function getOperation($name)
  117. {
  118. if (!$this->hasOperation($name)) {
  119. throw new \InvalidArgumentException("No operation found named $name");
  120. }
  121. // Lazily create operations as they are retrieved
  122. if (!($this->operations[$name] instanceof Operation)) {
  123. $this->operations[$name]['name'] = $name;
  124. $this->operations[$name] = new Operation($this->operations[$name], $this);
  125. }
  126. return $this->operations[$name];
  127. }
  128. /**
  129. * Get a shared definition structure.
  130. *
  131. * @param string $id ID/name of the model to retrieve
  132. *
  133. * @return Parameter
  134. * @throws \InvalidArgumentException if the model is not found
  135. */
  136. public function getModel($id)
  137. {
  138. if (!$this->hasModel($id)) {
  139. throw new \InvalidArgumentException("No model found named $id");
  140. }
  141. // Lazily create models as they are retrieved
  142. if (!($this->models[$id] instanceof Parameter)) {
  143. $this->models[$id] = new Parameter(
  144. $this->models[$id],
  145. ['description' => $this]
  146. );
  147. }
  148. return $this->models[$id];
  149. }
  150. /**
  151. * Get all models of the service description.
  152. *
  153. * @return array
  154. */
  155. public function getModels()
  156. {
  157. $models = [];
  158. foreach ($this->models as $name => $model) {
  159. $models[$name] = $this->getModel($name);
  160. }
  161. return $models;
  162. }
  163. /**
  164. * Check if the service description has a model by name.
  165. *
  166. * @param string $id Name/ID of the model to check
  167. *
  168. * @return bool
  169. */
  170. public function hasModel($id)
  171. {
  172. return isset($this->models[$id]);
  173. }
  174. /**
  175. * Get the API version of the service
  176. *
  177. * @return string
  178. */
  179. public function getApiVersion()
  180. {
  181. return $this->apiVersion;
  182. }
  183. /**
  184. * Get the name of the API
  185. *
  186. * @return string
  187. */
  188. public function getName()
  189. {
  190. return $this->name;
  191. }
  192. /**
  193. * Get a summary of the purpose of the API
  194. *
  195. * @return string
  196. */
  197. public function getDescription()
  198. {
  199. return $this->description;
  200. }
  201. /**
  202. * Format a parameter using named formats.
  203. *
  204. * @param string $format Format to convert it to
  205. * @param mixed $input Input string
  206. *
  207. * @return mixed
  208. */
  209. public function format($format, $input)
  210. {
  211. return $this->formatter->format($format, $input);
  212. }
  213. /**
  214. * Get arbitrary data from the service description that is not part of the
  215. * Guzzle service description specification.
  216. *
  217. * @param string $key Data key to retrieve or null to retrieve all extra
  218. *
  219. * @return null|mixed
  220. */
  221. public function getData($key = null)
  222. {
  223. if ($key === null) {
  224. return $this->extraData;
  225. } elseif (isset($this->extraData[$key])) {
  226. return $this->extraData[$key];
  227. } else {
  228. return null;
  229. }
  230. }
  231. }