Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
199 lars 1
<?php namespace Clockwork\Request\Timeline;
2
 
3
// Data structure representing a single timeline event with fluent API
4
class Event
5
{
6
	// Event description
7
	public $description;
8
	// Unique event name
9
	public $name;
10
 
11
	// Start time
12
	public $start;
13
	// End time
14
	public $end;
15
 
16
	// Color (blue, red, green, purple, grey)
17
	public $color;
18
	// Additional event data
19
	public $data;
20
 
21
	public function __construct($description, $data = [])
22
	{
23
		$this->description = $description;
24
		$this->name = isset($data['name']) ? $data['name'] : $description;
25
 
26
		$this->start = isset($data['start']) ? $data['start'] : null;
27
		$this->end = isset($data['end']) ? $data['end'] : null;
28
 
29
		$this->color = isset($data['color']) ? $data['color'] : null;
30
		$this->data = isset($data['data']) ? $data['data'] : null;
31
	}
32
 
33
	// Begin the event at current time
34
	public function begin()
35
	{
36
		$this->start = microtime(true);
37
 
38
		return $this;
39
	}
40
 
41
	// End the event at current time
42
	public function end()
43
	{
44
		$this->end = microtime(true);
45
 
46
		return $this;
47
	}
48
 
49
	// Begin the event, execute the passed in closure and end the event, returns the closure return value
50
	public function run(\Closure $closure)
51
	{
52
		$this->begin();
53
		try {
54
			return $closure();
55
		} finally {
56
			$this->end();
57
		}
58
	}
59
 
60
	// Set or retrieve event duration (in ms), event can be defined with both start and end time or just a single time and duration
61
	public function duration($duration = null)
62
	{
63
		if (! $duration) return ($this->start && $this->end) ? ($this->end - $this->start) * 1000 : 0;
64
 
65
		if ($this->start) $this->end = $this->start + $duration / 1000;
66
		if ($this->end) $this->start = $this->end - $duration / 1000;
67
 
68
		return $this;
69
	}
70
 
71
	// Finalize the event, ends the event, fills in start time if empty and limits the start and end time
72
	public function finalize($start = null, $end = null)
73
	{
74
		$end = $end ?: microtime(true);
75
 
76
		$this->start = $this->start ?: $start;
77
		$this->end = $this->end ?: $end;
78
 
79
		if ($this->start < $start) $this->start = $start;
80
		if ($this->end > $end) $this->end = $end;
81
	}
82
 
83
	// Fluent API
84
	public function __call($method, $parameters)
85
	{
86
		if (! count($parameters)) return $this->$method;
87
 
88
		$this->$method = $parameters[0];
89
 
90
		return $this;
91
	}
92
 
93
	// Return an array representation of the event
94
	public function toArray()
95
	{
96
		return [
97
			'description' => $this->description,
98
			'start'       => $this->start,
99
			'end'         => $this->end,
100
			'duration'    => $this->duration(),
101
			'color'       => $this->color,
102
			'data'        => $this->data
103
		];
104
	}
105
}