Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yiicod/yii2-listener
https://github.com/yiicod/yii2-listener
Last synced: 27 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/yiicod/yii2-listener
- Owner: yiicod
- License: other
- Created: 2015-03-15T14:32:34.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-04-09T20:22:11.000Z (over 6 years ago)
- Last Synced: 2024-11-10T04:24:27.205Z (about 2 months ago)
- Language: PHP
- Size: 14.6 KB
- Stars: 14
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
Yii Event listener manager
==========================[![Latest Stable Version](https://poser.pugx.org/yiicod/yii2-listener/v/stable)](https://packagist.org/packages/yiicod/yii2-listener) [![Total Downloads](https://poser.pugx.org/yiicod/yii2-listener/downloads)](https://packagist.org/packages/yiicod/yii2-listener) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiicod/yii2-listener/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiicod/yii2-listener/?branch=master)[![Code Climate](https://codeclimate.com/github/yiicod/yii2-listener/badges/gpa.svg)](https://codeclimate.com/github/yiicod/yii2-listener)
Provides listener logic.
Command parses chosen paths to find all listener/subscribers (depends on ListenerInterface and SubscriberInterface implementation).
Where listener can be used for single event and subscriber can be used for bunch of events.#### Installation
Either run
```
php composer.phar require --prefer-dist yiicod/yii2-listener "*"
```or add
```json
"yii2-listener": "*"
```
to your composer.json file#### Web config
```php
'bootstrap' => ['listener'],
'components' => [
'listener' => [
'class' => 'yiicod\listener\components\Listener'
],
]
```#### Console config
```php
'controllerMap' => [
'listener' => [
'class' => \yiicod\listener\commands\Listener::class
],
]
```
Run command. This command will warm and prepare all listeners and subscriptions. Run this command each time when you create
new Listener or Subscription.
```bash
listener/parse
```For trigger event use yii manual:
http://www.yiiframework.com/doc-2.0/guide-concept-events.html#class-level-event-handlers#### Listener usage
```php
namespace frontend\observers\listeners;use yii\db\ActiveRecord;
use yiicod\listener\components\listeners\ListenerAbstract;class TestListener extends ListenerAbstract
{
/**
* Call on event method
*/
public function handle($event)
{
// TODO: Implement handle() method.
}/**
* Return event name for emit
* @return string
*/
public static function event(): string
{
return ActiveRecord::class . '@' . ActiveRecord::EVENT_AFTER_FIND;
}
}
```#### Usage subscriber
```php
namespace frontend\observers\subscribers;use yii\db\ActiveRecord;
use yiicod\listener\components\listeners\SubscriberAbstract;class TestSubscriber extends SubscriberAbstract
{
/**
* @return array
* [
* 'event_class@event1' => 'on'
* 'event_class@event2' => 'on'
* ]
*/
public static function subscribe(): array
{
return [
ActiveRecord::class . '@' . ActiveRecord::EVENT_BEFORE_INSERT => 'on',
ActiveRecord::class . '@' . ActiveRecord::EVENT_AFTER_INSERT => 'on'
];
}/**
* Call on event method
*/
public function on($event)
{
// Handle
}
}
```