clock-work
This commit is contained in:
105
vendor/itsgoingd/clockwork/Clockwork/Request/Timeline/Event.php
vendored
Normal file
105
vendor/itsgoingd/clockwork/Clockwork/Request/Timeline/Event.php
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php namespace Clockwork\Request\Timeline;
|
||||
|
||||
// Data structure representing a single timeline event with fluent API
|
||||
class Event
|
||||
{
|
||||
// Event description
|
||||
public $description;
|
||||
// Unique event name
|
||||
public $name;
|
||||
|
||||
// Start time
|
||||
public $start;
|
||||
// End time
|
||||
public $end;
|
||||
|
||||
// Color (blue, red, green, purple, grey)
|
||||
public $color;
|
||||
// Additional event data
|
||||
public $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 time
|
||||
public function begin()
|
||||
{
|
||||
$this->start = microtime(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// End the event at current time
|
||||
public function end()
|
||||
{
|
||||
$this->end = microtime(true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Begin the event, execute the passed in closure and end the event, returns the closure return value
|
||||
public function run(\Closure $closure, ...$args)
|
||||
{
|
||||
$this->begin();
|
||||
try {
|
||||
return $closure(...$args);
|
||||
} 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 duration
|
||||
public 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 time
|
||||
public 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 API
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (! count($parameters)) return $this->$method;
|
||||
|
||||
$this->$method = $parameters[0];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Return an array representation of the event
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'description' => $this->description,
|
||||
'start' => $this->start,
|
||||
'end' => $this->end,
|
||||
'duration' => $this->duration(),
|
||||
'color' => $this->color,
|
||||
'data' => $this->data
|
||||
];
|
||||
}
|
||||
}
|
||||
72
vendor/itsgoingd/clockwork/Clockwork/Request/Timeline/Timeline.php
vendored
Normal file
72
vendor/itsgoingd/clockwork/Clockwork/Request/Timeline/Timeline.php
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php namespace Clockwork\Request\Timeline;
|
||||
|
||||
// Data structure representing collection of time-based events
|
||||
class Timeline
|
||||
{
|
||||
// Timeline events
|
||||
public $events = [];
|
||||
|
||||
// Create a new timeline, optionally with existing events
|
||||
public function __construct($events = [])
|
||||
{
|
||||
foreach ($events as $event) {
|
||||
$this->create($event['description'], $event);
|
||||
}
|
||||
}
|
||||
|
||||
// Find or create a new event, takes description and optional data - name, start, end, duration, color, data
|
||||
public function event($description, $data = [])
|
||||
{
|
||||
$name = isset($data['name']) ? $data['name'] : $description;
|
||||
|
||||
if ($event = $this->find($name)) return $event;
|
||||
|
||||
return $this->create($description, $data);
|
||||
}
|
||||
|
||||
// Create a new event, takes description and optional data - name, start, end, duration, color, data
|
||||
public function create($description, $data = [])
|
||||
{
|
||||
return $this->events[] = new Event($description, $data);
|
||||
}
|
||||
|
||||
// Find event by name
|
||||
public function find($name)
|
||||
{
|
||||
foreach ($this->events as $event) {
|
||||
if ($event->name == $name) return $event;
|
||||
}
|
||||
}
|
||||
|
||||
// Merge another timeline instance into the current timeline
|
||||
public function merge(Timeline $timeline)
|
||||
{
|
||||
$this->events = array_merge($this->events, $timeline->events);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// Finalize timeline, ends all events, sorts them and returns as an array
|
||||
public function finalize($start = null, $end = null)
|
||||
{
|
||||
foreach ($this->events as $event) {
|
||||
$event->finalize($start, $end);
|
||||
}
|
||||
|
||||
$this->sort();
|
||||
|
||||
return $this->toArray();
|
||||
}
|
||||
|
||||
// Sort the timeline events by start time
|
||||
public function sort()
|
||||
{
|
||||
usort($this->events, function ($a, $b) { return $a->start * 1000 - $b->start * 1000; });
|
||||
}
|
||||
|
||||
// Return events as an array
|
||||
public function toArray()
|
||||
{
|
||||
return array_map(function ($event) { return $event->toArray(); }, $this->events);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user