PageRenderTime 47ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Queue/Adapter/Array.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 355 lines | 156 code | 38 blank | 161 comment | 18 complexity | 68ddef248bf1ff6caf3a80ed26919235 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Array.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Queue_Adapter_AdapterAbstract
  24. */
  25. require_once 'Zend/Queue/Adapter/AdapterAbstract.php';
  26. /**
  27. * Class for using a standard PHP array as a queue
  28. *
  29. * @category Zend
  30. * @package Zend_Queue
  31. * @subpackage Adapter
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Queue_Adapter_Array extends Zend_Queue_Adapter_AdapterAbstract
  36. {
  37. /**
  38. * @var array
  39. */
  40. protected $_data = array();
  41. /**
  42. * Constructor
  43. *
  44. * @param array|Zend_Config $options
  45. * @param Zend_Queue|null $queue
  46. * @return void
  47. */
  48. public function __construct($options, Zend_Queue $queue = null)
  49. {
  50. parent::__construct($options, $queue);
  51. }
  52. /********************************************************************
  53. * Queue management functions
  54. *********************************************************************/
  55. /**
  56. * Does a queue already exist?
  57. *
  58. * Throws an exception if the adapter cannot determine if a queue exists.
  59. * use isSupported('isExists') to determine if an adapter can test for
  60. * queue existance.
  61. *
  62. * @param string $name
  63. * @return boolean
  64. */
  65. public function isExists($name)
  66. {
  67. return array_key_exists($name, $this->_data);
  68. }
  69. /**
  70. * Create a new queue
  71. *
  72. * Visibility timeout is how long a message is left in the queue "invisible"
  73. * to other readers. If the message is acknowleged (deleted) before the
  74. * timeout, then the message is deleted. However, if the timeout expires
  75. * then the message will be made available to other queue readers.
  76. *
  77. * @param string $name queue name
  78. * @param integer $timeout default visibility timeout
  79. * @return boolean
  80. */
  81. public function create($name, $timeout=null)
  82. {
  83. if ($this->isExists($name)) {
  84. return false;
  85. }
  86. if ($timeout === null) {
  87. $timeout = self::CREATE_TIMEOUT_DEFAULT;
  88. }
  89. $this->_data[$name] = array();
  90. return true;
  91. }
  92. /**
  93. * Delete a queue and all of it's messages
  94. *
  95. * Returns false if the queue is not found, true if the queue exists
  96. *
  97. * @param string $name queue name
  98. * @return boolean
  99. */
  100. public function delete($name)
  101. {
  102. $found = isset($this->_data[$name]);
  103. if ($found) {
  104. unset($this->_data[$name]);
  105. }
  106. return $found;
  107. }
  108. /**
  109. * Get an array of all available queues
  110. *
  111. * Not all adapters support getQueues(), use isSupported('getQueues')
  112. * to determine if the adapter supports this feature.
  113. *
  114. * @return array
  115. */
  116. public function getQueues()
  117. {
  118. return array_keys($this->_data);
  119. }
  120. /**
  121. * Return the approximate number of messages in the queue
  122. *
  123. * @param Zend_Queue $queue
  124. * @return integer
  125. * @throws Zend_Queue_Exception
  126. */
  127. public function count(Zend_Queue $queue=null)
  128. {
  129. if ($queue === null) {
  130. $queue = $this->_queue;
  131. }
  132. if (!isset($this->_data[$queue->getName()])) {
  133. /**
  134. * @see Zend_Queue_Exception
  135. */
  136. require_once 'Zend/Queue/Exception.php';
  137. throw new Zend_Queue_Exception('Queue does not exist');
  138. }
  139. return count($this->_data[$queue->getName()]);
  140. }
  141. /********************************************************************
  142. * Messsage management functions
  143. *********************************************************************/
  144. /**
  145. * Send a message to the queue
  146. *
  147. * @param string $message Message to send to the active queue
  148. * @param Zend_Queue $queue
  149. * @return Zend_Queue_Message
  150. * @throws Zend_Queue_Exception
  151. */
  152. public function send($message, Zend_Queue $queue=null)
  153. {
  154. if ($queue === null) {
  155. $queue = $this->_queue;
  156. }
  157. if (!$this->isExists($queue->getName())) {
  158. require_once 'Zend/Queue/Exception.php';
  159. throw new Zend_Queue_Exception('Queue does not exist:' . $queue->getName());
  160. }
  161. // create the message
  162. $data = array(
  163. 'message_id' => md5(uniqid(rand(), true)),
  164. 'body' => $message,
  165. 'md5' => md5($message),
  166. 'handle' => null,
  167. 'created' => time(),
  168. 'queue_name' => $queue->getName(),
  169. );
  170. // add $data to the "queue"
  171. $this->_data[$queue->getName()][] = $data;
  172. $options = array(
  173. 'queue' => $queue,
  174. 'data' => $data,
  175. );
  176. $classname = $queue->getMessageClass();
  177. if (!class_exists($classname)) {
  178. require_once 'Zend/Loader.php';
  179. Zend_Loader::loadClass($classname);
  180. }
  181. return new $classname($options);
  182. }
  183. /**
  184. * Get messages in the queue
  185. *
  186. * @param integer $maxMessages Maximum number of messages to return
  187. * @param integer $timeout Visibility timeout for these messages
  188. * @param Zend_Queue $queue
  189. * @return Zend_Queue_Message_Iterator
  190. */
  191. public function receive($maxMessages = null, $timeout = null, Zend_Queue $queue = null)
  192. {
  193. if ($maxMessages === null) {
  194. $maxMessages = 1;
  195. }
  196. if ($timeout === null) {
  197. $timeout = self::RECEIVE_TIMEOUT_DEFAULT;
  198. }
  199. if ($queue === null) {
  200. $queue = $this->_queue;
  201. }
  202. $data = array();
  203. if ($maxMessages > 0) {
  204. $start_time = microtime(true);
  205. $count = 0;
  206. $temp = &$this->_data[$queue->getName()];
  207. foreach ($temp as $key=>&$msg) {
  208. // count check has to be first, as someone can ask for 0 messages
  209. // ZF-7650
  210. if ($count >= $maxMessages) {
  211. break;
  212. }
  213. if (($msg['handle'] === null)
  214. || ($msg['timeout'] + $timeout < $start_time)
  215. ) {
  216. $msg['handle'] = md5(uniqid(rand(), true));
  217. $msg['timeout'] = microtime(true);
  218. $data[] = $msg;
  219. $count++;
  220. }
  221. }
  222. }
  223. $options = array(
  224. 'queue' => $queue,
  225. 'data' => $data,
  226. 'messageClass' => $queue->getMessageClass(),
  227. );
  228. $classname = $queue->getMessageSetClass();
  229. if (!class_exists($classname)) {
  230. require_once 'Zend/Loader.php';
  231. Zend_Loader::loadClass($classname);
  232. }
  233. return new $classname($options);
  234. }
  235. /**
  236. * Delete a message from the queue
  237. *
  238. * Returns true if the message is deleted, false if the deletion is
  239. * unsuccessful.
  240. *
  241. * @param Zend_Queue_Message $message
  242. * @return boolean
  243. * @throws Zend_Queue_Exception
  244. */
  245. public function deleteMessage(Zend_Queue_Message $message)
  246. {
  247. // load the queue
  248. $queue = &$this->_data[$message->queue_name];
  249. foreach ($queue as $key => &$msg) {
  250. if ($msg['handle'] == $message->handle) {
  251. unset($queue[$key]);
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. /********************************************************************
  258. * Supporting functions
  259. *********************************************************************/
  260. /**
  261. * Return a list of queue capabilities functions
  262. *
  263. * $array['function name'] = true or false
  264. * true is supported, false is not supported.
  265. *
  266. * @param string $name
  267. * @return array
  268. */
  269. public function getCapabilities()
  270. {
  271. return array(
  272. 'create' => true,
  273. 'delete' => true,
  274. 'send' => true,
  275. 'receive' => true,
  276. 'deleteMessage' => true,
  277. 'getQueues' => true,
  278. 'count' => true,
  279. 'isExists' => true,
  280. );
  281. }
  282. /********************************************************************
  283. * Functions that are not part of the Zend_Queue_Adapter_Abstract
  284. *********************************************************************/
  285. /**
  286. * serialize
  287. */
  288. public function __sleep()
  289. {
  290. return array('_data');
  291. }
  292. /*
  293. * These functions are debug helpers.
  294. */
  295. /**
  296. * returns underlying _data array
  297. * $queue->getAdapter()->getData();
  298. *
  299. * @return $this;
  300. */
  301. public function getData()
  302. {
  303. return $this->_data;
  304. }
  305. /**
  306. * sets the underlying _data array
  307. * $queue->getAdapter()->setData($data);
  308. *
  309. * @param array $data
  310. * @return $this;
  311. */
  312. public function setData($data)
  313. {
  314. $this->_data = $data;
  315. return $this;
  316. }
  317. }