48 static protected $binary = null;
51 static protected $windowsStrategy = 0;
85 if (self::$binary === null) {
86 if (defined(
'PHP_BINARY')) {
89 $binary = PHP_BINDIR . DIRECTORY_SEPARATOR .
'php' .
90 ((!strncasecmp(PHP_OS,
'WIN', 3)) ?
'.exe' :
'');
93 if (!strncasecmp(PHP_OS,
'WIN', 3)) {
94 self::$windowsStrategy = 1 + (
95 (int) version_compare(PHP_VERSION,
'5.3.0',
'>=')
98 self::$binary =
'"' . $binary .
'"';
100 if (defined(
'HHVM_VERSION')) {
101 self::$binary .=
' --php';
105 $this->delay = $delay;
106 $this->handle = null;
107 $this->resource = null;
108 $this->setCallback($callback);
109 $this->setRepetition($repeat);
110 $this->setArgs($args);
125 if ($this->resource) {
126 proc_terminate($this->resource);
129 if (is_resource($this->handle)) {
130 fclose($this->handle);
133 $this->handle = null;
134 $this->resource = null;
139 $this->callback = $callback;
144 return $this->callback;
164 return $this->repeat;
171 if (is_bool($repeat)) {
172 $repeat = (-intval($repeat)) * 2 + 1;
177 if (!is_int($repeat) && $repeat !== null) {
178 throw new \InvalidArgumentException(
'Invalid repetition');
181 $this->repeat = $repeat;
186 return $this->handle;
191 if ($this->repeat > 0) {
193 } elseif (!$this->repeat) {
199 if (self::$windowsStrategy == 1) {
207 $this->handle = tmpfile();
208 $descriptors = $this->handle;
209 } elseif (self::$windowsStrategy == 2) {
213 $pair = stream_socket_pair(
218 $descriptors = $pair[0];
219 $this->handle = $pair[1];
222 $descriptors = array(
'pipe',
'w');
226 $command = self::$binary .
' -n -d detect_unicode=Off ' .
227 '-d display_errors=Off -d display_startup_errors=Off ' .
228 '-r "usleep('. ((int) ($this->delay * 1000000)).
'); ' .
232 '// '.addslashes(\
Erebot\CallableWrapper::represent($this->callback)).
'"';
234 $this->resource = proc_open(
236 array(1 => $descriptors),
240 array(
'bypass_shell' =>
true)
243 if (self::$windowsStrategy == 1) {
247 fgets($this->handle);
248 } elseif (self::$windowsStrategy == 2) {
252 $this->handle = $pipes[1];
261 $args = array_merge(array(&$this), $this->args);
262 return (
bool) call_user_func_array($this->callback, $args);
__construct(\Erebot\CallableInterface $callback, $delay, $repeat, $args=array())
An implementation of timers.
Interface for something that can be called.
__destruct()
Destroys the timer.
$delay
Delay after which the timer will expire.
$callback
Function or method to call when the timer expires.
setCallback(\Erebot\CallableInterface $callback)
$resource
Internal resource used to implement timers.
Interface for a timer implementation.
$repeat
Number of times the timer will be reset.
$args
Additional arguments to call the callback function with.
$handle
A file descriptor which is used to implement timers.