Download the PHP package zaw/attribute-driven-cqrs without Composer
On this page you can find all versions of the php package zaw/attribute-driven-cqrs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zaw/attribute-driven-cqrs
More information about zaw/attribute-driven-cqrs
Files in zaw/attribute-driven-cqrs
Package attribute-driven-cqrs
Short Description A PHP package that simplifies the implementation of the CQRS pattern using attributes, enabling cleaner and more efficient command/query handling.
License MIT
Informations about the package attribute-driven-cqrs
Attribute Driven CQRS
A simple PHP package that makes it easy to implement the CQRS (Command Query Responsibility Segregation) pattern using attributes. With this package, you can streamline command and query handling without a lot of boilerplate, making your code cleaner and more manageable.
Requirement
- PHP 8.1+
Installation
Examples
1. Commands
- A Command describes a single action. It won't execute it.
- Commands are pure PHP classes whose purpose is to only hold the values needed to execute an operation.
- Commands can be handled by a command handler. Each command must have a handler.
- The
HandleCommandWithattribute can be used to link a command to its handler. The handler class will receive the command as a parameter in itshandlemethod. Note: A command should not return a value. But the package provide a feature to access return values from handlers toMiddlewaresto handle edge cases.
Example:
2. Queries
- A query describe a data retrival. But it doens't perform it.
- Each query must have a handler. The
HandleQueryWithattribute can be used to link a query to its handler. - The handler class will receive the query as a parameter in its
handlemethod.
3. Handlers
Handlers are responsible for executing the logic associated with a command or query.
-
Handlers must implement the
handlemethod, which accepts the command or query as a parameter. This is where the actual logic related to the action (in the case of commands) or data retrieval (in the case of queries) is performed. - Dependency Injection Support: Handlers can now take advantage of dependency injection. This allows you to inject services, repositories, or other dependencies directly into the handler. The package uses PHP-DI to resolve handler dependencies automatically.
Example:
4. Middlewares
- Middlewares allow you to execute custom logic before/after a command or query handler is executed.
- Middlewares must implement the
MiddlewareInterface, which enforces the implementation of theprocessmethod. Theprocessmethod receives the command or query as the first parameter and the return value from thehandlemethod of the CommandHandler/QueryHandler as the second parameter (if applicable). - Dependency Injection Support: Middlewares can now take advantage of dependency injection.
Middlewares can be applied in two ways:
Local Middleware
Local middleware applies to specific commands or queries. You can attach middleware directly using the BeforeHandle and AfterHandle attributes. Local middleware can be used for purposes such as logging, or dispatching events after a command has been successfully handled.
- BeforeHandle: Runs before the handler is executed.
- AfterHandle: Runs after the handler has finished.
Example of adding local middleware:
Global Middleware
Global middleware applies to all commands or queries within a specific bus (CommandBus or QueryBus). These middlewares will automatically run before or after any command or query is handled. Global middleware can be used for purposes such as logging, setting default db connection for each bus, adding authentication for each bus.
To register global middlewares, you can use the registerBeforeHandle and registerAfterHandle methods:
Exceptions
The package includes several custom exceptions to help you catch common errors:
NoHandlersFoundException: No handler is found for the command/query.MultipleHandlersFoundException: Multiple handlers are found for the same command/query.HandlerNotFoundException: The specified handler class doesn’t exist.InvalidCommandHandlerException: The handler class doesn’t implement handle method.MiddlewareNotFoundException: A middleware class couldn’t be found.MiddlewareRegistrationClosedException: You tried to add global middleware after handling started.
Performance Considerations
This package uses PHP reflection to find handlers and middleware. But don’t worry — reflection data is cached, so even with a lot of commands and queries, performance remains smooth(unless we're running thousands of commands and queries within a single request-respones cycle).
License
This package is licensed under the MIT License.
Contributing
We welcome contributions! If you'd like to help improve this package, please open an issue or submit a pull request or contact me directly at [email protected]. Your feedback is invaluable in making this package better for everyone!