Connection.php 1.51 KB
<?php

/**
 * Created by PhpStorm.
 * User: Ziy
 * Date: 14/8/31
 * Time: 下午2:17
 */
class Q_Message_Amqp_Connection
{

    private $section = 'amqp';

    private $nodeName = 'message';

    private $credentials = array(
        'host' => '127.0.0.1',
        'vhost' => '/',
        'port' => 5672,
        'login' => 'guest',
        'password' => 'guest'
    );

    public function __construct()
    {
        $server = $this->getServers();
        $this->credentials['host'] = $server['host'];
        $this->credentials['port'] = $server['port'];
        $this->credentials['login'] = $server['login'];
        $this->credentials['password'] = $server['password'];
    }

    public function setVhost($vhost)
    {
        $this->credentials['vhost'] = $vhost;
        return $this;
    }

    public function setSection($section)
    {
        $this->section = strtolower($section);
        return $this;
    }

    public function setNodeName($nodeName)
    {
        $this->nodeName = $nodeName;
        return $this;
    }

    private function getServers()
    {
        $options = array(
            'mode' => Q_Server_Core::SERVER_MODE_PROXY,
            'select' => Q_Server_Core::SERVER_SELECT_MODE_RAND
        );
        return Q_Server::factory('message', $this->section, $this->nodeName, $options);
    }

    /**
     * @return AMQPConnection
     */
    public function connect()
    {
        $connection = new AMQPConnection($this->credentials);
        $connection->connect();
        return $connection;
    }
}