Blame | Letzte Änderung | Log anzeigen | RSS feed
<?php namespace Clockwork\Request\Timeline;// Data structure representing a single timeline event with fluent APIclass Event{// Event descriptionpublic $description;// Unique event namepublic $name;// Start timepublic $start;// End timepublic $end;// Color (blue, red, green, purple, grey)public $color;// Additional event datapublic $data;public function __construct($description, $data = []){$this->description = $description;$this->name = isset($data['name']) ? $data['name'] : $description;$this->start = isset($data['start']) ? $data['start'] : null;$this->end = isset($data['end']) ? $data['end'] : null;$this->color = isset($data['color']) ? $data['color'] : null;$this->data = isset($data['data']) ? $data['data'] : null;}// Begin the event at current timepublic function begin(){$this->start = microtime(true);return $this;}// End the event at current timepublic function end(){$this->end = microtime(true);return $this;}// Begin the event, execute the passed in closure and end the event, returns the closure return valuepublic function run(\Closure $closure){$this->begin();try {return $closure();} finally {$this->end();}}// Set or retrieve event duration (in ms), event can be defined with both start and end time or just a single time and durationpublic function duration($duration = null){if (! $duration) return ($this->start && $this->end) ? ($this->end - $this->start) * 1000 : 0;if ($this->start) $this->end = $this->start + $duration / 1000;if ($this->end) $this->start = $this->end - $duration / 1000;return $this;}// Finalize the event, ends the event, fills in start time if empty and limits the start and end timepublic function finalize($start = null, $end = null){$end = $end ?: microtime(true);$this->start = $this->start ?: $start;$this->end = $this->end ?: $end;if ($this->start < $start) $this->start = $start;if ($this->end > $end) $this->end = $end;}// Fluent APIpublic function __call($method, $parameters){if (! count($parameters)) return $this->$method;$this->$method = $parameters[0];return $this;}// Return an array representation of the eventpublic function toArray(){return ['description' => $this->description,'start' => $this->start,'end' => $this->end,'duration' => $this->duration(),'color' => $this->color,'data' => $this->data];}}