1. Go to this page and download the library: Download hnto/bick library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hnto / bick example snippets
try {
$bick = new Bick(new BickConnectionSetup([
'host' => 'localhost',
'port' => '5672',
'user' => 'guest',
'pass' => 'guest'
]));
$bick->setup([
'queues' => [
new QueueSetup('mailing'),
new QueueSetup('process-file')
],
'exchanges' => [
new ExchangeSetup('default')
],
'bindings' => [
new QueueBindExchangeSetup('mailing', 'default', 'mail'),
new QueueBindExchangeSetup('process-file', 'default', 'process-csv'),
new QueueBindExchangeSetup('process-file', 'default', 'process-image'),
new QueueBindExchangeSetup('process-file', 'default', 'process-pdf'),
]
]);
} catch (BickException $exception) {
}
try {
$publisher = $bick->publisher(MyPublisher::class);
//Set the persistence adapter
$publisher->setPersistenceAdapter($adapter);
//Or if you don't want to persist
$publisher->persistMessages(false);
//When using the default BickPublisher object
//you can set a set of available publishing options
//Available publishing options
$publisher->setPublishingOptions(
//With this option you provide a valid callback that will be
//executed upon an ACK from the broker when publishing a message.
//Due note: this does not mean that the message was successfully routed
//to a queue. Only that it was accepted by the broker.
'callback_ack' => function(AMQPMessage $msg),
//With this option you provide a valid callback that will be
//executed upon a NACK from the broker when publishing a message.
//A NACK happens in exceptional cases that a message was
//rejected by the broker.
'callback_nack' => function(AMQPMessage $msg),
//With this option you provide a valid callback that will be
//executed when the broker returns a message.
//This usually happens if the message was, for some reason,
//unroutable. This means that the message could not be routed
//to a certain queue. This callback receives more info that the two above.
//You receive a reply code, a reply text, the exchange the message
//was published to, the routing key used and the AMQPMessage itself.
callback_return => function($replyCode, $replyText, $exchange, $routingKey, AMQPMessage $msg),
);
$publisher->publish(new BickMessage([
'body' => ['headers' => [], 'subject' => 'test', 'body' => '<html><body>Test</body></html>'],
'meta' => ['mail' => '[email protected]'],
'exchange' => 'default',
'routingKey' => 'mail'
]));
} catch (BickException $exception) {
}
//MailingConsumer
class MyConsumer extends BickConsumer
{
/**
* Must contain the queue name
*
* @var string
*/
protected $queue = 'mailing';
protected $persist = false;
/**
* @inheritdoc
*/
public function process(BickMessageInterface $message): int
{
//Do something with the message
return BickMessage::MESSAGE_ACK;
}
}
//Consume messages
$consumer = $bick->consumer(MyConsumer::class);
//Set the persistence adapter (not requred if $persist is set to false)
$consumer->setPersistenceAdapter($adapter);
//Consume
$consumer->consume('my-queue');
$consumer->setTranslator(new MyTranslator());
class MyTranslator implements BickMessageTranslatorInterface
{
public function translate(AMQPMesage $msg): BickMessageInterface {
//Do your own thing with the messsage
//and return a valid BickMessageInterface object
}
}
//PersistenceAdapterInterface
...
public function persist(BickMessage $message): void;
...
public function update(BickMessage $message): void;
...
public function analyse(BickMessage $message, BickMessageFault $fault): void;
...
//BickPublisher
public function persist(BickMessage $message)
{
....
//Your adapter method "persist" will receive a BickMessage object
//persist is used to insert a new message into your datastorage
$this->getPersistenceAdapter()->persist($message);
}
//BickConsumer
public function persist(BickMessage $message): void
{
...
Your adapter method "update" will receive a BickMessage object
//update is used to update an existing message in your datastorage
$this->getPersistenceAdapter()->update($message);
}
}
//MessageFaultAnalyseInterface
...
public function analyse(BickMessage $message, BickMessageFault $fault): void;
//MailingConsumer
class MailingConsumer extends BickConsumer {
...
public function process(BickMessage $message): int
{
//Do something with the message
//Fault message (string|array), fault code (integer)
$this->fault = new BickMessageFault('fault message', 1);
return BickMessage::MESSAGE_NACK;
...
//Bick\Service\Storage\BickStorage
...
//This method is executed upon publishing a message
public function persist(BickMessage $message)
{
...
//Insert into batch
...
//Insert into message
...
//This method is executed upon consuming a queue
public function update(BickMessage $message)
{
//Update message
...
//Check if all messages of batch have been completed
...
//Update the batch
...
$dsn = 'mysql:host=host;dbname=name';
$username = 'username';
$password = 'password';
//Optional options
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
//Instantiate the adapter and give it a PDO object
$adapter = new \Bick\Service\Storage\BickStorage($dbh);
//Set the adapter in the publisher
$bick->publisher(BickPublisher::class)
->setPersistenceAdapter($adapter);
//Set the adapter in the consumer
$bick->consumer(MyConsumer::class)
->setPersistenceAdapter($adapter);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.