A PHP library providing async messaging interfaces for domain-driven design (DDD) applications.
This package offers a set of standardized interfaces for implementing message-based communication patterns in PHP applications. It provides abstractions for publishers, consumers, message handlers, and connections to message brokers, allowing you to build decoupled, event-driven architectures.
- PHP >= 8.2
Install via Composer:
composer require jardisport/messagingUnified interface combining both publisher and consumer functionality in one service.
public function publish(string $topic, string|object|array $message, array $options = []): bool;
public function consume(string $topic, MessageHandlerInterface $handler, array $options = []): void;
public function getPublisher(): MessagePublisherInterface;
public function getConsumer(): MessageConsumerInterface;Manages connections to message brokers.
public function connect(): void;
public function disconnect(): void;
public function isConnected(): bool;Low-level interface for publishing messages to topics, channels, or queues.
public function publish(string $topic, string $message, array $options = []): bool;High-level interface for application-specific message publishing.
public function publish(string|object|array $message): bool;Low-level interface for consuming messages from topics, channels, or queues.
public function consume(string $topic, callable $callback, array $options = []): void;
public function stop(): void;High-level interface for application-specific message consumption.
public function consume(MessageHandlerInterface $handler): void;Handles received messages with acknowledgment control.
public function handle(string|array $message, array $metadata): bool;The library provides specific exceptions for different error scenarios:
ConnectionException- Connection-related errorsPublishException- Publishing failuresConsumerException- Consumption errorsMessageException- Message handling errors
The unified service interface simplifies implementation by combining publisher and consumer:
use JardisPort\Messaging\MessagingServiceInterface;
use JardisPort\Messaging\MessageHandlerInterface;
class MyMessagingService implements MessagingServiceInterface {
public function publish(string $topic, string|object|array $message, array $options = []): bool {
// Publish message to your broker
return true;
}
public function consume(string $topic, MessageHandlerInterface $handler, array $options = []): void {
// Start consuming messages
}
public function getPublisher(): MessagePublisherInterface {
// Return publisher instance
}
public function getConsumer(): MessageConsumerInterface {
// Return consumer instance
}
}
// Usage
$service = new MyMessagingService();
$service->publish('orders', ['order_id' => 123, 'status' => 'created']);
$service->consume('orders', new MyMessageHandler());For more granular control, implement the interfaces separately:
use JardisPort\Messaging\PublisherInterface;
use JardisPort\Messaging\ConsumerInterface;
use JardisPort\Messaging\MessageHandlerInterface;
class MyPublisher implements PublisherInterface {
public function publish(string $topic, string $message, array $options = []): bool {
// Implementation for your message broker
return true;
}
}
class MyMessageHandler implements MessageHandlerInterface {
public function handle(string|array $message, array $metadata): bool {
// Process the message
// Return true to acknowledge, false to reject/requeue
return true;
}
}
class MyConsumer implements ConsumerInterface {
public function consume(string $topic, callable $callback, array $options = []): void {
// Implementation for your message broker
}
public function stop(): void {
// Stop consuming
}
}The project includes PHPStan and PHP_CodeSniffer for maintaining code quality:
# Run PHPStan
composer exec phpstan analyse
# Run PHP_CodeSniffer
composer exec phpcsA pre-commit hook is automatically installed via Composer to ensure code quality before commits.
MIT License - see LICENSE file for details.
- Issues: GitHub Issues
- Email: jardisCore@headgent.dev
Jardis Core Development - jardisCore@headgent.dev
messaging, interfaces, JardisPort, Headgent, DDD