1. Go to this page and download the library: Download djereg/symfony-rabbitmq 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/ */
djereg / symfony-rabbitmq example snippets
use Djereg\Symfony\RabbitMQ\Event\MessagePublishEvent;
class UserCreated extends MessagePublishEvent
{
// Set the event name
protected string $event = 'user.created';
public function __construct(private User $user)
{
$this->user = $user;
}
// Create a payload method that returns the data to be sent
public function payload(): array
{
return [
'user_id' => $this->user->id,
];
}
}
use Djereg\Symfony\RabbitMQ\Service\EventDispatcher;
class UserService
{
public function __construct(
private EventDispatcher $dispatcher
) {
//
}
public function createUser(User $user): void
{
// Dispatch the event
$this->dispatcher->dispatch(new UserCreated($user));
}
}
use Djereg\Symfony\RabbitMQ\Attribute\AsMessageEventListener;
#[AsMessageEventListener('user.created')]
class NotifyUserListener
{
public function __invoke(MessageEvent $event): void
{
// Do something
}
}
use Djereg\Symfony\RabbitMQ\Message\EventMessage;
class UserCreatedMessage extends EventMessage {}
use Djereg\Symfony\RabbitMQ\Attribute\AsMessageEventListener;
use Djereg\Symfony\RabbitMQ\Listeners\MessageEventListener;
#[AsMessageEventListener('user.created')]
class UserCreatedListener extends MessageEventListener
{
protected string $message = UserCreatedMessage::class;
}
#[AsMessageHandler]
class UserCreatedMessageHandler
{
public function __invoke(UserCreatedMessage $message): void {
// Get the event name
$event = $message->getEvent();
// Get the event payload
$payload = $event->getPayload();
// Get the event wrapped in the message
$raw = $message->getMessageEvent();
}
}
use Djereg\Symfony\RabbitMQ\Contract\ClientInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
class UserService
{
public function __construct(
#[Autowire('users_client')]
private ClientInterface $client,
) {
//
}
public function getUser(int $id): User
{
// Call the remote procedure
$user = $this->client->call('get', ['id' => $id]);
// Return the user or do something else with it
return $user;
}
}
use Djereg\Symfony\RabbitMQ\Attribute\AsRemoteProcedure;
#[AsRemoteProcedure('get')]
class GetUser
{
public function __invoke(int $id): array
{
// Query the database and return the result
}
}
use Djereg\Symfony\RabbitMQ\Attribute\AsRemoteProcedure;
class UserService
{
#[AsRemoteProcedure('get')]
public function getUser(int $id): array
{
// Query the database and return the result
}
// When adding the attribute to a method, you can omit the name of the procedure.
// In this case, the name will be the same as the method name.
#[AsRemoteProcedure]
public function update(int $id, array $data): bool
{
// Update the user and return the result of the operation
}
}
use Djereg\Symfony\RabbitMQ\Event\MessagePublishingEvent;
use Djereg\Symfony\RabbitMQ\Event\MessageReceivedEvent;
use Djereg\Symfony\RabbitMQ\Event\MessageProcessingEvent;
use Djereg\Symfony\RabbitMQ\Event\MessageProcessedEvent;
bash
php bin/console rabbitmq:consume
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.