PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

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

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