https://github.com/gianarb/fast-event-manager
PHP event manager based on regex. Easy to use and to extend
https://github.com/gianarb/fast-event-manager
Last synced: 6 months ago
JSON representation
PHP event manager based on regex. Easy to use and to extend
- Host: GitHub
- URL: https://github.com/gianarb/fast-event-manager
- Owner: gianarb
- Archived: true
- Created: 2015-10-27T18:52:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-02-01T13:57:38.000Z (over 10 years ago)
- Last Synced: 2025-05-19T20:12:01.333Z (about 1 year ago)
- Language: PHP
- Size: 12.7 KB
- Stars: 9
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# FastEventManager
[](https://scrutinizer-ci.com/g/gianarb/fast-event-manager/?branch=master)
[](https://scrutinizer-ci.com/g/gianarb/fast-event-manager/?branch=master)
[](https://travis-ci.org/gianarb/fast-event-manager)
PHP event manager based on regex. Trigger events and attach listeners, core feature
easy to understand and to extend.
## Install
```bash
$ composer install gianarb/fast-event-manager
```
## Usage
## Getting Started
This is the basic usage
```php
attach("post-save", function ($assertArg) {
// DO STUFF
});
$assert = false;
$eventManager->trigger("/post-save/", $assert);
```
## Priority
FastEventManager support priority listeners
```php
$eventManager = new EventManager();
$eventManager->attach("post-save", function ($assertArg) {
echo "Hi";
}, 100);
$eventManager->attach("post-save", function ($assertArg) {
echo " dev!";
}, 10);
$eventManager->trigger("/post-save/");
// output "Hi dev!"
```
## Regex
FastEventManager resolve regex, you can trigger more events.
```php
$eventManager = new EventManager();
$eventManager->attach("post-save", function ($assertArg) {
echo "Hi";
});
$eventManager->attach("pload", function ($assertArg) {
echo " none!";
});
$eventManager->attach("post-load", function ($assertArg) {
echo " dev!";
});
$eventManager->trigger("/post-(save|load)/i", $assert);
// output "Hi dev!"
```
## Stop Propagation
At the moment we decided to don't support this feature into the core of FastEventManager because
there are a lot of implementation around this feature. This is an example
```php
$eventManager = new EventManager();
$count = 0;
$eventManager->attach("post", function () use (&$count) {
$count++;
}, 100);
$eventManager->attach("post", function () use (&$count) {
throw new \Exception();
}, 110);
$eventManager->attach("post", function () use (&$count) {
$count++;
}, 120);
try {
$eventManager->trigger("/post/");
} catch (\Exception $exc) {
// STOP!
}
```