|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Interop\Async\EventLoop; |
| 4 | + |
| 5 | +final class EventLoop |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @var EventLoopDriver |
| 9 | + */ |
| 10 | + private static $driver = null; |
| 11 | + |
| 12 | + /** |
| 13 | + * Execute a callback within the scope of an event loop driver. |
| 14 | + * |
| 15 | + * @param callable $callback The callback to execute |
| 16 | + * @param EventLoopDriver $driver The event loop driver |
| 17 | + * |
| 18 | + * @return void |
| 19 | + */ |
| 20 | + public static function execute(callable $callback, EventLoopDriver $driver) |
| 21 | + { |
| 22 | + $previousDriver = self::$driver; |
| 23 | + |
| 24 | + self::$driver = $driver; |
| 25 | + |
| 26 | + try { |
| 27 | + $callback(); |
| 28 | + |
| 29 | + self::$driver->run(); |
| 30 | + } finally { |
| 31 | + self::$driver = $previousDriver; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Retrieve the event loop driver that is in scope. |
| 37 | + * |
| 38 | + * @return EventLoopDriver |
| 39 | + */ |
| 40 | + public static function get() |
| 41 | + { |
| 42 | + if (null === self::$driver) { |
| 43 | + throw new \RuntimeException('Not within the scope of an event loop driver'); |
| 44 | + } |
| 45 | + |
| 46 | + return self::$driver; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Stop the event loop. |
| 51 | + * |
| 52 | + * @return void |
| 53 | + */ |
| 54 | + public static function stop() |
| 55 | + { |
| 56 | + self::get()->stop(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Defer the execution of a callback. |
| 61 | + * |
| 62 | + * @param callable $callback The callback to defer. |
| 63 | + * |
| 64 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 65 | + */ |
| 66 | + public static function defer(callable $callback) |
| 67 | + { |
| 68 | + return self::get()->defer($callback); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Delay the execution of a callback. The time delay is approximate and accuracy is not guaranteed. |
| 73 | + * |
| 74 | + * @param callable $callback The callback to delay. |
| 75 | + * @param int $time The amount of time, in milliseconds, to delay the execution for. |
| 76 | + * |
| 77 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 78 | + */ |
| 79 | + public static function delay(callable $callback, $time) |
| 80 | + { |
| 81 | + return self::get()->delay($callback, $time); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Repeatedly execute a callback. The interval between executions is approximate and accuracy is not guaranteed. |
| 86 | + * |
| 87 | + * @param callable $callback The callback to repeat. |
| 88 | + * @param int $interval The time interval, in milliseconds, to wait between executions. |
| 89 | + * |
| 90 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 91 | + */ |
| 92 | + public static function repeat(callable $callback, $interval) |
| 93 | + { |
| 94 | + return self::get()->repeat($callback, $interval); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Execute a callback when a stream resource becomes readable. |
| 99 | + * |
| 100 | + * @param resource $stream The stream to monitor. |
| 101 | + * @param callable $callback The callback to execute. |
| 102 | + * |
| 103 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 104 | + */ |
| 105 | + public static function onReadable($stream, callable $callback) |
| 106 | + { |
| 107 | + return self::get()->onReadable($stream, $callback); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Execute a callback when a stream resource becomes writable. |
| 112 | + * |
| 113 | + * @param resource $stream The stream to monitor. |
| 114 | + * @param callable $callback The callback to execute. |
| 115 | + * |
| 116 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 117 | + */ |
| 118 | + public static function onWritable($stream, callable $callback) |
| 119 | + { |
| 120 | + return self::get()->onWritable($stream, $callback); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Execute a callback when a signal is received. |
| 125 | + * |
| 126 | + * @param int $signo The signal number to monitor. |
| 127 | + * @param callable $callback The callback to execute. |
| 128 | + * |
| 129 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 130 | + */ |
| 131 | + public static function onSignal($signo, callable $callback) |
| 132 | + { |
| 133 | + return self::get()->onSignal($signo, $callback); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Execute a callback when an error occurs. |
| 138 | + * |
| 139 | + * @param callable $callback The callback to execute. |
| 140 | + * |
| 141 | + * @return string An identifier that can be used to cancel, enable or disable the event. |
| 142 | + */ |
| 143 | + public static function onError(callable $callback) |
| 144 | + { |
| 145 | + return self::get()->onError($callback); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Enable an event. |
| 150 | + * |
| 151 | + * @param string $eventIdentifier The event identifier. |
| 152 | + * |
| 153 | + * @return void |
| 154 | + */ |
| 155 | + public static function enable($eventIdentifier) |
| 156 | + { |
| 157 | + self::get()->enable($eventIdentifier); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Disable an event. |
| 162 | + * |
| 163 | + * @param string $eventIdentifier The event identifier. |
| 164 | + * |
| 165 | + * @return void |
| 166 | + */ |
| 167 | + public static function disable($eventIdentifier) |
| 168 | + { |
| 169 | + self::get()->disable($eventIdentifier); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Cancel an event. |
| 174 | + * |
| 175 | + * @param string $eventIdentifier The event identifier. |
| 176 | + * |
| 177 | + * @return void |
| 178 | + */ |
| 179 | + public static function cancel($eventIdentifier) |
| 180 | + { |
| 181 | + self::get()->cancel($eventIdentifier); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Reference an event. |
| 186 | + * |
| 187 | + * This will keep the event loop alive whilst the event is still being monitored. Events have this state by default. |
| 188 | + * |
| 189 | + * @param string $eventIdentifier The event identifier. |
| 190 | + * |
| 191 | + * @return void |
| 192 | + */ |
| 193 | + public static function reference($eventIdentifier) |
| 194 | + { |
| 195 | + self::get()->reference($eventIdentifier); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Unreference an event. |
| 200 | + * |
| 201 | + * The event loop should exit the run method when only unreferenced events are still being monitored. Events are all |
| 202 | + * referenced by default. |
| 203 | + * |
| 204 | + * @param string $eventIdentifier The event identifier. |
| 205 | + * |
| 206 | + * @return void |
| 207 | + */ |
| 208 | + public static function unreference($eventIdentifier) |
| 209 | + { |
| 210 | + self::get()->unreference($eventIdentifier); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Disable construction as this is a static class. |
| 215 | + */ |
| 216 | + private function __construct() {} |
| 217 | +} |
0 commit comments