Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- Yii::import('application.components.AMQP.CAMQP');
- /**
- * AMQP Manager class
- * @package App.component
- */
- class AmqpManager extends AbstractManager
- {
- public $host = 'localhost';
- public $port = '5672';
- public $vhost = '/';
- public $login = 'guest';
- public $password = 'guest';
- protected $client = null;
- public $exchangeName;
- private $_exchange = null;
- public $isFakeMode = false;
- /**
- * Initializes exchange object
- *
- * @return void
- */
- public function init()
- {
- Yii::trace('Initializating AMQP', 'system.amqp.init');
- parent::init();
- if (!$this->isFakeMode) {
- $this->client = new AMQPConnection(
- array(
- 'host' => $this->host,
- 'vhost' => $this->vhost,
- 'port' => $this->port,
- 'login' => $this->login,
- 'password' => $this->password,
- )
- );
- try {
- $this->client->connect();
- } catch (Exception $e) {
- $this->error("Failed to connect to AMQP server (switching to fake mode): " . $e->getMessage());
- $this->isFakeMode = true;
- }
- }
- $this->getExchange();
- }
- /**
- * Returns an exchange instance
- *
- * @return AMQPExchange
- */
- public function getExchange()
- {
- if ($this->_exchange == null) {
- $class = !$this->isFakeMode ? 'AMQPExchange' : 'FakeAMQPExchange';
- $this->_exchange = new $class($this->client);
- $this->_exchange->declare($this->exchangeName, AMQP_EX_TYPE_TOPIC, AMQP_DURABLE);
- }
- return $this->_exchange;
- }
- /**
- * Declares a new Queue on the broker and binds it to the exchange
- *
- * @param string $name queue name
- * @param integer $flags queue flags
- * @param string|array $routingKeys queue routing keys
- *
- * @return AMQPQueue
- */
- public function createQueue($name, $flags = null, $routingKeys = "")
- {
- $class = !$this->isFakeMode ? 'AMQPQueue' : 'FakeAMQPQueue';
- $queue = new $class($this->client);
- $queue->declare($name, $flags);
- foreach ((array)$routingKeys as $routingKey) {
- $queue->bind($this->exchangeName, $routingKey);
- }
- return $queue;
- }
- /**
- * Retrieve client object instance
- *
- * @return AMQPConnection|null instance of object AMQPConnection or null
- */
- public function getClient()
- {
- return $this->client;
- }
- }
- /**
- * Fake AMQP Exchange class
- *
- * @package App.component
- */
- class FakeAMQPExchange
- {
- private static $_funct_list = array('publish');
- /**
- * Create an instance of FakeAMQPExchange
- *
- * @param null|AMQPConnect $connection connection
- * @param string $exchangeName exchange name
- */
- public function __construct ($connection, $exchangeName = "")
- {
- Yii::trace("FakeAMQPExchange: Warning: FAKE MODE", "system.amqp.FakeAMQPExchange");
- Yii::trace("FakeAMQPExchange is initiated for exchange '$exchangeName'", "system.amqp.FakeAMQPExchange");
- }
- /**
- * Magic call method
- *
- * @param string $name method name
- * @param array $arguments arguments
- *
- * @return boolean
- */
- public function __call($name, $arguments)
- {
- if (in_array($name, self::$_funct_list)) {
- Yii::trace("FakeAMQPExchange: Warning: FAKE MODE", "system.amqp.FakeAMQPExchange.$name");
- Yii::trace("Execute with params: " . print_r($arguments, 1) . "system.amqp.FakeAMQPExchange.$name");
- return true;
- }
- return false;
- }
- }
- /**
- * Fake AMQP Queue class
- */
- class FakeAMQPQueue
- {
- private static $_funct_list = array('ack', 'bind', 'cancel', 'consume', 'declare', 'delete', 'get', 'purge', 'unbind', 'existingQueue');
- /**
- * Create an instance of an FakeAMQPQueue object.
- *
- * @param null|AMQPConnect $AMQPConnection connection
- * @param string $queueName exchange name
- */
- public function __construct ($AMQPConnection, $queueName = "")
- {
- Yii::trace("FakeAMQPQueue: Warning: FAKE MODE", "system.amqp.FakeAMQPQueue");
- Yii::trace("FakeAMQPQueue is initiated for queue '$queueName'", "system.amqp.FakeAMQPQueue");
- }
- /**
- * Magic call method
- *
- * @param string $name method name
- * @param array $arguments arguments
- *
- * @return boolean
- */
- public function __call($name, $arguments)
- {
- if (in_array($name, self::$_funct_list)) {
- Yii::trace("FakeAMQPQueue: Warning: FAKE MODE", "system.amqp.FakeAMQPQueue.$name");
- Yii::trace("Execute with params: " . print_r($arguments, 1) . "system.amqp.FakeAMQPQueue.$name");
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement