├── .gitignore ├── composer.json ├── LICENSE ├── EventBootstrap.php ├── EventManager.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea 3 | 4 | # netbeans project files 5 | nbproject 6 | 7 | # zend studio for eclipse project files 8 | .buildpath 9 | .project 10 | .settings 11 | 12 | # windows thumbnail cache 13 | Thumbs.db 14 | 15 | # composer vendor dir 16 | /vendor 17 | 18 | # Mac DS_Store Files 19 | .DS_Store -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bariew/yii2-event-component", 3 | "description": "Attaches events to all models", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension","event"], 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Bariev Pavel", 10 | "email": "bariew@yandex.ru" 11 | } 12 | ], 13 | "autoload": { 14 | "psr-4": { 15 | "bariew\\eventManager\\": "" 16 | } 17 | }, 18 | "extra": { 19 | "bootstrap": "bariew\\eventManager\\EventBootstrap" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Pavel Bariev 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the {organization} nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /EventBootstrap.php: -------------------------------------------------------------------------------- 1 | 17 | */ 18 | class EventBootstrap implements BootstrapInterface 19 | { 20 | /** 21 | * @var EventManager EventManager memory storage for getEventManager method 22 | */ 23 | protected static $_eventManager; 24 | /** 25 | * @inheritdoc 26 | */ 27 | public function bootstrap($app) 28 | { 29 | self::getEventManager($app); 30 | } 31 | /** 32 | * finds and creates app event manager from its settings 33 | * @param Application $app yii app 34 | * @return EventManager app event manager component 35 | * @throws Exception Define event manager 36 | */ 37 | public static function getEventManager($app) 38 | { 39 | if (self::$_eventManager) { 40 | return self::$_eventManager; 41 | } 42 | foreach ($app->components as $name => $config) { 43 | $class = is_string($config) ? $config : @$config['class']; 44 | if($class == str_replace('Bootstrap', 'Manager', get_called_class())){ 45 | return self::$_eventManager = $app->$name; 46 | } 47 | } 48 | $eventFile = \Yii::getAlias('@app/config/_events.php'); 49 | $app->setComponents([ 50 | 'eventManager' => [ 51 | 'class' => 'bariew\eventManager\EventManager', 52 | 'events' => file_exists($eventFile) && is_file($eventFile) 53 | ? include $eventFile 54 | : [] 55 | ], 56 | ]); 57 | return self::$_eventManager = $app->eventManager; 58 | } 59 | } -------------------------------------------------------------------------------- /EventManager.php: -------------------------------------------------------------------------------- 1 | 16 | */ 17 | class EventManager extends Component 18 | { 19 | /** 20 | * System wide models events settings - 21 | * an array with structure: [ 22 | * $eventSenderClassName => [ 23 | * $eventName => [ 24 | * [$handlerClassName, $handlerMethodName] 25 | * ] 26 | * ] 27 | * ] 28 | * 29 | * @since 1.3.0 handler can also keep additional data and $append boolean as for Event::on() method eg: 30 | * ... [[$handlerClassName, $handlerMethodName], ['myData'], false] 31 | * 32 | * @var array events settings 33 | */ 34 | public $events = []; 35 | 36 | /** 37 | * @inheritdoc 38 | */ 39 | public function init() 40 | { 41 | parent::init(); 42 | $this->attachEvents($this->events); 43 | } 44 | 45 | /** 46 | * attaches all events to all classNames 47 | * @param array $eventConfig commonly $this->events config 48 | */ 49 | public function attachEvents($eventConfig) 50 | { 51 | foreach ($eventConfig as $className => $events) { 52 | foreach ($events as $eventName => $handlers) { 53 | foreach ($handlers as $handler) { 54 | if (is_array($handler) && is_callable($handler[0])) { 55 | $data = isset($handler[1]) ? array_pop($handler) : null; 56 | $append = isset($handler[2]) ? array_pop($handler) : null; 57 | Event::on($className, $eventName, $handler[0], $data, $append); 58 | } else if (is_callable($handler)){ 59 | Event::on($className, $eventName, $handler); 60 | } 61 | } 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | Yii2 event manager component 3 | =================== 4 | Attaches events to all application models in a very simple way. 5 | You just list your event handlers in config/_events.php this way: 6 | [ 7 | 'event\sender\ClassName' => [ 8 | 'eventName' => [ 9 | 'event\handler\ClassName' => 'methodName' 10 | ] 11 | ] 12 | ]; 13 | 14 | See example below. 15 | 16 | Installation 17 | ------------ 18 | 19 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 20 | 21 | Either run 22 | 23 | ``` 24 | php composer.phar require --prefer-dist bariew/yii2-event-component "*" 25 | ``` 26 | 27 | or add 28 | 29 | ``` 30 | "bariew/yii2-event-component": "*" 31 | ``` 32 | 33 | to the require section of your `composer.json` file. 34 | 35 | 36 | Usage 37 | ----- 38 | 39 | ``` 40 | Define app component in main config components section like in this example: 41 | 'components' => [ 42 | ... 43 | 'eventManager'=> [ 44 | 'class' => 'bariew\eventManager\EventManager', 45 | 'events' => [ 46 | 'app\models\User' => [ 47 | 'afterInsert' => [ 48 | ['app\models\Email', 'userRegistration'] 49 | ], 50 | ] 51 | ] 52 | ], 53 | ] 54 | 55 | Explanation: in the example we defined that after creating new User model ('afterInsert') 56 | Email::userRegistration($event) method will be called. 57 | ``` 58 | 59 | ``` 60 | Since 1.1.0 you may also not define event manager, but just put _events.php 61 | into your config folder returning the same 'events' array as in example: 62 | 63 | [ 66 | 'afterInsert' => [ 67 | ['app\models\Email', 'userRegistration'] 68 | ], 69 | ] 70 | ]; 71 | ``` 72 | 73 | ``` 74 | since 1.3.0 handler can also keep additional data and $append boolean as for Event::on() method eg: 75 | ... [$handlerClassName, $handlerMethodName, ['myData'], false] 76 | ``` --------------------------------------------------------------------------------