PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Cloud/QueueService/Adapter/WindowsAzure.php

https://gitlab.com/blingbang2016/shop
PHP | 343 lines | 189 code | 23 blank | 131 comment | 23 complexity | b4762cebc1460850e73552d5a617ea6d MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage QueueService
  16. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. #require_once 'Zend/Cloud/QueueService/Adapter/AbstractAdapter.php';
  20. #require_once 'Zend/Cloud/QueueService/Exception.php';
  21. #require_once 'Zend/Service/WindowsAzure/Storage/Queue.php';
  22. /**
  23. * WindowsAzure adapter for simple queue service.
  24. *
  25. * @category Zend
  26. * @package Zend_Cloud
  27. * @subpackage QueueService
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Cloud_QueueService_Adapter_WindowsAzure
  32. extends Zend_Cloud_QueueService_Adapter_AbstractAdapter
  33. {
  34. /**
  35. * Option array keys for the Windows Azure adapter.
  36. */
  37. const ACCOUNT_NAME = 'storage_accountname';
  38. const ACCOUNT_KEY = 'storage_accountkey';
  39. const HOST = "storage_host";
  40. const PROXY_HOST = "storage_proxy_host";
  41. const PROXY_PORT = "storage_proxy_port";
  42. const PROXY_CREDENTIALS = "storage_proxy_credentials";
  43. /** list options */
  44. const LIST_PREFIX = 'prefix';
  45. const LIST_MAX_RESULTS = 'max_results';
  46. /** message options */
  47. const MESSAGE_TTL = 'ttl';
  48. const DEFAULT_HOST = Zend_Service_WindowsAzure_Storage::URL_CLOUD_QUEUE;
  49. /**
  50. * Storage client
  51. *
  52. * @var Zend_Service_WindowsAzure_Storage_Queue
  53. */
  54. protected $_storageClient = null;
  55. /**
  56. * Constructor
  57. *
  58. * @param array|Zend_Config $options
  59. * @return void
  60. */
  61. public function __construct($options = array())
  62. {
  63. if ($options instanceof Zend_Config) {
  64. $options = $options->toArray();
  65. }
  66. if (!is_array($options)) {
  67. throw new Zend_Cloud_QueueService_Exception('Invalid options provided');
  68. }
  69. if (isset($options[self::MESSAGE_CLASS])) {
  70. $this->setMessageClass($options[self::MESSAGE_CLASS]);
  71. }
  72. if (isset($options[self::MESSAGESET_CLASS])) {
  73. $this->setMessageSetClass($options[self::MESSAGESET_CLASS]);
  74. }
  75. // Build Zend_Service_WindowsAzure_Storage_Blob instance
  76. if (!isset($options[self::HOST])) {
  77. $host = self::DEFAULT_HOST;
  78. } else {
  79. $host = $options[self::HOST];
  80. }
  81. if (! isset($options[self::ACCOUNT_NAME])) {
  82. throw new Zend_Cloud_Storage_Exception('No Windows Azure account name provided.');
  83. }
  84. if (! isset($options[self::ACCOUNT_KEY])) {
  85. throw new Zend_Cloud_Storage_Exception('No Windows Azure account key provided.');
  86. }
  87. try {
  88. // TODO: support $usePathStyleUri and $retryPolicy
  89. $this->_storageClient = new Zend_Service_WindowsAzure_Storage_Queue(
  90. $host, $options[self::ACCOUNT_NAME], $options[self::ACCOUNT_KEY]);
  91. // Parse other options
  92. if (! empty($options[self::PROXY_HOST])) {
  93. $proxyHost = $options[self::PROXY_HOST];
  94. $proxyPort = isset($options[self::PROXY_PORT]) ? $options[self::PROXY_PORT] : 8080;
  95. $proxyCredentials = isset($options[self::PROXY_CREDENTIALS]) ? $options[self::PROXY_CREDENTIALS] : '';
  96. $this->_storageClient->setProxy(true, $proxyHost, $proxyPort, $proxyCredentials);
  97. }
  98. if (isset($options[self::HTTP_ADAPTER])) {
  99. $this->_storageClient->setHttpClientChannel($httpAdapter);
  100. }
  101. } catch(Zend_Service_WindowsAzure_Exception $e) {
  102. throw new Zend_Cloud_QueueService_Exception('Error on create: '.$e->getMessage(), $e->getCode(), $e);
  103. }
  104. }
  105. /**
  106. * Create a queue. Returns the ID of the created queue (typically the URL).
  107. * It may take some time to create the queue. Check your vendor's
  108. * documentation for details.
  109. *
  110. * @param string $name
  111. * @param array $options
  112. * @return string Queue ID (typically URL)
  113. */
  114. public function createQueue($name, $options = null)
  115. {
  116. try {
  117. $queue = $this->_storageClient->createQueue($name, $options);
  118. return $queue->Name;
  119. } catch (Zend_Service_WindowsAzure_Exception $e) {
  120. throw new Zend_Cloud_QueueService_Exception('Error on queue creation: '.$e->getMessage(), $e->getCode(), $e);
  121. }
  122. }
  123. /**
  124. * Delete a queue. All messages in the queue will also be deleted.
  125. *
  126. * @param string $queueId
  127. * @param array $options
  128. * @return boolean true if successful, false otherwise
  129. */
  130. public function deleteQueue($queueId, $options = null)
  131. {
  132. try {
  133. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  134. $queueId = $queueId->Name;
  135. }
  136. return $this->_storageClient->deleteQueue($queueId);
  137. } catch (Zend_Service_WindowsAzure_Exception $e) {
  138. throw new Zend_Cloud_QueueService_Exception('Error on queue deletion: '.$e->getMessage(), $e->getCode(), $e);
  139. }
  140. }
  141. /**
  142. * List all queues.
  143. *
  144. * @param array $options
  145. * @return array Queue IDs
  146. */
  147. public function listQueues($options = null)
  148. {
  149. $prefix = $maxResults = null;
  150. if (is_array($options)) {
  151. isset($options[self::LIST_PREFIX]) ? $prefix = $options[self::LIST_PREFIX] : null;
  152. isset($options[self::LIST_MAX_RESULTS]) ? $maxResults = $options[self::LIST_MAX_RESULTS] : null;
  153. }
  154. try {
  155. $queues = $this->_storageClient->listQueues($prefix, $maxResults);
  156. $result = array();
  157. foreach ($queues as $queue) {
  158. $result[] = $queue->Name;
  159. }
  160. return $result;
  161. } catch (Zend_Service_WindowsAzure_Exception $e) {
  162. throw new Zend_Cloud_QueueService_Exception('Error on listing queues: '.$e->getMessage(), $e->getCode(), $e);
  163. }
  164. }
  165. /**
  166. * Get a key/value array of metadata for the given queue.
  167. *
  168. * @param string $queueId
  169. * @param array $options
  170. * @return array
  171. */
  172. public function fetchQueueMetadata($queueId, $options = null)
  173. {
  174. try {
  175. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  176. $queueId = $queueId->Name;
  177. }
  178. return $this->_storageClient->getQueueMetadata($queueId);
  179. } catch (Zend_Service_WindowsAzure_Exception $e) {
  180. throw new Zend_Cloud_QueueService_Exception('Error on fetching queue metadata: '.$e->getMessage(), $e->getCode(), $e);
  181. }
  182. }
  183. /**
  184. * Store a key/value array of metadata for the specified queue.
  185. * WARNING: This operation overwrites any metadata that is located at
  186. * $destinationPath. Some adapters may not support this method.
  187. *
  188. * @param string $queueId
  189. * @param array $metadata
  190. * @param array $options
  191. * @return void
  192. */
  193. public function storeQueueMetadata($queueId, $metadata, $options = null)
  194. {
  195. try {
  196. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  197. $queueId = $queueId->Name;
  198. }
  199. return $this->_storageClient->setQueueMetadata($queueId, $metadata);
  200. } catch (Zend_Service_WindowsAzure_Exception $e) {
  201. throw new Zend_Cloud_QueueService_Exception('Error on setting queue metadata: '.$e->getMessage(), $e->getCode(), $e);
  202. }
  203. }
  204. /**
  205. * Send a message to the specified queue.
  206. *
  207. * @param string $queueId
  208. * @param string $message
  209. * @param array $options
  210. * @return string Message ID
  211. */
  212. public function sendMessage($queueId, $message, $options = null)
  213. {
  214. try {
  215. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  216. $queueId = $queueId->Name;
  217. }
  218. return $this->_storageClient->putMessage(
  219. $queueId, $message, $options[self::MESSAGE_TTL]
  220. );
  221. } catch (Zend_Service_WindowsAzure_Exception $e) {
  222. throw new Zend_Cloud_QueueService_Exception('Error on sending message: '.$e->getMessage(), $e->getCode(), $e);
  223. }
  224. }
  225. /**
  226. * Recieve at most $max messages from the specified queue and return the
  227. * message IDs for messages recieved.
  228. *
  229. * @param string $queueId
  230. * @param int $max
  231. * @param array $options
  232. * @return Zend_Cloud_QueueService_Message[]
  233. */
  234. public function receiveMessages($queueId, $max = 1, $options = null)
  235. {
  236. try {
  237. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  238. $queueId = $queueId->Name;
  239. }
  240. if (isset($options[self::VISIBILITY_TIMEOUT])) {
  241. $visibility = $options[self::VISIBILITY_TIMEOUT];
  242. } else {
  243. $visibility = self::DEFAULT_TIMEOUT;
  244. }
  245. return $this->_makeMessages($this->_storageClient->getMessages($queueId, $max, $visibility, false));
  246. } catch (Zend_Service_WindowsAzure_Exception $e) {
  247. throw new Zend_Cloud_QueueService_Exception('Error on recieving messages: '.$e->getMessage(), $e->getCode(), $e);
  248. }
  249. }
  250. /**
  251. * Create Zend_Cloud_QueueService_Message array for
  252. * Azure messages.
  253. *
  254. * @param array $messages
  255. * @return Zend_Cloud_QueueService_Message[]
  256. */
  257. protected function _makeMessages($messages)
  258. {
  259. $messageClass = $this->getMessageClass();
  260. $setClass = $this->getMessageSetClass();
  261. $result = array();
  262. foreach ($messages as $message) {
  263. $result[] = new $messageClass($message->MessageText, $message);
  264. }
  265. return new $setClass($result);
  266. }
  267. /**
  268. * Delete the specified message from the specified queue.
  269. *
  270. * @param string $queueId
  271. * @param Zend_Cloud_QueueService_Message $message Message ID or message
  272. * @param array $options
  273. * @return void
  274. */
  275. public function deleteMessage($queueId, $message, $options = null)
  276. {
  277. try {
  278. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  279. $queueId = $queueId->Name;
  280. }
  281. if ($message instanceof Zend_Cloud_QueueService_Message) {
  282. $message = $message->getMessage();
  283. }
  284. if ($message instanceof Zend_Service_WindowsAzure_Storage_QueueMessage) {
  285. return $this->_storageClient->deleteMessage($queueId, $message);
  286. } else {
  287. throw new Zend_Cloud_QueueService_Exception('Cannot delete the message: message object required');
  288. }
  289. } catch (Zend_Service_WindowsAzure_Exception $e) {
  290. throw new Zend_Cloud_QueueService_Exception('Error on deleting a message: '.$e->getMessage(), $e->getCode(), $e);
  291. }
  292. }
  293. /**
  294. * Peek at the messages from the specified queue without removing them.
  295. *
  296. * @param string $queueId
  297. * @param int $num How many messages
  298. * @param array $options
  299. * @return Zend_Cloud_QueueService_Message[]
  300. */
  301. public function peekMessages($queueId, $num = 1, $options = null)
  302. {
  303. try {
  304. if ($queueId instanceof Zend_Service_WindowsAzure_Storage_QueueInstance) {
  305. $queueId = $queueId->Name;
  306. }
  307. return $this->_makeMessages($this->_storageClient->peekMessages($queueId, $num));
  308. } catch (Zend_Service_WindowsAzure_Exception $e) {
  309. throw new Zend_Cloud_QueueService_Exception('Error on peeking messages: '.$e->getMessage(), $e->getCode(), $e);
  310. }
  311. }
  312. /**
  313. * Get Azure implementation
  314. * @return Zend_Service_Azure_Storage_Queue
  315. */
  316. public function getClient()
  317. {
  318. return $this->_storageClient;
  319. }
  320. }