1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Notimatica\Driver\Support; |
4
|
|
|
|
5
|
|
|
use League\Event\Emitter; |
6
|
|
|
use League\Event\EventInterface; |
7
|
|
|
use League\Event\ListenerInterface; |
8
|
|
|
use Notimatica\Driver\StatisticsStorages\AbstractStorage; |
9
|
|
|
use Notimatica\Driver\StatisticsStoragesFactory; |
10
|
|
|
|
11
|
|
|
trait EventsEmitter |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Emitter |
15
|
|
|
*/ |
16
|
|
|
public static $events; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Boot events emitter. |
20
|
|
|
*/ |
21
|
9 |
|
public function bootEvents() |
22
|
|
|
{ |
23
|
9 |
|
static::$events = new Emitter(); |
24
|
|
|
|
25
|
9 |
|
$this->bootListeners(); |
26
|
9 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Boot event listeners. |
30
|
|
|
*/ |
31
|
9 |
|
protected function bootListeners() |
32
|
|
|
{ |
33
|
9 |
|
$statisticsStorage = $this->makeStatisticsStorage(); |
34
|
|
|
|
35
|
9 |
|
if (!is_null($statisticsStorage)) { |
36
|
9 |
|
static::$events->useListenerProvider($statisticsStorage); |
37
|
9 |
|
} |
38
|
9 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Emit event. |
42
|
|
|
* |
43
|
|
|
* @return \League\Event\EventInterface|string |
44
|
|
|
*/ |
45
|
4 |
|
public static function emit() |
46
|
|
|
{ |
47
|
4 |
|
return call_user_func_array([static::$events, 'emit'], func_get_args()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Emit event. |
52
|
|
|
* |
53
|
|
|
* @param string $event |
54
|
|
|
* @param ListenerInterface|callable $listener |
55
|
|
|
* @return EventInterface|string |
56
|
|
|
*/ |
57
|
1 |
|
public static function on($event, $listener) |
58
|
|
|
{ |
59
|
1 |
|
return static::$events->addListener($event, $listener); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Remove listeners. |
64
|
|
|
* |
65
|
|
|
* @param $event |
66
|
|
|
*/ |
67
|
1 |
|
public static function off($event) |
68
|
|
|
{ |
69
|
1 |
|
static::$events->removeAllListeners($event); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Make statistics storage. |
74
|
|
|
* |
75
|
|
|
* @return AbstractStorage|null |
76
|
|
|
*/ |
77
|
9 |
|
protected function makeStatisticsStorage() |
78
|
|
|
{ |
79
|
9 |
|
if (!empty($this->project->config['statistics']['storage']) |
80
|
9 |
|
|| class_exists($this->project->config['statistics']['storage'])) { |
81
|
9 |
|
$storage = $this->project->config['statistics']['storage']; |
|
|
|
|
82
|
|
|
|
83
|
9 |
|
return new $storage; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: