Explorar o código

Removed PHPStrom attributes, fixed minor typos and changed DEBUG to USER mode.

Ako Tulu %!s(int64=2) %!d(string=hai) anos
pai
achega
247d87ca29

+ 1 - 1
src/Connection/AsyncTcpConnection.php

@@ -301,7 +301,7 @@ class AsyncTcpConnection extends TcpConnection
             }
             return;
         }
-        // Add socket to global event loop waiting connection is successfully established or faild.
+        // Add socket to global event loop waiting connection is successfully established or failed.
         $this->eventLoop->onWritable($this->socket, [$this, 'checkConnection']);
         // For windows.
         if (DIRECTORY_SEPARATOR === '\\' && method_exists($this->eventLoop, 'onExcept')) {

+ 8 - 10
src/Connection/TcpConnection.php

@@ -15,7 +15,6 @@ declare(strict_types=1);
 
 namespace Workerman\Connection;
 
-use JetBrains\PhpStorm\Pure;
 use JsonSerializable;
 use stdClass;
 use Throwable;
@@ -898,7 +897,6 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      *
      * return bool.
      */
-    #[Pure]
     public function isIpV4(): bool
     {
         if ($this->transport === 'unix') {
@@ -912,7 +910,6 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      *
      * return bool.
      */
-    #[Pure]
     public function isIpV6(): bool
     {
         if ($this->transport === 'unix') {
@@ -1038,8 +1035,8 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @param mixed $value
      */
-    public static function enableCache(bool $value = true)
-    {
+    public static function enableCache(bool $value = true): void
+	{
         static::$enableCache = $value;
     }
 
@@ -1065,11 +1062,12 @@ class TcpConnection extends ConnectionInterface implements JsonSerializable
         ];
     }
 
-    /**
-     * Destruct.
-     *
-     * @return void
-     */
+	/**
+	 * Destruct.
+	 *
+	 * @return void
+	 * @throws Throwable
+	 */
     public function __destruct()
     {
         static $mod;

+ 0 - 8
src/Connection/UdpConnection.php

@@ -16,8 +16,6 @@ declare(strict_types=1);
 
 namespace Workerman\Connection;
 
-use JetBrains\PhpStorm\ArrayShape;
-use JetBrains\PhpStorm\Pure;
 use JsonSerializable;
 use Workerman\Protocols\ProtocolInterface;
 use function stream_socket_get_name;
@@ -191,7 +189,6 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      *
      * return bool.
      */
-    #[Pure]
     public function isIpV4(): bool
     {
         if ($this->transport === 'unix') {
@@ -205,7 +202,6 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      *
      * return bool.
      */
-    #[Pure]
     public function isIpV6(): bool
     {
         if ($this->transport === 'unix') {
@@ -229,10 +225,6 @@ class UdpConnection extends ConnectionInterface implements JsonSerializable
      *
      * @return array
      */
-    #[ArrayShape([
-        'transport' => "string", 'getRemoteIp' => "string", 'remotePort' => "int", 'getRemoteAddress' => "string",
-        'getLocalIp' => "string", 'getLocalPort' => "int", 'getLocalAddress' => "string", 'isIpV4' => "bool", 'isIpV6' => "bool"]
-    )]
     public function jsonSerialize(): array
     {
         return [

+ 1 - 1
src/Events/Select.php

@@ -380,7 +380,7 @@ class Select implements EventInterface
             if ($read || $write || $except) {
                 // Waiting read/write/signal/timeout events.
                 try {
-                    @stream_select($read, $write, $except, 0, $this->selectTimeout);
+                    stream_select($read, $write, $except, 0, $this->selectTimeout);
                 } catch (Throwable) {
                 }
             } else {

+ 8 - 8
src/Protocols/Websocket.php

@@ -236,14 +236,14 @@ class Websocket
         return 0;
     }
 
-    /**
-     * Websocket encode.
-     *
-     * @param mixed $buffer
-     * @param TcpConnection $connection
-     * @return string
-     * @throws Exception
-     */
+	/**
+	 * Websocket encode.
+	 *
+	 * @param mixed $buffer
+	 * @param TcpConnection $connection
+	 * @return string
+	 * @throws Throwable
+	 */
     public static function encode(mixed $buffer, TcpConnection $connection): string
     {
         if (!is_scalar($buffer)) {

+ 57 - 51
src/Worker.php

@@ -620,7 +620,7 @@ class Worker
                 @mkdir(dirname(static::$logFile), 0777, true);
             }
             touch(static::$logFile);
-            chmod(static::$logFile, 0622);
+            chmod(static::$logFile, 0644);
         }
 
         // State.
@@ -761,7 +761,7 @@ class Worker
     }
 
     /**
-     * Get unix user of current porcess.
+     * Get unix user of current process.
      *
      * @return string
      */
@@ -792,7 +792,7 @@ class Worker
 
         //show version
         $lineVersion = 'Workerman version:' . static::VERSION . str_pad('PHP version:', 16, ' ', STR_PAD_LEFT) . PHP_VERSION . str_pad('Event-loop:', 16, ' ', STR_PAD_LEFT) . static::getEventLoopName() . PHP_EOL;
-        !defined('LINE_VERSIOIN_LENGTH') && define('LINE_VERSIOIN_LENGTH', strlen($lineVersion));
+        !defined('LINE_VERSION_LENGTH') && define('LINE_VERSION_LENGTH', strlen($lineVersion));
         $totalLength = static::getSingleLineTotalLength();
         $lineOne = '<n>' . str_pad('<w> WORKERMAN </w>', $totalLength + strlen('<w></w>'), '-', STR_PAD_BOTH) . '</n>' . PHP_EOL;
         $lineTwo = str_pad('<w> WORKERS </w>', $totalLength + strlen('<w></w>'), '-', STR_PAD_BOTH) . PHP_EOL;
@@ -827,9 +827,11 @@ class Worker
 
         if (static::$daemonize) {
             static::safeEcho('Input "php ' . basename(static::$startFile) . ' stop" to stop. Start success.' . "\n\n");
+        } else if (!empty(static::$command)) {
+			static::safeEcho("Start success.\n"); // Workerman used as library
         } else {
-            static::safeEcho("Press Ctrl+C to stop. Start success.\n");
-        }
+			static::safeEcho("Press Ctrl+C to stop. Start success.\n");
+		}
     }
 
     /**
@@ -867,8 +869,8 @@ class Worker
         }
 
         //Keep beauty when show less columns
-        !defined('LINE_VERSIOIN_LENGTH') && define('LINE_VERSIOIN_LENGTH', 0);
-        $totalLength <= LINE_VERSIOIN_LENGTH && $totalLength = LINE_VERSIOIN_LENGTH;
+        !defined('LINE_VERSION_LENGTH') && define('LINE_VERSION_LENGTH', 0);
+        $totalLength <= LINE_VERSION_LENGTH && $totalLength = LINE_VERSION_LENGTH;
 
         return $totalLength;
     }
@@ -886,7 +888,7 @@ class Worker
 
         // Check argv;
         $startFile = basename(static::$startFile);
-        $usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in DEBUG mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
+        $usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in USER mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
         $availableCommands = [
             'start',
             'stop',
@@ -919,7 +921,7 @@ class Worker
             if ($mode === '-d' || static::$daemonize) {
                 $modeStr = 'in DAEMON mode';
             } else {
-                $modeStr = 'in DEBUG mode';
+                $modeStr = 'in USER mode';
             }
         }
         static::log("Workerman[$startFile] $command $modeStr");
@@ -973,9 +975,9 @@ class Worker
                 }
                 // Master process will send SIGIO signal to all child processes.
                 posix_kill($masterPid, SIGIO);
-                // Waiting amoment.
+                // Waiting a moment.
                 usleep(500000);
-                // Display statisitcs data from a disk file.
+                // Display statistics data from a disk file.
                 if (is_readable($statisticsFile)) {
                     readfile($statisticsFile);
                 }
@@ -1005,7 +1007,7 @@ class Worker
                             static::log("Workerman[$startFile] stop fail");
                             exit;
                         }
-                        // Waiting amoment.
+                        // Waiting a moment.
                         usleep(10000);
                         continue;
                     }
@@ -1165,12 +1167,12 @@ class Worker
         }
     }
 
-    /**
-     * Signal handler.
-     *
-     * @param int $signal
-     * @throws Exception
-     */
+	/**
+	 * Signal handler.
+	 *
+	 * @param int $signal
+	 * @throws Throwable
+	 */
     public static function signalHandler(int $signal): void
     {
         switch ($signal) {
@@ -1298,9 +1300,9 @@ class Worker
         }
 
         static::$masterPid = posix_getpid();
-        if (false === file_put_contents(static::$pidFile, static::$masterPid)) {
-            throw new RuntimeException('can not save pid to ' . static::$pidFile);
-        }
+		if (false === file_put_contents(static::$pidFile, static::$masterPid)) {
+			throw new RuntimeException('can not save pid to ' . static::$pidFile);
+		}
     }
 
     /**
@@ -1784,12 +1786,12 @@ class Worker
         exit(0);
     }
 
-    /**
-     * Execute reload.
-     *
-     * @return void
-     * @throws Exception
-     */
+	/**
+	 * Execute reload.
+	 *
+	 * @return void
+	 * @throws Throwable
+	 */
     protected static function reload(): void
     {
         // For master process.
@@ -1866,12 +1868,13 @@ class Worker
         }
     }
 
-    /**
-     * Stop all.
-     *
-     * @param int $code
-     * @param mixed $log
-     */
+	/**
+	 * Stop all.
+	 *
+	 * @param int $code
+	 * @param mixed $log
+	 * @throws Throwable
+	 */
     public static function stopAll(int $code = 0, mixed $log = ''): void
     {
         if ($log) {
@@ -2448,11 +2451,12 @@ class Worker
         }
     }
 
-    /**
-     * Stop current worker instance.
-     *
-     * @return void
-     */
+	/**
+	 * Stop current worker instance.
+	 *
+	 * @return void
+	 * @throws Throwable
+	 */
     public function stop(): void
     {
         // Try to emit onWorkerStop callback.
@@ -2466,7 +2470,7 @@ class Worker
         // Remove listener for server socket.
         $this->unlisten();
         // Close all connections for the worker.
-        if (!static::$gracefulStop) {
+        if (static::$gracefulStop) {
             foreach ($this->connections as $connection) {
                 $connection->close();
             }
@@ -2481,12 +2485,13 @@ class Worker
         $this->onMessage = $this->onClose = $this->onError = $this->onBufferDrain = $this->onBufferFull = null;
     }
 
-    /**
-     * Accept a connection.
-     *
-     * @param resource $socket
-     * @return void
-     */
+	/**
+	 * Accept a connection.
+	 *
+	 * @param resource $socket
+	 * @return void
+	 * @throws Throwable
+	 */
     public function acceptTcpConnection($socket): void
     {
         // Accept a connection on server socket.
@@ -2522,12 +2527,13 @@ class Worker
         }
     }
 
-    /**
-     * For udp package.
-     *
-     * @param resource $socket
-     * @return bool
-     */
+	/**
+	 * For udp package.
+	 *
+	 * @param resource $socket
+	 * @return bool
+	 * @throws Throwable
+	 */
     public function acceptUdpConnection($socket): bool
     {
         set_error_handler(function () {