prooph/common
Shared utility classes for prooph components, providing common interfaces and infrastructure aligned with PHP-FIG standards. Note: this library is deprecated and support ended Dec 31, 2019; use only with compatible prooph versions (4.x for newer components).
Installation Add the package via Composer:
composer require prooph/common
No additional configuration is required—it’s a pure PHP library with no Laravel-specific setup.
First Use Case: Event Streaming
The package provides Prooph\Common\Event\EventStream for handling event sourcing. Initialize it in a Laravel service:
use Prooph\Common\Event\EventStream;
$stream = new EventStream('order_events', 'user-123');
$stream->append(new OrderCreatedEvent('order-456', '2023-01-01'));
Key Classes to Explore
EventStream: Core for event sourcing.Message: Base class for domain events/commands.EventStoreId: UUID handling (via ramsey/uuid).Metadata: Attach metadata to messages.Aggregate Root Design
Use EventStream to track state transitions:
class OrderAggregate {
private EventStream $stream;
public function __construct(EventStream $stream) {
$this->stream = $stream;
}
public function create(string $orderId): void {
$this->stream->append(new OrderCreatedEvent($orderId));
}
}
Replaying Events Reconstruct state by replaying events:
$stream = new EventStream('order_events', 'user-123');
$stream->loadFromHistory([/* array of serialized events */]);
Metadata Integration Attach context (e.g., timestamps, user IDs):
$event = new OrderCreatedEvent('order-456');
$event->setMetadata(['created_by' => 'admin']);
Prooph\Common\Message\Command for CQRS.Prooph\Common\Message\Query for read models.EventStream to the container:
$this->app->bind(EventStream::class, function () {
return new EventStream('events', 'user-' . auth()->id());
});
Event facade to trigger domain events:
event(new OrderCreatedEvent('order-456'));
UUID Handling
EventStoreId uses ramsey/uuid. Ensure your Laravel app has it installed:
composer require ramsey/uuid
use Prooph\Common\Event\EventStoreId;
$id = EventStoreId::fromString(Uuid::uuid4()->toString());
Immutable Events
Events are immutable. Modify metadata via setMetadata() before appending to the stream.
Serialization
Events must implement JsonSerializable or __toString(). Use Prooph\Common\Event\ActionEvent as a base for complex events.
dd($stream->getUncommittedEvents());
Custom Event Stores
Extend Prooph\Common\Event\EventStore for persistence (e.g., Doctrine, Eloquent).
Example:
class LaravelEventStore implements EventStore {
public function load($streamName, $streamIdentifier) {
// Fetch from DB
}
}
Event Subscribers
Use Laravel’s Events to listen to Prooph\Common\Event\EventPublished:
Event::listen(OrderCreatedEvent::class, function ($event) {
// Side effects (e.g., notifications)
});
Testing
Mock EventStream in tests:
$mockStream = Mockery::mock(EventStream::class);
$mockStream->shouldReceive('append')->once();
How can I help you explore Laravel packages today?