-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventBootstrap.php
executable file
·59 lines (56 loc) · 1.67 KB
/
EventBootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* EventBootstrap class file
* @copyright Copyright (c) 2014 Galament
* @license http://www.yiiframework.com/license/
*/
namespace bariew\eventManager;
use yii\base\BootstrapInterface;
use yii\base\Application;
/**
* Bootstrap class initiates event manager.
*
* @author Pavel Bariev <[email protected]>
*/
class EventBootstrap implements BootstrapInterface
{
/**
* @var EventManager EventManager memory storage for getEventManager method
*/
protected static $_eventManager;
/**
* @inheritdoc
*/
public function bootstrap($app)
{
self::getEventManager($app);
}
/**
* finds and creates app event manager from its settings
* @param Application $app yii app
* @return EventManager app event manager component
* @throws Exception Define event manager
*/
public static function getEventManager($app)
{
if (self::$_eventManager) {
return self::$_eventManager;
}
foreach ($app->components as $name => $config) {
$class = is_string($config) ? $config : @$config['class'];
if($class == str_replace('Bootstrap', 'Manager', get_called_class())){
return self::$_eventManager = $app->$name;
}
}
$eventFile = \Yii::getAlias('@app/config/_events.php');
$app->setComponents([
'eventManager' => [
'class' => 'bariew\eventManager\EventManager',
'events' => file_exists($eventFile) && is_file($eventFile)
? include $eventFile
: []
],
]);
return self::$_eventManager = $app->eventManager;
}
}