New API ideas for RabbitMQ AMQP for PHP

    Recently published a previously developed PHP API for RabbitMQ “AMQP now for PHP”.

    During the discussion, it was proposed to make the PHP API a more object model,
    closer to the model proposed in the AMQP Protocol.

    The code will be a little more complicated, but the object model will be more beautiful.

    Before moving on to coding, I present a new API for discussion

    Class AMQPConnection

    opening a logical connection, including a channel connection.
    $ cnn = new APMQConection ([host = localhost], [port = 5672], [login = guest], [psw = guest], [vhost = /]);
    $ cnn-> getResult (); - true / false result of the connection

    AMQPExchange Exchange Class

    $ exchange = new AMQPExchange ($ cnn, [name], [parms]) - create an exchange, if the name is specified otherwise initialization of the class
    $ res = $ exchange-> declare ([name]) - declare the exchange
    $ res = $ exchange-> delete ([name]) - delete the exchange, if no name is specified - delete with the current name
    $ res = $ exchange-> publish (msg, routing_key, [parms])
    $ res = $ exchange-> getResult (); - true / false the result of the last operation

    $ exchange = new AMQPExchange ($ cnn, name, [parms]) is equivalent to
    $ exchange = new AMQPExchange ($ cnn);
    $ res = $ exchange-> declare ();

    AMQPQueue Queue Class

    $ queue = new AMQPQueue (cnn, [name], [params]); - create a queue, if the name is specified otherwise initialization of the class
    $ res = $ queue-> delete ([name]) - delete the queue, returns the result of the operation
    $ res = $ queue-> declaere ([name], [params]) - declaration queue, returns the result of the operation.
    $ res = $ queue-> purge ([name]) - removal of all elements of the queue, returns the result of the operation
    $ res = $ queue-> bind (exchange, routing_key, [parms]); -connection of the queue and exchange
    $ queueItem = $ queue-> getItem (); - get one element from the queue
    $ arrayOfQueueItems = $ queue-> consume ([n]) - get an array of n-messages from the queue (all others are reset) if n is not set - all messages are selected
    $ queue-> getResult (); - true / false the result of the last operation, mainly intended to use the operation check in the constructor

    $ queue = new AMQPQueue (cnn, name, [params]); equivalent:
    $ queue = new AMQPQueue (cnn)
    $ queue-> declaere (name, [params])

    Although the object model does not quite comply with the AMQP standard, there are already four instead of one monolithic class. In practice, the functionality has not changed, only methods for removing the queue and exchange have been added.

    Also popular now: