Download the PHP package cmatosbc/clotho without Composer
On this page you can find all versions of the php package cmatosbc/clotho. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cmatosbc/clotho
More information about cmatosbc/clotho
Files in cmatosbc/clotho
Package clotho
Short Description A PHP event system with attribute-based event handling
License GPL-3.0-or-later
Informations about the package clotho
Clotho - PHP Event Attribute Library
Why Clotho?
In Greek mythology, Clotho (meaning "spinner") was one of the three Fates, or Moirai. She was responsible for spinning the thread of human life, determining when pivotal events would occur. Just as Clotho spun the threads that would become significant moments in one's life, this library helps you weave together the events that shape your application's behavior.
Overview
Clotho is a modern, attribute-based event system for PHP 8.1+. It provides a powerful and intuitive way to handle events in your application using PHP attributes. The library is PSR-14 compliant and offers features like:
- Declarative event handling using PHP attributes
- Support for both PSR-14 event objects and named events
- Priority-based event execution
- Wildcard event patterns
- Event groups
- Comprehensive error handling
Installation
Basic Usage
For more detailed examples and use cases, check out the examples/
folder in this repository.
1. Simple Event Handling
2. Priority-based Execution
3. Wildcard Event Patterns
4. Event Groups
5. Event Propagation Control
Event Objects and Payload
Clotho supports both event objects and named events with payloads. When using attributes, both types are dispatched:
Advanced Features
Event Objects
Clotho provides dedicated event objects for different scenarios:
Priority-based Event Handling
Events can be handled with different priorities, where higher priority listeners execute first:
Event Propagation Control
All event objects implement StoppableEventInterface
, allowing you to control event propagation:
Error Handling
Clotho provides a set of custom exceptions for handling error conditions in the event system. These exceptions are used only for truly exceptional cases, not for normal flow control.
Exception Types
Normal Flow Control
The following scenarios are handled as normal flow control, not exceptions:
Exception Hierarchy
Event Payload Structure
When using named events, Clotho dispatches an array with the following structure:
Before Method Events
After Method Events
Before Function Events
After Function Events
Example usage with payload:
Best Practices
-
Event Naming:
- Use lowercase, dot-separated names
- Start with module/context (e.g.,
user.
,order.
) - Use verbs for actions (e.g.,
create
,update
) - Use colons for groups (e.g.,
admin.group:create
)
-
Priority Levels:
- 20+ : Critical operations (validation, security)
- 10-19: Core business logic
- 0-9: Logging, notifications, non-critical operations
- Event Payload:
- Keep payloads serializable
- Include only necessary data
- Consider security implications
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Acknowledgments
- PSR-14 Event Dispatcher Interface
- PHP 8.1+ Attributes
- The PHP Community
Built with ❤️ by Carlos Artur Matos