Download the PHP package anik/laravel-amqp without Composer
On this page you can find all versions of the php package anik/laravel-amqp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download anik/laravel-amqp
More information about anik/laravel-amqp
Files in anik/laravel-amqp
Package laravel-amqp
Short Description anik/amqp wrapper for Laravel-ish frameworks
License MIT
Informations about the package laravel-amqp
anik/laravel-amqp
anik/amqp wrapper for Laravel-ish frameworks.
Examples
Checkout the repository for example.
Documentation
Installation
To install the package, run
composer require anik/laravel-amqp
Laravel
The Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class
service provider should automatically get registered. If
not, then you can manually add the service provider in your config/app.php
providers array:
- Publish configuration with
php artisan vendor:publish --provider "Anik\Laravel\Amqp\Providers\AmqpServiceProvider"
command.
Lumen
-
Register
Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class
service provider in yourbootstrap/app.php
file. -
Copy configuration
amqp.php
in your config directory fromvendor/anik/laravel-amqp/src/config/amqp.php
. - Import your configuration using
$app->configure('amqp');
in yourbootstrap/app.php
.
Laravel Zero
-
Register
Anik\Laravel\Amqp\Providers\AmqpServiceProvider::class
service provider in yourconfig/app.php
providers array: - Copy configuration
amqp.php
in your config directory fromvendor/anik/laravel-amqp/src/config/amqp.php
.
Configuration
In your config/amqp.php
, you can define multiple connections and use them from your code by pointing the connection
name.
amqp.default
denoting the default connection. Will be used if no connection is specified when producing or consuming messages.amqp.connections.*.connection.class
denoting the underlying Amqp connection to be used. By default, it uses lazy connection. You can change it to any implementation ofPhpAmqpLib\Connection\AbstractConnection
.amqp.connections.*.connection.hosts
can have multiple host configuration. Each host config must containhost
,port
,user
,password
keys. It can also containvhost
which is optional. Lazy connections cannot have more than one host configuration otherwise it'll throw error.- You can also pass optional array of parameters through
amqp.connections.*.connection.options
when creating an instance ofamqp.connections.*.connection.class
internally. amqp.connections.*.message
holds the default properties of a message when publishing.amqp.connections.*.exchange
holds the default properties of your exchange when publishing & consuming.amqp.connections.*.queue
holds the default properties of your queue when consuming.amqp.connections.*.consumer
holds the default properties of consumer when consuming.amqp.connections.*.qos
holds the default properties of QoS when consuming.
Octane support
This package supports laravel octane by
default. To keep the AMQP connection alive,
you have to configure octane to warm
the connection, by adding 'amqp' to the warm array in octane configurations.
Usage
The followings work the same.
Note
In this documentation, it'll use FACADE afterwards. If you're using Lumen, then you can use other approaches. The package doesn't require enabling Facade.
Publishing messages
To publish messages,
$messages
Type:mixed
. Required. It can be a single message, or an array of messages of any scalar type or implementation ofAnik\Amqp\Producible
.$routingKey
Type:string
. Optional. Default:''
(empty string).$exchange
Type:null | Anik\Amqp\Exchanges\Exchange
. Optional. Default:null
.$options
Type:array
. Optional. Default:[]
.- Key
message
- Accepts:array
. Valid properties forPhpAmqpLib\Message\AMQPMessage
. - Key
exchange
- Accepts:array
. Refer toamqp.connections.*.exchange
. - Key
publish
- Accepts:array
. Refer toAnik\Amqp\Producer::publishBatch
- Key
Note
- If any of the
$messages
is not an implementation ofAnik\Amqp\Producible
, then that message will be converted toAnik\Amqp\Producible
usingAnik\Amqp\ProducibleMessage
. - When converting to
Anik\Amqp\Producible
, it'll try to use$options['message']
as the message property. If not set, it'll then try to useamqp.connections.*.message
properties if available. - If
$exchange
is set tonull
, it'll check if$options['exchange']
is set or not. If not set, it'll then useamqp.connections.*.exchange
properties if available. - If
$options['publish']
is not set, it'll try to useamqp.connections.*.publish
properties if available.
Consuming messages
To consume messages,
$handler
Type:callable | Anik\Amqp\Consumable
. Required.$bindingKey
Type:string
. Optional. Default:''
(empty string).$exchange
Type:null | Anik\Amqp\Exchanges\Exchange
. Optional. Default:null
.$queue
Type:null | Anik\Amqp\Queues\Queue
. Optional. Default:null
.$qos
Type:null | Anik\Amqp\Qos\Qos
. Optional. Default:null
.$options
Type:array
. Optional. Default:[]
.- Key
exchange
- Accepts:array
. Refer toamqp.connections.*.exchange
. - Key
queue
- Accepts:array
. Refer toamqp.connections.*.queue
. - Key
qos
- Accepts:array
. Refer toamqp.connections.*.qos
. - Key
consumer
- Accepts:array
. Refer toamqp.connections.*.consumer
. - Key
bind
- Accepts:array
. Refer toAnik\Amqp\Consumer::consume
- Key
Note
- If
$handler
is not an implementation ofAnik\Amqp\Consumable
, then the handler will be converted toAnik\Amqp\Consumable
usingAnik\Amqp\ConsumableMessage
. - If
$exchange
is set tonull
, it'll check if$options['exchange']
is set or not. If not set, it'll then useamqp.connections.*.exchange
properties if available. - If
$queue
is set tonull
, it'll check if$options['queue']
is set or not. If not set, it'll then useamqp.connections.*.queue
properties if available. - If
$qos
is set tonull
, it'll check if$options['qos']
is set or not. If not set, it'll then useamqp.connections.*.qos
properties ifamqp.connections.*.qos.enabled
is set to a truthy value. - If
$options['bind']
is not set, it'll useamqp.connections.*.bind
properties if available. - If
$options['consumer']
is not set, it'll useamqp.connections.*.consumer
properties if available.
Testing
The package allows asserting a few scenarios. Before you can run those assertions, you'll need use Amqp::fake()
.
Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnConnection(string $name)
- To check if at least one message was published on the connection$name
.Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnExchange(string $name)
- To check if at least one message was published on exchange$name
.Anik\Laravel\Amqp\Facades\Amqp::assertPublishedOnExchangeType(string $type)
- To check if at least one message was published on exchange type$type
.Anik\Laravel\Amqp\Facades\Amqp::assertPublishedWithRoutingKey(string $key)
- To check if at least one message was published with routing key$key
.Anik\Laravel\Amqp\Facades\Amqp::assertPublished($message = null)
- If
$message
isnull
, it will check if at least one message was published. - Otherwise, checks in the following order.
- If a message exactly matches the
$message
. - If a message exactly matches the
get_class($message)
. - If a message is an implementation of
$message
.
- If a message exactly matches the
- If
Anik\Laravel\Amqp\Facades\Amqp::assertNotPublished($message = null)
- If
$message
isnull
, it will check if no message was published. - Otherwise, checks in the following order.
- No message was published that exactly matches the
$message
. - No message was published that exactly matches the
get_class($message)
. - No message was published that is an implementation of
$message
.
- No message was published that exactly matches the
- If
Anik\Laravel\Amqp\Facades\Amqp::assertPublishedCount(int $count, $message = null)
- If
$message
isnull
, it will check if exactly$count
messages have been published. - Otherwise, checks in the following order.
- If a message exactly matches the
$message
. - If a message exactly matches the
get_class($message)
. - If a message is an implementation of
$message
.
- If a message exactly matches the
- If
Note
Using Anik\Laravel\Amqp\Facades\Amqp::consume()
after Anik\Laravel\Amqp\Facades\Amqp::fake()
will throw exception.