cyrilleinvalides/choupas/www/admin/app/libs/sense/System/Timers/ArrayTimer.php

82 lines
1.2 KiB
PHP
Executable File

<?php
class ArrayTimer extends ArrayObject {
public static function get ( $period ) {
$a = new static();
return $a->setPeriod($period);
}
private $period = 60, $min = 0;
public function setPeriod ( $period, $min = 0 ) {
$this->period = $period;
$this->min = $min;
return $this;
}
public function update ( $id, $delta = 0 ) {
$this[$id] = time() + $delta;
return $this;
}
public function offsetGet ( $id ) {
if ( !$this->offsetExists($id) )
$this[$id] = time() - $this->period;
return parent::offsetGet($id);
}
public function sleepTime ( $id ) {
return max(0, $this->period + $this[$id] - time());
}
public function sleepSkip ( $id ) {
$skip = !$this->sleepTime($id);
if ( $skip )
$this->update($id);
return $skip;
}
public function sleep ( $id ) {
$sleep = $this->sleepTime($id);
if ( $sleep || $this->min )
sleep(max($sleep, $this->min));
return $this->update($id);
}
public function select () {
$timer = $this;
return new CallbackFilterIterator($this, function ( $value, $key, $it ) use ( $timer ) {
return !$timer->sleepTime($key);
});
}
}
?>