walkor 2 years ago
parent
commit
553e307ed3
6 changed files with 86 additions and 60 deletions
  1. 9 7
      src/Events/Event.php
  2. 4 0
      src/Events/EventInterface.php
  3. 10 5
      src/Events/Revolt.php
  4. 26 20
      src/Events/Select.php
  5. 20 14
      src/Events/Swoole.php
  6. 17 14
      src/Events/Swow.php

+ 9 - 7
src/Events/Event.php

@@ -14,8 +14,10 @@
 
 namespace Workerman\Events;
 
-use Throwable;
 use EventBase;
+use Throwable;
+use function class_exists;
+use function count;
 
 /**
  * libevent eventloop
@@ -76,13 +78,13 @@ class Event implements EventInterface
      */
     public function __construct()
     {
-        if (\class_exists('\\\\Event', false)) {
+        if (class_exists('\\\\Event', false)) {
             $className = '\\\\Event';
         } else {
             $className = '\Event';
         }
         $this->eventClassName = $className;
-        if (\class_exists('\\\\EventBase', false)) {
+        if (class_exists('\\\\EventBase', false)) {
             $className = '\\\\EventBase';
         } else {
             $className = '\EventBase';
@@ -100,7 +102,7 @@ class Event implements EventInterface
         $event = new $className($this->eventBase, -1, $className::TIMEOUT, function () use ($func, $args) {
             try {
                 $func(...$args);
-            } catch (\Throwable $e) {
+            } catch (Throwable $e) {
                 $this->error($e);
             }
         });
@@ -142,7 +144,7 @@ class Event implements EventInterface
         $event = new $className($this->eventBase, -1, $className::TIMEOUT | $className::PERSIST, function () use ($func, $args) {
             try {
                 $func(...$args);
-            } catch (\Throwable $e) {
+            } catch (Throwable $e) {
                 $this->error($e);
             }
         });
@@ -269,7 +271,7 @@ class Event implements EventInterface
      */
     public function getTimerCount(): int
     {
-        return \count($this->eventTimer);
+        return count($this->eventTimer);
     }
 
     /**
@@ -300,7 +302,7 @@ class Event implements EventInterface
                 throw new $e;
             }
             ($this->errorHandler)($e);
-        } catch (\Throwable $e) {
+        } catch (Throwable $e) {
             // Cannot trigger an exception in the Event callback, otherwise it will cause an infinite loop
             echo $e;
         }

+ 4 - 0
src/Events/EventInterface.php

@@ -13,6 +13,8 @@
  */
 namespace Workerman\Events;
 
+use Throwable;
+
 interface EventInterface
 {
     /**
@@ -82,6 +84,7 @@ interface EventInterface
      * @param int $signal
      * @param callable $func
      * @return void
+     * @throws Throwable
      */
     public function onSignal(int $signal, callable $func);
 
@@ -101,6 +104,7 @@ interface EventInterface
     /**
      * Run the event loop.
      * @return void
+     * @throws Throwable
      */
     public function run();
 

+ 10 - 5
src/Events/Revolt.php

@@ -14,8 +14,11 @@
 
 namespace Workerman\Events;
 
-use Revolt\EventLoop\Driver;
 use Revolt\EventLoop;
+use Revolt\EventLoop\Driver;
+use function count;
+use function function_exists;
+use function pcntl_signal;
 
 /**
  * Revolt eventloop
@@ -66,7 +69,9 @@ class Revolt implements EventInterface
     }
 
     /**
-     * {@inheritdoc}
+     * Get driver
+     *
+     * @return Driver
      */
     public function driver(): Driver
     {
@@ -90,8 +95,8 @@ class Revolt implements EventInterface
             $this->driver->cancel($cbId);
         }
         $this->driver->stop();
-        if (\function_exists('pcntl_signal')) {
-            \pcntl_signal(SIGINT, SIG_IGN);
+        if (function_exists('pcntl_signal')) {
+            pcntl_signal(SIGINT, SIG_IGN);
         }
     }
 
@@ -249,7 +254,7 @@ class Revolt implements EventInterface
      */
     public function getTimerCount(): int
     {
-        return \count($this->eventTimer);
+        return count($this->eventTimer);
     }
 
     /**

+ 26 - 20
src/Events/Select.php

@@ -16,6 +16,12 @@ namespace Workerman\Events;
 
 use SplPriorityQueue;
 use Throwable;
+use function count;
+use function max;
+use function microtime;
+use function pcntl_signal;
+use function pcntl_signal_dispatch;
+use const DIRECTORY_SEPARATOR;
 
 /**
  * select eventloop
@@ -126,10 +132,10 @@ class Select implements EventInterface
     public function delay(float $delay, callable $func, array $args = []): int
     {
         $timerId = $this->timerId++;
-        $runTime = \microtime(true) + $delay;
+        $runTime = microtime(true) + $delay;
         $this->scheduler->insert($timerId, -$runTime);
         $this->eventTimer[$timerId] = [$func, $args];
-        $selectTimeout = ($runTime - \microtime(true)) * 1000000;
+        $selectTimeout = ($runTime - microtime(true)) * 1000000;
         $selectTimeout = $selectTimeout <= 0 ? 1 : (int)$selectTimeout;
         if ($this->selectTimeout > $selectTimeout) {
             $this->selectTimeout = $selectTimeout;
@@ -143,10 +149,10 @@ class Select implements EventInterface
     public function repeat(float $interval, callable $func, array $args = []): int
     {
         $timerId = $this->timerId++;
-        $runTime = \microtime(true) + $interval;
+        $runTime = microtime(true) + $interval;
         $this->scheduler->insert($timerId, -$runTime);
         $this->eventTimer[$timerId] = [$func, $args, $interval];
-        $selectTimeout = ($runTime - \microtime(true)) * 1000000;
+        $selectTimeout = ($runTime - microtime(true)) * 1000000;
         $selectTimeout = $selectTimeout <= 0 ? 1 : (int)$selectTimeout;
         if ($this->selectTimeout > $selectTimeout) {
             $this->selectTimeout = $selectTimeout;
@@ -179,10 +185,10 @@ class Select implements EventInterface
      */
     public function onReadable($stream, callable $func)
     {
-        $count = \count($this->readFds);
+        $count = count($this->readFds);
         if ($count >= 1024) {
             echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
-        } else if (\DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
+        } else if (DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
             echo "Warning: system call select exceeded the maximum number of connections 256.\n";
         }
         $fdKey = (int)$stream;
@@ -208,10 +214,10 @@ class Select implements EventInterface
      */
     public function onWritable($stream, callable $func)
     {
-        $count = \count($this->writeFds);
+        $count = count($this->writeFds);
         if ($count >= 1024) {
             echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
-        } else if (\DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
+        } else if (DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
             echo "Warning: system call select exceeded the maximum number of connections 256.\n";
         }
         $fdKey = (int)$stream;
@@ -233,7 +239,7 @@ class Select implements EventInterface
     }
 
     /**
-     * {@inheritdoc}
+     * On except.
      */
     public function onExcept($stream, $func)
     {
@@ -243,7 +249,7 @@ class Select implements EventInterface
     }
 
     /**
-     * {@inheritdoc}
+     * Off except.
      */
     public function offExcept($stream): bool
     {
@@ -260,11 +266,11 @@ class Select implements EventInterface
      */
     public function onSignal(int $signal, callable $func)
     {
-        if (\DIRECTORY_SEPARATOR !== '/') {
+        if (!function_exists('pcntl_signal')) {
             return;
         }
         $this->signalEvents[$signal] = $func;
-        \pcntl_signal($signal, [$this, 'signalHandler']);
+        pcntl_signal($signal, [$this, 'signalHandler']);
     }
 
     /**
@@ -272,10 +278,10 @@ class Select implements EventInterface
      */
     public function offSignal(int $signal): bool
     {
-        if (\DIRECTORY_SEPARATOR !== '/') {
+        if (!function_exists('pcntl_signal')) {
             return false;
         }
-        \pcntl_signal($signal, SIG_IGN);
+        pcntl_signal($signal, SIG_IGN);
         if (isset($this->signalEvents[$signal])) {
             unset($this->signalEvents[$signal]);
             return true;
@@ -306,7 +312,7 @@ class Select implements EventInterface
             $schedulerData = $this->scheduler->top();
             $timerId = $schedulerData['data'];
             $nextRunTime = -$schedulerData['priority'];
-            $timeNow = \microtime(true);
+            $timeNow = microtime(true);
             $this->selectTimeout = (int)(($nextRunTime - $timeNow) * 1000000);
             if ($this->selectTimeout <= 0) {
                 $this->scheduler->extract();
@@ -339,8 +345,8 @@ class Select implements EventInterface
         if (!$this->scheduler->isEmpty()) {
             $schedulerData = $this->scheduler->top();
             $nextRunTime = -$schedulerData['priority'];
-            $timeNow = \microtime(true);
-            $this->selectTimeout = \max((int)(($nextRunTime - $timeNow) * 1000000), 0);
+            $timeNow = microtime(true);
+            $this->selectTimeout = max((int)(($nextRunTime - $timeNow) * 1000000), 0);
             return;
         }
         $this->selectTimeout = 100000000;
@@ -369,7 +375,7 @@ class Select implements EventInterface
                 // Waiting read/write/signal/timeout events.
                 try {
                     @stream_select($read, $write, $except, 0, $this->selectTimeout);
-                } catch (Throwable $e) {
+                } catch (Throwable) {
                 }
             } else {
                 $this->selectTimeout >= 1 && usleep($this->selectTimeout);
@@ -402,7 +408,7 @@ class Select implements EventInterface
 
             if (!empty($this->signalEvents)) {
                 // Calls signal handlers for pending signals
-                \pcntl_signal_dispatch();
+                pcntl_signal_dispatch();
             }
         }
     }
@@ -426,7 +432,7 @@ class Select implements EventInterface
      */
     public function getTimerCount(): int
     {
-        return \count($this->eventTimer);
+        return count($this->eventTimer);
     }
 
     /**

+ 20 - 14
src/Events/Swoole.php

@@ -13,10 +13,14 @@
 
 namespace Workerman\Events;
 
-use Throwable;
 use Swoole\Event;
-use Swoole\Timer;
 use Swoole\Process;
+use Swoole\Timer;
+use Throwable;
+use function count;
+use function posix_kill;
+use const SWOOLE_EVENT_READ;
+use const SWOOLE_EVENT_WRITE;
 
 class Swoole implements EventInterface
 {
@@ -49,7 +53,8 @@ class Swoole implements EventInterface
     public function __construct()
     {
         // Avoid process exit due to no listening
-        Timer::tick(100000000, function () {});
+        Timer::tick(100000000, function () {
+        });
     }
 
     /**
@@ -117,12 +122,12 @@ class Swoole implements EventInterface
     {
         $fd = (int)$stream;
         if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
-            Event::add($stream, $func, null, \SWOOLE_EVENT_READ);
+            Event::add($stream, $func, null, SWOOLE_EVENT_READ);
         } else {
             if (isset($this->writeEvents[$fd])) {
-                Event::set($stream, $func, null, \SWOOLE_EVENT_READ | \SWOOLE_EVENT_WRITE);
+                Event::set($stream, $func, null, SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
             } else {
-                Event::set($stream, $func, null, \SWOOLE_EVENT_READ);
+                Event::set($stream, $func, null, SWOOLE_EVENT_READ);
             }
         }
         $this->readEvents[$fd] = $stream;
@@ -142,7 +147,7 @@ class Swoole implements EventInterface
             Event::del($stream);
             return true;
         }
-        Event::set($stream, null, null, \SWOOLE_EVENT_WRITE);
+        Event::set($stream, null, null, SWOOLE_EVENT_WRITE);
         return true;
     }
 
@@ -153,12 +158,12 @@ class Swoole implements EventInterface
     {
         $fd = (int)$stream;
         if (!isset($this->readEvents[$fd]) && !isset($this->writeEvents[$fd])) {
-            Event::add($stream, null, $func, \SWOOLE_EVENT_WRITE);
+            Event::add($stream, null, $func, SWOOLE_EVENT_WRITE);
         } else {
             if (isset($this->readEvents[$fd])) {
-                Event::set($stream, null, $func, \SWOOLE_EVENT_WRITE | \SWOOLE_EVENT_READ);
+                Event::set($stream, null, $func, SWOOLE_EVENT_WRITE | SWOOLE_EVENT_READ);
             } else {
-                Event::set($stream, null, $func, \SWOOLE_EVENT_WRITE);
+                Event::set($stream, null, $func, SWOOLE_EVENT_WRITE);
             }
         }
         $this->writeEvents[$fd] = $stream;
@@ -178,7 +183,7 @@ class Swoole implements EventInterface
             Event::del($stream);
             return true;
         }
-        Event::set($stream, null, null, \SWOOLE_EVENT_READ);
+        Event::set($stream, null, null, SWOOLE_EVENT_READ);
         return true;
     }
 
@@ -195,7 +200,8 @@ class Swoole implements EventInterface
      */
     public function offSignal(int $signal): bool
     {
-        return Process::signal($signal, function () {});
+        return Process::signal($signal, function () {
+        });
     }
 
     /**
@@ -224,7 +230,7 @@ class Swoole implements EventInterface
     public function stop()
     {
         Event::exit();
-        \posix_kill(posix_getpid(), SIGINT);
+        posix_kill(posix_getpid(), SIGINT);
     }
 
     /**
@@ -234,7 +240,7 @@ class Swoole implements EventInterface
      */
     public function getTimerCount(): int
     {
-        return \count($this->eventTimer);
+        return count($this->eventTimer);
     }
 
     /**

+ 17 - 14
src/Events/Swow.php

@@ -7,6 +7,8 @@ use Swow\Coroutine;
 use Swow\Signal;
 use Swow\SignalException;
 use Throwable;
+use function count;
+use function is_resource;
 use function max;
 use function msleep;
 use function stream_poll_one;
@@ -54,7 +56,7 @@ class Swow implements EventInterface
      */
     public function getTimerCount(): int
     {
-        return \count($this->eventTimer);
+        return count($this->eventTimer);
     }
 
     /**
@@ -62,15 +64,15 @@ class Swow implements EventInterface
      */
     public function delay(float $delay, callable $func, array $args = []): int
     {
-        $t = (int) ($delay * 1000);
+        $t = (int)($delay * 1000);
         $t = max($t, 1);
         $that = $this;
         $coroutine = Coroutine::run(function () use ($t, $func, $args, $that): void {
             msleep($t);
             unset($this->eventTimer[Coroutine::getCurrent()->getId()]);
             try {
-                $func(...(array) $args);
-            } catch (\Throwable $e) {
+                $func(...$args);
+            } catch (Throwable $e) {
                 $that->error($e);
             }
         });
@@ -84,15 +86,15 @@ class Swow implements EventInterface
      */
     public function repeat(float $interval, callable $func, array $args = []): int
     {
-        $t = (int) ($interval * 1000);
+        $t = (int)($interval * 1000);
         $t = max($t, 1);
         $that = $this;
         $coroutine = Coroutine::run(static function () use ($t, $func, $args, $that): void {
             while (true) {
                 msleep($t);
                 try {
-                    $func(...(array) $args);
-                } catch (\Throwable $e) {
+                    $func(...$args);
+                } catch (Throwable $e) {
                     $that->error($e);
                 }
             }
@@ -141,7 +143,7 @@ class Swow implements EventInterface
      */
     public function onReadable($stream, callable $func)
     {
-        $fd = (int) $stream;
+        $fd = (int)$stream;
         if (isset($this->readEvents[$fd])) {
             $this->offReadable($stream);
         }
@@ -149,7 +151,7 @@ class Swow implements EventInterface
             try {
                 $this->readEvents[$fd] = Coroutine::getCurrent();
                 while (true) {
-                    if (!\is_resource($stream)) {
+                    if (!is_resource($stream)) {
                         $this->offReadable($stream);
                         break;
                     }
@@ -177,7 +179,7 @@ class Swow implements EventInterface
     public function offReadable($stream): bool
     {
         // 在当前协程执行 $coroutine->kill() 会导致不可预知问题,所以没有使用$coroutine->kill()
-        $fd = (int) $stream;
+        $fd = (int)$stream;
         if (isset($this->readEvents[$fd])) {
             unset($this->readEvents[$fd]);
             return true;
@@ -190,7 +192,7 @@ class Swow implements EventInterface
      */
     public function onWritable($stream, callable $func)
     {
-        $fd = (int) $stream;
+        $fd = (int)$stream;
         if (isset($this->writeEvents[$fd])) {
             $this->offWritable($stream);
         }
@@ -221,7 +223,7 @@ class Swow implements EventInterface
      */
     public function offWritable($stream): bool
     {
-        $fd = (int) $stream;
+        $fd = (int)$stream;
         if (isset($this->writeEvents[$fd])) {
             unset($this->writeEvents[$fd]);
             return true;
@@ -244,7 +246,8 @@ class Swow implements EventInterface
                         break;
                     }
                     $func($signal);
-                } catch (SignalException) {}
+                } catch (SignalException) {
+                }
             }
         });
     }
@@ -278,7 +281,7 @@ class Swow implements EventInterface
     {
         Coroutine::killAll();
     }
-    
+
     /**
      * {@inheritdoc}
      */