1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Monolog Extensions |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/MonologExtensions/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/MonologExtensions |
12
|
|
|
*/ |
13
|
|
|
namespace Graze\Monolog; |
14
|
|
|
|
15
|
|
|
use DateTime; |
16
|
|
|
use DateTimeZone; |
17
|
|
|
|
18
|
|
|
class Event |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $eventData; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array of event handlers |
27
|
|
|
*/ |
28
|
|
|
private $handlers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $handlers |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
7 |
|
public function __construct(array $handlers = []) |
35
|
|
|
{ |
36
|
7 |
|
$this->handlers = $handlers; |
37
|
7 |
|
$this->eventData = [ |
38
|
7 |
|
'eventIdentifier' => 'defaultEvent', |
39
|
7 |
|
'timestamp' => $this->getNow(), |
40
|
|
|
'data' => [], |
41
|
|
|
'metadata' => [], |
42
|
|
|
|
43
|
|
|
]; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* sets $value under key $key in data store |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
1 |
|
public function setData($key, $value) |
54
|
|
|
{ |
55
|
1 |
|
$this->eventData['data'][$key] = $value; |
56
|
1 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* sets $value under key $key in metadata store |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $value |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
1 |
|
public function setMetadata($key, $value) |
67
|
|
|
{ |
68
|
1 |
|
$this->eventData['metadata'][$key] = $value; |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* sets the (string) identifier of the event by which it will be identified |
74
|
|
|
* |
75
|
|
|
* @param string $identifier |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
1 |
|
public function setIdentifier($identifier) |
79
|
|
|
{ |
80
|
1 |
|
$this->eventData['eventIdentifier'] = $identifier; |
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* triggers all the event handlers set for this event |
86
|
|
|
* |
87
|
|
|
* @return bool true if at least one handler set |
88
|
|
|
*/ |
89
|
6 |
|
public function publish() |
90
|
|
|
{ |
91
|
6 |
|
if (empty($this->handlers)) { |
92
|
1 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
foreach ($this->handlers as $handler) { |
96
|
5 |
|
$handler->handle($this->eventData); |
97
|
|
|
} |
98
|
5 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* returns a datetime object representing the current instant with microseconds |
103
|
|
|
* |
104
|
|
|
* @return \DateTime |
105
|
|
|
*/ |
106
|
7 |
|
private function getNow() |
107
|
|
|
{ |
108
|
7 |
|
$timezone = new DateTimeZone(date_default_timezone_get() ?: 'UTC'); |
109
|
|
|
|
110
|
7 |
|
return DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)))->setTimezone($timezone); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.