https://github.com/phalcon/incubator-events
Extra Phalcon Events Adapters.
https://github.com/phalcon/incubator-events
Last synced: about 1 year ago
JSON representation
Extra Phalcon Events Adapters.
- Host: GitHub
- URL: https://github.com/phalcon/incubator-events
- Owner: phalcon
- License: bsd-3-clause
- Created: 2020-05-20T11:57:28.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-19T19:26:56.000Z (over 3 years ago)
- Last Synced: 2025-06-21T20:35:39.485Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 57.6 KB
- Stars: 4
- Watchers: 7
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Phalcon\Incubator\Events
Usage examples of the features is here:
## Manager
### Classic event handling
Classic event handling requires one class with a bunch of methods named equal to event name.
And multiple `$eventsManager->attach()` calls if you use more than one handler for single event:
```php
class DispatchEventsHandler
{
public function beforeCallActionMethod(
Phalcon\Events\EventInterface $event,
Phalcon\Dispatcher\DispatcherInterface $dispatcher
): void {
//do some stuff
}
public function beforeDoSomeMistakes(
Phalcon\Events\EventInterface $event,
Phalcon\Dispatcher\DispatcherInterface $dispatcher
): void {
//do some right stuff
}
}
class DispatchEventsHandlerTwo
{
public function beforeCallActionMethod(
Phalcon\Events\EventInterface $event,
Phalcon\Dispatcher\DispatcherInterface $dispatcher
): void {
//do another stuff in same event
}
}
$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch:beforeCallActionMethod',new DispatchEventsHandlerTwo(), 101);
$eventsManager->attach('dispatch:beforeDoSomeMistakes',new DispatchEventsHandler());
//or global way
$eventsManager->attach('dispatch',new DispatchEventsHandler(), 100);
$eventsManager->attach('dispatch',new DispatchEventsHandlerTwo(), 101);
```
### Featured event handling
This manager version provides a feature of single responsible event handlers via configs, using `__invoke`.
Create your handler class:
```php
class BeforeCallActionMethod
{
public function __invoke(
Phalcon\Events\EventInterface $event,
Phalcon\Dispatcher\DispatcherInterface $dispatcher
): void {
//do some stuff
}
}
```
Define configuration in config file:
```php
$config = new Phalcon\Config([
'handlers' => [
'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
],
]);
```
Define `Phalcon\Incubator\Events\Manager` in container as eventsManager service using config of handlers:
```php
$handlers = $config->get('handlers')->toArray();
$eventsManager = new Phalcon\Incubator\Events\Manager();
$eventsManager->loadHandlers($handlers);
```
There are multiple ways to define configs of handlers:
Flat classname usage, if your handler is invokable:
```php
$flat = new Phalcon\Config([
'handlers' => [
'dispatch:beforeCallActionMethod' => BeforeCallActionMethod::class,
],
]);
```
Same, but more informative and with optional priority:
```php
$verbose = new Phalcon\Config([
'handlers' => [
'dispatch:beforeCallActionMethod' => [
'class' => BeforeCallActionMethod::class,
'priority' => 100, //optional
],
],
]);
```
Using callable constructions:
```php
$callable = new Phalcon\Config([
'handlers' => [
//you can use any other public method name instead of method, for example run()
'dispatch:beforeCallActionMethod' => [new BeforeCallActionMethod(), 'run'],
//or
'dispatch:beforeCallActionMethod' => [BeforeCallActionMethod::class, 'staticRun'],
],
]);
```
Grouping more than one handler for one event:
```php
$grouped = new Phalcon\Config([
'handlers' => [
'dispatch:beforeCallActionMethod' => [
BeforeCallActionMethod::class,
BeforeCallActionMethodAnother::class,
BeforeCallActionMethodThird::class,
],
],
]);
$groupedVerbose = new Phalcon\Config([
'handlers' => [
'dispatch:beforeCallActionMethod' => [
[
'class' => BeforeCallActionMethod::class,
'priority' => 100, //optional
],
[
'class' => BeforeCallActionMethodAnother::class,
'priority' => 101, //optional
],
[
'class' => BeforeCallActionMethodThird::class,
'priority' => 101, //optional
],
],
],
]);
```