A powerful integration package that brings Laminas EventManager support to CmdBus, enabling event-driven architecture in your command bus applications.
- Features
- Requirements
- Installation
- Quick Start
- Documentation
- Configuration
- Usage Examples
- Testing
- Contributing
- License
- 🎯 Event-Driven Architecture: Seamlessly integrate Laminas EventManager with CmdBus
- ⚡ Pre/Post Command Events: Trigger events before and after command execution
- 🔧 Flexible Configuration: Easy setup with Laminas ServiceManager
- 🎨 Middleware Pipeline: Built-in middleware for event triggering
- 🧩 PSR-11 Compatible: Works with any PSR-11 container
- 📦 Zero Configuration: Works out of the box with sensible defaults
- 🔌 Extensible: Easy to customize and extend for your needs
- PHP 8.2, 8.3, 8.4, or 8.5
- php-cmd/cmd-bus ^0.3.0
- laminas/laminas-eventmanager ^3.13
- PSR-11 Container implementation
Install via Composer:
composer require php-cmd/laminas-eventsIf you're using Laminas Component Installer (automatically included with Mezzio and Laminas MVC applications), you'll be prompted to inject the configuration provider. Select the appropriate option when prompted.
If you're using Laminas MVC or Mezzio:
// config/config.php or config/modules.config.php
return [
'PhpCmd\\Event\\ConfigProvider',
// ... other config providers
];use PhpCmd\CmdBus\Command\NamedCommandInterface;
use PhpCmd\CmdBus\Command\NamedCommandTrait;
final class CreateUserCommand implements NamedCommandInterface
{
use NamedCommandTrait;
public function __construct(
public readonly string $email,
public readonly string $name
) {}
}use PhpCmd\CmdBus\CommandHandlerInterface;
use PhpCmd\CmdBus\CommandInterface;
final class CreateUserHandler implements CommandHandlerInterface
{
public function handle(CommandInterface $command): mixed
{
// Your command handling logic
return $user;
}
}use Laminas\EventManager\AbstractListenerAggregate;
use Laminas\EventManager\EventManagerInterface;
use PhpCmd\Event\PreHandleEvent;
use PhpCmd\Event\PostHandleEvent;
final class UserEventListener extends AbstractListenerAggregate
{
public function attach(EventManagerInterface $events, $priority = 1): void
{
$this->listeners[] = $events->attach(
PreHandleEvent::NAME,
[$this, 'onPreHandle'],
$priority
);
$this->listeners[] = $events->attach(
PostHandleEvent::NAME,
[$this, 'onPostHandle'],
$priority
);
}
public function onPreHandle(PreHandleEvent $event): void
{
$command = $event->getTarget();
// Log, validate, or prepare before command execution
}
public function onPostHandle(PostHandleEvent $event): void
{
$result = $event->getTarget();
// Log, notify, or process after command execution
}
}// config/autoload/listeners.global.php
return [
'listeners' => [
[
'listener' => UserEventListener::class,
'priority' => 100,
],
],
];$cmdBus = $container->get(CmdBusInterface::class);
$result = $cmdBus->handle(new CreateUserCommand('[email protected]', 'John Doe'));Comprehensive documentation is available in the docs directory:
- Getting Started - Installation and basic setup
- Configuration Guide - Detailed configuration options
- Events Reference - Pre and post-handle events explained
- Middleware Guide - Understanding the middleware pipeline
- Listener Configuration - Setting up event listeners
- Best Practices - Recommended patterns and practices
- Advanced Usage - Advanced patterns and techniques
- API Reference - Complete API documentation
- Examples - Real-world usage examples
- Troubleshooting - Common issues and solutions
The package provides a ConfigProvider that registers all necessary services and middleware:
use PhpCmd\Event\ConfigProvider;
$config = (new ConfigProvider())();The package automatically:
- Registers EventManager and SharedEventManager
- Configures Pre/Post handle middleware
- Sets up delegator factories for dependency injection
- Provides sensible priority defaults
For detailed configuration options, see the Configuration Guide.
public function onPreHandle(PreHandleEvent $event): void
{
$command = $event->getTarget();
$this->logger->info('Executing command: ' . $command->getName());
}
public function onPostHandle(PostHandleEvent $event): void
{
$this->logger->info('Command executed successfully');
}public function onPreHandle(PreHandleEvent $event): void
{
$command = $event->getTarget();
if (!$this->validator->isValid($command)) {
throw new ValidationException($this->validator->getMessages());
}
}public function onPostHandle(PostHandleEvent $event): void
{
$result = $event->getTarget();
$this->broadcaster->broadcast('user.created', $result);
}For more examples, see the Examples Directory.
Run the test suite:
# Unit tests
composer test
# Integration tests
composer test-integration
# All checks (CS, static analysis, tests)
composer check
# Code coverage
composer test-coverageContributions are welcome! Please see CONTRIBUTING.md for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the BSD-3-Clause License - see the LICENSE file for details.
- Issues: GitHub Issues
- Source: GitHub Repository
- Documentation: docs
Built with ❤️ by the PHP-CMD team.