1: <?php
2:
3: namespace React\EventLoop\Timer;
4:
5: use React\EventLoop\LoopInterface;
6:
7: class Timer implements TimerInterface
8: {
9: const MIN_INTERVAL = 0.000001;
10:
11: protected $loop;
12: protected $interval;
13: protected $callback;
14: protected $periodic;
15: protected $data;
16:
17: /**
18: * Constructor initializes the fields of the Timer
19: *
20: * @param LoopInterface $loop The loop with which this timer is associated
21: * @param float $interval The interval after which this timer will execute, in seconds
22: * @param callable $callback The callback that will be executed when this timer elapses
23: * @param bool $periodic Whether the time is periodic
24: * @param mixed $data Arbitrary data associated with timer
25: */
26: public function __construct(LoopInterface $loop, $interval, callable $callback, $periodic = false, $data = null)
27: {
28: if ($interval < self::MIN_INTERVAL) {
29: $interval = self::MIN_INTERVAL;
30: }
31:
32: $this->loop = $loop;
33: $this->interval = (float) $interval;
34: $this->callback = $callback;
35: $this->periodic = (bool) $periodic;
36: $this->data = null;
37: }
38:
39: /**
40: * {@inheritdoc}
41: */
42: public function getLoop()
43: {
44: return $this->loop;
45: }
46:
47: /**
48: * {@inheritdoc}
49: */
50: public function getInterval()
51: {
52: return $this->interval;
53: }
54:
55: /**
56: * {@inheritdoc}
57: */
58: public function getCallback()
59: {
60: return $this->callback;
61: }
62:
63: /**
64: * {@inheritdoc}
65: */
66: public function setData($data)
67: {
68: $this->data = $data;
69: }
70:
71: /**
72: * {@inheritdoc}
73: */
74: public function getData()
75: {
76: return $this->data;
77: }
78:
79: /**
80: * {@inheritdoc}
81: */
82: public function isPeriodic()
83: {
84: return $this->periodic;
85: }
86:
87: /**
88: * {@inheritdoc}
89: */
90: public function isActive()
91: {
92: return $this->loop->isTimerActive($this);
93: }
94:
95: /**
96: * {@inheritdoc}
97: */
98: public function cancel()
99: {
100: $this->loop->cancelTimer($this);
101: }
102: }
103: