123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace backend\forms;
- use components\Curl;
- use components\Exception;
- use Yii;
- /**
- * Class QueueForm
- * @package backend\forms
- */
- class SubscribeForm extends BaseForm
- {
- /**
- * @var string 主题名字
- */
- public $topicName;
- /**
- * @var string 订阅名字
- */
- public $subscriptionName;
- /**
- * @var string 订阅的协议
- */
- public $protocol;
- /**
- * @var string 接收通知的 endpoint
- */
- public $endpoint;
- /**
- * @var string 订阅接收消息的过滤策略
- */
- public $bindingKey;
- public function rules()
- {
- return [
- //create_subscribe create_subscribe
- [['topicName','subscriptionName', 'endpoint'], 'required', 'message' => 1500, 'on' => ['create_subscribe', 'delete_subscribe']],
- ['topicName', 'string', 'message' => 1501, 'on' => ['create_subscribe', 'delete_subscribe']],
- ['topicName', 'filter', 'filter' => 'trim', 'on' => ['create_subscribe', 'delete_subscribe']],
- ];
- }
- /**
- * [创建队列]
- * @author: libingke
- * @return array
- * @throws Exception
- */
- public function Subscribe()
- {
- try {
- $connect = $this->getConnect();
- $channel = $connect->channel();
- list($subscriptionName,,) = $channel->queue_bind($this->endpoint, $this->topicName, is_null($this->bindingKey)?'*':$this->bindingKey);
- } catch (\Exception $e) {
- throw new Exception(1001, $e->getMessage());
- }
- return $data = [
- 'subscriptionName' => empty($subscriptionName)?$this->subscriptionName:$subscriptionName,
- 'bindingKey' => $this->topicName,
- 'endpoint' => $this->endpoint,
- ];
- }
- /**
- * [删除队列]
- * @author: libingke
- * @return array
- * @throws Exception
- */
- public function Unsubscribe()
- {
- try {
- $connect = $this->getConnect();
- $channel = $connect->channel();
- $channel->queue_unbind($this->endpoint, $this->topicName, false );
- list($subscriptionName,,) = $channel->queue_unbind($this->endpoint, $this->topicName, is_null($this->bindingKey)?'*':$this->bindingKey);
- return [
- 'name' => $this->endpoint,
- 'result' => '删除成功'
- ];
- } catch (\Exception $e) {
- throw new Exception(1001, $e->getMessage());
- }
- }
- /**
- * [获取消息列表]
- * @author: libingke
- */
- public function getSubscribeList()
- {
- $authStr = Yii::$app->Amqp->user . ':' . Yii::$app->Amqp->pass;
- $url = Yii::$app->Amqp->host . ':' . Yii::$app->Amqp->api_port . "/api/bindings";
- $curl = new Curl();
- $curl->setOption(CURLOPT_USERPWD, $authStr);
- $result = json_decode($curl->get($url), true);
- if ($curl->responseCode != 200)
- throw new Exception(1002);
- if ($curl->errorText)
- throw new Exception(1002, $curl->errorText);
- if (isset($result['error']) && is_string($result['error']))
- throw new Exception(1002, $result['error']);
- $rows = [];
- foreach ($result as $k => $v) {
- //destination exchange
- $name = $v['destination'];
- $rows[$name]['destination'] = $name;
- $rows[$name]['source'] = $v['source'];
- $rows[$name]['topicName'] = $v['source'];
- $rows[$name]['destination_type'] = $v['destination_type'];
- $rows[$name]['routing_key'] = $v['routing_key'];
- $rows[$name]['arguments'] = $v['arguments'];
- $rows[$name]['properties_key'] = $v['properties_key'];
- }
- unset($result);
- return ['count' => count($rows), 'rows' => $rows];
- }
- }
|