yf_wrapper_rabbitmq class

Rabbitmq API wrapper.

EXAMPLES: https://github.com/pdezwart/php-amqp/tree/master/tests
EXAMPLES: https://otokarev.com/2014/11/25/rabbitmq-primer-realizatsii-rpc-na-php/

Fileplugins/rabbitmq/classes/yf_wrapper_rabbitmq.class.php
Use_class('wrapper_rabbitmq')

Public properties

A site sets them in its config with $PROJECT_CONF['wrapper_rabbitmq']['NAME'] = ...;

PropertyDefaultDescription
$driver 'pecl'
$routing_key 'mykey'

Public methods 12

add

public function add($queue, $what)
    public function add($queue, $what)
    {
        if ( ! $queue || ! strlen($what)) {
            return false;
        }
        ! $this->_connection && $this->connect();

        if ($this->driver == 'pecl') {
            $ex = $this->init_exchange($queue);
            $ex->publish($what, $this->routing_key);
        } elseif ($this->driver == 'amqplib') {
            // TODO
        }
        return true;
    }

conf

public function conf($params = [])
    public function conf($params = [])
    {
        if ($this->driver == 'pecl' && ! extension_loaded('amqp')) {
            $this->driver = null;
        }
        if (is_array($params) && ! empty($params)) {
            isset($params['host']) && $this->host = $params['host'];
            isset($params['port']) && $this->port = $params['port'];
            isset($params['login']) && $this->login = $params['login'];
            isset($params['password']) && $this->password = $params['password'];
            isset($params['vhost']) && $this->vhost = $params['vhost'];
            isset($params['routing_key']) && $this->routing_key = $params['routing_key'];
        }
        return $this;
    }

connect

public function connect($params = [])
    public function connect($params = [])
    {
        if ($this->_connection) {
            return $this->_connection;
        }
        if ($params) {
            $this->conf($params);
        }
        if ($this->driver == 'pecl') {
            $this->_connection = new AMQPConnection([
                'host' => $this->host,
                'port' => $this->port,
                'login' => $this->login,
                'password' => $this->password,
                'vhost' => $this->vhost,
            ]);
            $this->_connection->connect();
        } elseif ($this->driver == 'amqplib') {
            require_php_lib('amqplib');
            $this->_connection = new \PhpAmqpLib\Connection\AMQPConnection(
                $this->host,
                $this->port,
                $this->login,
                $this->password,
                $this->vhost
            );
        }
        return $this->_connection;
    }

disconnect

public function disconnect()
    public function disconnect()
    {
        if ($this->driver == 'pecl') {
            unset($this->_connection);
        } elseif ($this->driver == 'amqplib') {
            $cnn = $this->_connection;
            $ch = $cnn->channel();
            $ch->close();
            return $cnn->close();
        }
    }

get

public function get($queue)
    public function get($queue)
    {
        if ( ! $queue) {
            return false;
        }
        ! $this->_connection && $this->connect();

        $msg = null;
        if ($this->driver == 'pecl') {
            $q = $this->init_queue($queue);
            $msg = $q->get(AMQP_AUTOACK);
        } elseif ($this->driver == 'amqplib') {
            // TODO
        }
        return $msg;
    }

init_channel

public function init_channel($params = [])
    public function init_channel($params = [])
    {
        if ( ! $this->_channel) {
            if ($this->driver == 'pecl') {
                $cnn = $this->connect($params);
                $this->_channel = new AMQPChannel($cnn);
            } elseif ($this->driver == 'amqplib') {
                $cnn = $this->connect($params);
                $this->_channel = $cnn->channel();
            }
        }
        return $this->_channel;
    }

init_exchange

public function init_exchange($name, $params = [])
    public function init_exchange($name, $params = [])
    {
        if ( ! $name) {
            return null;
        }
        if ( ! isset($this->_exchange[$name])) {
            if ($this->driver == 'pecl') {
                $ch = $this->init_channel($params);
                $ex = new AMQPExchange($ch);
                $ex->setName($name);
                $ex->setType(($params['exchange_type'] ?? null) ?: AMQP_EX_TYPE_FANOUT);
                $ex->declareExchange();
                $this->_exchange[$name] = $ex;
            } elseif ($this->driver == 'amqplib') {
                // TODO
            }
        }
        return $this->_exchange[$name];
    }

init_queue

public function init_queue($name, $params = [])
    public function init_queue($name, $params = [])
    {
        if ( ! $name) {
            return null;
        }
        if ( ! isset($this->_queue[$name])) {
            if ($this->driver == 'pecl') {
                $ch = $this->init_channel($params);
                $q = new AMQPQueue($ch);
                $q->setName($name);
                $q->setFlags(($params['queue_flags'] ?? null) ?: AMQP_DURABLE);
                $q->declareQueue();
                $this->_queue[$name] = $q;
            }
        }
        return $this->_queue[$name];
    }

is_ready

public function is_ready()
    public function is_ready()
    {
        ! $this->_connection && $this->connect();
        return (bool) $this->_connection;
    }

pub

public function pub($channel, $what)

Publish new event.

    public function pub($channel, $what)
    {
        if ( ! $channel || ! strlen($what)) {
            return false;
        }
        ! $this->_connection && $this->connect();

        if ($this->driver == 'pecl') {
            $ex = $this->init_exchange($channel);
            $ex->publish($what, $this->routing_key);
        } elseif ($this->driver == 'amqplib') {
            $ch = $this->_connection->channel();
            $ch->exchange_declare($channel, 'fanout', false, false, false);
            $msg = new \PhpAmqpLib\Message\AMQPMessage($what);
            $ch->basic_publish($msg, $channel);
        }
    }

sub

public function sub($channel, $callback)

Subscribe for one or more events.

    public function sub($channel, $callback)
    {
        if ( ! $channel || ! is_callable($callback)) {
            return false;
        }
        ! $this->_connection && $this->connect();

        if ($this->driver == 'pecl') {
            $q = $this->init_queue($channel);
            return $q->consume($callback, AMQP_AUTOACK);
        } elseif ($this->driver == 'amqplib') {
            $ch = $this->_connection->channel();
            $ch->exchange_declare($channel, $e_type = 'fanout', $e_passive = false, $e_durable = false, $e_auto_delete = false);
            list($queue_name) = $ch->queue_declare($q_name = '', $q_passive = false, $q_durable = false, $q_exclusive = true, $q_auto_delete = false, $q_nowait = false, $qparams = []);
            $ch->queue_bind($queue_name, $channel);
            $ch->basic_consume($queue_name, $c_tag = '', $c_no_local = false, $c_no_ack = true, $c_exclusive = false, $c_nowait = false, $callback);
            while (count((array) $ch->callbacks)) {
                $channel->wait();
            }
        }
    }

Methods starting with _ hooks and helpers; not routable as page actions

__clone

public function __clone()
    public function __clone()
    {
        $this->_connection = null;
    }