QueueController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_Cloud
  17. * @subpackage examples
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Cloud
  24. * @subpackage examples
  25. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class QueueController extends Zend_Controller_Action
  29. {
  30. public $dependencies = array('config');
  31. /**
  32. * @var Zend_Cloud_QueueService_Adapter
  33. */
  34. protected $_queue = null;
  35. public function preDispatch()
  36. {
  37. $this->_queue = Zend_Cloud_QueueService_Factory::getAdapter($this->config->queue);
  38. }
  39. public function indexAction()
  40. {
  41. $this->view->qs = $this->_queue->listQueues();
  42. }
  43. public function createAction()
  44. {
  45. $request = $this->getRequest();
  46. if (!$request->isPost()) {
  47. return;
  48. }
  49. if (!$name = $this->_getParam('name', false)) {
  50. return;
  51. }
  52. $this->_queue->createQueue($name);
  53. return $this->_helper->redirector('index');
  54. }
  55. public function sendAction()
  56. {
  57. $this->view->qs = $this->_queue->listQueues();
  58. $request = $this->getRequest();
  59. $name = $this->view->name = $this->_getParam('name', false);
  60. if (!$name) {
  61. return;
  62. }
  63. if (!$request->isPost()) {
  64. return;
  65. }
  66. if (!$message = $this->_getParam('message', false)) {
  67. return;
  68. }
  69. $ret = $this->_queue->sendMessage($name, $message);
  70. return $this->_helper->redirector('index');
  71. }
  72. public function receiveAction()
  73. {
  74. $this->view->qs = $this->_queue->listQueues();
  75. $request = $this->getRequest();
  76. $name = $this->view->name = $this->_getParam('name', false);
  77. if (!$name) {
  78. return;
  79. }
  80. $messages = $this->_queue->receiveMessages($name);
  81. foreach ($messages as $msg) {
  82. $texts[] = $msg->getBody();
  83. // remove messages from the queue
  84. $this->_queue->deleteMessage($name, $msg);
  85. }
  86. $this->view->messages = $texts;
  87. }
  88. }