queue-interop/queue-spec
Interoperability specification for PHP queue implementations. Defines common interfaces and contracts so different queue libraries and transports can work together with minimal coupling, enabling portable producers/consumers and easier swapping of backends.
Installation Add the package via Composer:
composer require queue-interop/queue-spec
This package is primarily a specification (not an implementation), so no direct Laravel integration is needed. Instead, use it to validate or test your queue system.
Understand the Purpose
The queue-spec package defines interfaces and behaviors for queue systems (e.g., Queue, Context, Message). It’s useful for:
First Use Case: Testing a Custom Queue Driver
Suppose you’re building a custom queue driver for Laravel. Use queue-spec to test it:
use QueueInterop\Queue;
use QueueInterop\Message;
use QueueInterop\Context;
// Pseudocode: Validate your driver against the spec
$queue = new YourCustomQueueDriver(); // Your Laravel queue adapter
$context = new YourContext(); // Implement Context interface
assert($queue instanceof Queue);
assert($context instanceof Context);
While queue-spec doesn’t integrate directly with Laravel, you can use it to:
DatabaseQueue, RedisQueue) against the spec.namespace App\Queues;
use Illuminate\Queue\Queue as LaravelQueue;
use QueueInterop\Queue;
class SpecCompliantQueue implements Queue {
protected $laravelQueue;
public function __construct(LaravelQueue $queue) {
$this->laravelQueue = $queue;
}
public function createContext() {
return new SpecContext($this->laravelQueue);
}
// Delegate other methods to Laravel's queue...
}
Use the spec to define how messages are handled:
// Example: Using the spec to validate message processing
$message = new YourMessage(); // Implement QueueInterop\Message
$context = $queue->createContext();
$context->createConsumer()
->consume($message)
->acknowledge();
Extend Laravel’s ShouldQueue interface to comply with queue-spec:
use QueueInterop\Message;
class YourJob implements ShouldQueue {
public function toQueueInteropMessage(): Message {
return new YourJobMessage(
$this->handle(),
$this->data
);
}
}
Validate middleware behavior against the spec:
public function testMiddlewareCompliance() {
$queue = new YourQueueWithMiddleware();
$context = $queue->createContext();
$this->assertInstanceOf(
QueueInterop\Context::class,
$context,
"Middleware must not break Context compliance."
);
}
Not a Runtime Dependency
Laravel’s Queue System Already Works
Illuminate\Queue\Queue doesn’t natively implement QueueInterop\Queue, so you’ll need a wrapper.Message Serialization Mismatches
serialize() by default, which may not align with queue-spec expectations.QueueInterop\Message with Laravel-compatible serialization:
class LaravelJobMessage implements Message {
public function __construct(private array $data) {}
public function getBody() {
return serialize($this->data);
}
public static function fromBody(string $body) {
return new self(unserialize($body));
}
}
Check Interface Compliance
Use instanceof checks to verify implementations:
$this->assertInstanceOf(
QueueInterop\Queue::class,
$yourQueue,
"Your queue must implement QueueInterop\Queue."
);
Mock Context for Testing
Laravel’s Queue doesn’t provide a Context, so mock it for testing:
$mockContext = $this->createMock(Context::class);
$mockContext->method('createConsumer')->willReturn(new YourConsumer());
Handle Exceptions
The spec defines QueueException. Ensure your Laravel queue throws compatible exceptions:
try {
$queue->createContext();
} catch (QueueInterop\QueueException $e) {
// Handle spec-compliant errors
}
Custom Message Classes
Extend QueueInterop\Message to add Laravel-specific metadata (e.g., job ID, retry count):
class LaravelMessage implements Message {
public function getJobId(): ?string {
return $this->data['job_id'] ?? null;
}
}
Queue Adapter Wrapper Create a Laravel service provider to auto-wrap queues:
// config/queue.php
'spec_compliant' => [
'driver' => 'database',
'wrapper' => App\Queues\SpecCompliantQueue::class,
];
Testing Helpers Build a trait for Laravel tests:
trait QueueSpecTests {
protected function assertQueueCompliant($queue) {
$this->assertInstanceOf(Queue::class, $queue);
$this->assertInstanceOf(Context::class, $queue->createContext());
}
}
How can I help you explore Laravel packages today?