|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: arnaud |
|
5
|
|
|
* Date: 04/11/15 |
|
6
|
|
|
* Time: 20:53 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ndrx\Profiler\Components; |
|
10
|
|
|
|
|
11
|
|
|
use Ndrx\Profiler\Events\DispatcherAwareInterface; |
|
12
|
|
|
use Ndrx\Profiler\Events\DispatcherAwareTrait; |
|
13
|
|
|
use Ndrx\Profiler\Events\Timeline\End; |
|
14
|
|
|
use Ndrx\Profiler\Events\Timeline\Start; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
16
|
|
|
|
|
17
|
|
|
class Timeline implements DispatcherAwareInterface |
|
18
|
|
|
{ |
|
19
|
|
|
use DispatcherAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param EventDispatcher $eventDispatcher |
|
23
|
|
|
*/ |
|
24
|
102 |
|
public function __construct(EventDispatcher $eventDispatcher) |
|
25
|
|
|
{ |
|
26
|
102 |
|
$this->dispatcher = $eventDispatcher; |
|
27
|
102 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Start item for the timeline |
|
32
|
|
|
* |
|
33
|
|
|
* @param $key |
|
34
|
|
|
* @param $label |
|
35
|
|
|
* @param null $data |
|
36
|
|
|
* @param null $timetamp |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function start($key, $label, $data = null, $timetamp = null) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$event = new Start($key, $label, $data, $timetamp); |
|
41
|
2 |
|
$this->dispatcher->dispatch(Start::EVENT_NAME, $event); |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* End item for the timeline |
|
46
|
|
|
* |
|
47
|
|
|
* @param $key |
|
48
|
|
|
* @param null $timetamp |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function stop($key, $timetamp = null) |
|
51
|
|
|
{ |
|
52
|
2 |
|
$event = new End($key, $timetamp); |
|
53
|
2 |
|
$this->dispatcher->dispatch(End::EVENT_NAME, $event); |
|
54
|
2 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param $label |
|
58
|
|
|
* @param \Closure $closure |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function monitor($label, \Closure $closure) |
|
62
|
|
|
{ |
|
63
|
2 |
|
$key = uniqid(); |
|
64
|
2 |
|
$this->start($key, $label); |
|
65
|
2 |
|
$result = $closure(); |
|
66
|
2 |
|
$this->start($key, $label); |
|
67
|
|
|
|
|
68
|
2 |
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|