Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. <?php
  2.  
  3. Yii::import('application.components.AMQP.CAMQP');
  4.  
  5. /**
  6. * AMQP Manager class
  7. * @package App.component
  8. */
  9. class AmqpManager extends AbstractManager
  10. {
  11.  
  12. public $host = 'localhost';
  13. public $port = '5672';
  14. public $vhost = '/';
  15. public $login = 'guest';
  16. public $password = 'guest';
  17. protected $client = null;
  18. public $exchangeName;
  19. private $_exchange = null;
  20.  
  21. public $isFakeMode = false;
  22.  
  23. /**
  24. * Initializes exchange object
  25. *
  26. * @return void
  27. */
  28. public function init()
  29. {
  30. Yii::trace('Initializating AMQP', 'system.amqp.init');
  31. parent::init();
  32.  
  33. if (!$this->isFakeMode) {
  34. $this->client = new AMQPConnection(
  35. array(
  36. 'host' => $this->host,
  37. 'vhost' => $this->vhost,
  38. 'port' => $this->port,
  39. 'login' => $this->login,
  40. 'password' => $this->password,
  41. )
  42. );
  43. try {
  44. $this->client->connect();
  45. } catch (Exception $e) {
  46. $this->error("Failed to connect to AMQP server (switching to fake mode): " . $e->getMessage());
  47. $this->isFakeMode = true;
  48. }
  49.  
  50. }
  51.  
  52. $this->getExchange();
  53. }
  54.  
  55. /**
  56. * Returns an exchange instance
  57. *
  58. * @return AMQPExchange
  59. */
  60. public function getExchange()
  61. {
  62. if ($this->_exchange == null) {
  63. $class = !$this->isFakeMode ? 'AMQPExchange' : 'FakeAMQPExchange';
  64. $this->_exchange = new $class($this->client);
  65. $this->_exchange->declare($this->exchangeName, AMQP_EX_TYPE_TOPIC, AMQP_DURABLE);
  66. }
  67. return $this->_exchange;
  68. }
  69.  
  70. /**
  71. * Declares a new Queue on the broker and binds it to the exchange
  72. *
  73. * @param string $name queue name
  74. * @param integer $flags queue flags
  75. * @param string|array $routingKeys queue routing keys
  76. *
  77. * @return AMQPQueue
  78. */
  79. public function createQueue($name, $flags = null, $routingKeys = "")
  80. {
  81. $class = !$this->isFakeMode ? 'AMQPQueue' : 'FakeAMQPQueue';
  82. $queue = new $class($this->client);
  83. $queue->declare($name, $flags);
  84.  
  85. foreach ((array)$routingKeys as $routingKey) {
  86. $queue->bind($this->exchangeName, $routingKey);
  87. }
  88.  
  89. return $queue;
  90. }
  91.  
  92. /**
  93. * Retrieve client object instance
  94. *
  95. * @return AMQPConnection|null instance of object AMQPConnection or null
  96. */
  97. public function getClient()
  98. {
  99. return $this->client;
  100. }
  101. }
  102.  
  103. /**
  104. * Fake AMQP Exchange class
  105. *
  106. * @package App.component
  107. */
  108. class FakeAMQPExchange
  109. {
  110. private static $_funct_list = array('publish');
  111.  
  112. /**
  113. * Create an instance of FakeAMQPExchange
  114. *
  115. * @param null|AMQPConnect $connection connection
  116. * @param string $exchangeName exchange name
  117. */
  118. public function __construct ($connection, $exchangeName = "")
  119. {
  120. Yii::trace("FakeAMQPExchange: Warning: FAKE MODE", "system.amqp.FakeAMQPExchange");
  121. Yii::trace("FakeAMQPExchange is initiated for exchange '$exchangeName'", "system.amqp.FakeAMQPExchange");
  122. }
  123.  
  124. /**
  125. * Magic call method
  126. *
  127. * @param string $name method name
  128. * @param array $arguments arguments
  129. *
  130. * @return boolean
  131. */
  132. public function __call($name, $arguments)
  133. {
  134. if (in_array($name, self::$_funct_list)) {
  135. Yii::trace("FakeAMQPExchange: Warning: FAKE MODE", "system.amqp.FakeAMQPExchange.$name");
  136. Yii::trace("Execute with params: " . print_r($arguments, 1) . "system.amqp.FakeAMQPExchange.$name");
  137. return true;
  138. }
  139.  
  140. return false;
  141. }
  142.  
  143. }
  144.  
  145. /**
  146. * Fake AMQP Queue class
  147. */
  148. class FakeAMQPQueue
  149. {
  150. private static $_funct_list = array('ack', 'bind', 'cancel', 'consume', 'declare', 'delete', 'get', 'purge', 'unbind', 'existingQueue');
  151.  
  152. /**
  153. * Create an instance of an FakeAMQPQueue object.
  154. *
  155. * @param null|AMQPConnect $AMQPConnection connection
  156. * @param string $queueName exchange name
  157. */
  158. public function __construct ($AMQPConnection, $queueName = "")
  159. {
  160. Yii::trace("FakeAMQPQueue: Warning: FAKE MODE", "system.amqp.FakeAMQPQueue");
  161. Yii::trace("FakeAMQPQueue is initiated for queue '$queueName'", "system.amqp.FakeAMQPQueue");
  162. }
  163.  
  164. /**
  165. * Magic call method
  166. *
  167. * @param string $name method name
  168. * @param array $arguments arguments
  169. *
  170. * @return boolean
  171. */
  172. public function __call($name, $arguments)
  173. {
  174. if (in_array($name, self::$_funct_list)) {
  175. Yii::trace("FakeAMQPQueue: Warning: FAKE MODE", "system.amqp.FakeAMQPQueue.$name");
  176. Yii::trace("Execute with params: " . print_r($arguments, 1) . "system.amqp.FakeAMQPQueue.$name");
  177. return true;
  178. }
  179. return false;
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement