浏览代码

format session

walkor 2 年之前
父节点
当前提交
300640b229

+ 4 - 1
src/Protocols/Http/Chunk.php

@@ -15,6 +15,9 @@
 namespace Workerman\Protocols\Http;
 
 
+use function dechex;
+use function strlen;
+
 /**
  * Class Chunk
  * @package Workerman\Protocols\Http
@@ -45,6 +48,6 @@ class Chunk
      */
     public function __toString()
     {
-        return \dechex(\strlen($this->buffer)) . "\r\n$this->buffer\r\n";
+        return dechex(strlen($this->buffer)) . "\r\n$this->buffer\r\n";
     }
 }

+ 38 - 23
src/Protocols/Http/Session/FileSessionHandler.php

@@ -15,6 +15,21 @@
 namespace Workerman\Protocols\Http\Session;
 
 use Workerman\Protocols\Http\Session;
+use function clearstatcache;
+use function file_get_contents;
+use function file_put_contents;
+use function filemtime;
+use function glob;
+use function is_dir;
+use function is_file;
+use function mkdir;
+use function rename;
+use function session_save_path;
+use function strlen;
+use function sys_get_temp_dir;
+use function time;
+use function touch;
+use function unlink;
 
 /**
  * Class FileSessionHandler
@@ -41,9 +56,9 @@ class FileSessionHandler implements SessionHandlerInterface
      */
     public static function init()
     {
-        $savePath = @\session_save_path();
+        $savePath = @session_save_path();
         if (!$savePath || str_starts_with($savePath, 'tcp://')) {
-            $savePath = \sys_get_temp_dir();
+            $savePath = sys_get_temp_dir();
         }
         static::sessionSavePath($savePath);
     }
@@ -73,13 +88,13 @@ class FileSessionHandler implements SessionHandlerInterface
     public function read(string $sessionId): string
     {
         $sessionFile = static::sessionFile($sessionId);
-        \clearstatcache();
-        if (\is_file($sessionFile)) {
-            if (\time() - \filemtime($sessionFile) > Session::$lifetime) {
-                \unlink($sessionFile);
+        clearstatcache();
+        if (is_file($sessionFile)) {
+            if (time() - filemtime($sessionFile) > Session::$lifetime) {
+                unlink($sessionFile);
                 return '';
             }
-            $data = \file_get_contents($sessionFile);
+            $data = file_get_contents($sessionFile);
             return $data ?: '';
         }
         return '';
@@ -91,10 +106,10 @@ class FileSessionHandler implements SessionHandlerInterface
     public function write(string $sessionId, string $sessionData): bool
     {
         $tempFile = static::$sessionSavePath . uniqid(bin2hex(random_bytes(8)), true);
-        if (!\file_put_contents($tempFile, $sessionData)) {
+        if (!file_put_contents($tempFile, $sessionData)) {
             return false;
         }
-        return \rename($tempFile, static::sessionFile($sessionId));
+        return rename($tempFile, static::sessionFile($sessionId));
     }
 
     /**
@@ -103,21 +118,21 @@ class FileSessionHandler implements SessionHandlerInterface
      * @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
      * @see https://www.php.net/manual/zh/function.touch.php
      *
-     * @param string $id Session id.
+     * @param string $sessionId Session id.
      * @param string $data Session Data.
      *
      * @return bool
      */
-    public function updateTimestamp(string $id, string $data = ""): bool
+    public function updateTimestamp(string $sessionId, string $data = ""): bool
     {
-        $sessionFile = static::sessionFile($id);
+        $sessionFile = static::sessionFile($sessionId);
         if (!file_exists($sessionFile)) {
             return false;
         }
         // set file modify time to current time
-        $setModifyTime = \touch($sessionFile);
+        $setModifyTime = touch($sessionFile);
         // clear file stat cache
-        \clearstatcache();
+        clearstatcache();
         return $setModifyTime;
     }
 
@@ -135,8 +150,8 @@ class FileSessionHandler implements SessionHandlerInterface
     public function destroy(string $sessionId): bool
     {
         $sessionFile = static::sessionFile($sessionId);
-        if (\is_file($sessionFile)) {
-            \unlink($sessionFile);
+        if (is_file($sessionFile)) {
+            unlink($sessionFile);
         }
         return true;
     }
@@ -146,10 +161,10 @@ class FileSessionHandler implements SessionHandlerInterface
      */
     public function gc(int $maxLifetime): bool
     {
-        $timeNow = \time();
-        foreach (\glob(static::$sessionSavePath . static::$sessionFilePrefix . '*') as $file) {
-            if (\is_file($file) && $timeNow - \filemtime($file) > $maxLifetime) {
-                \unlink($file);
+        $timeNow = time();
+        foreach (glob(static::$sessionSavePath . static::$sessionFilePrefix . '*') as $file) {
+            if (is_file($file) && $timeNow - filemtime($file) > $maxLifetime) {
+                unlink($file);
             }
         }
         return true;
@@ -175,12 +190,12 @@ class FileSessionHandler implements SessionHandlerInterface
     public static function sessionSavePath(string $path): string
     {
         if ($path) {
-            if ($path[\strlen($path) - 1] !== DIRECTORY_SEPARATOR) {
+            if ($path[strlen($path) - 1] !== DIRECTORY_SEPARATOR) {
                 $path .= DIRECTORY_SEPARATOR;
             }
             static::$sessionSavePath = $path;
-            if (!\is_dir($path)) {
-                \mkdir($path, 0777, true);
+            if (!is_dir($path)) {
+                mkdir($path, 0777, true);
             }
         }
         return $path;

+ 4 - 3
src/Protocols/Http/Session/RedisClusterSessionHandler.php

@@ -14,7 +14,8 @@
 
 namespace Workerman\Protocols\Http\Session;
 
-use Workerman\Protocols\Http\Session;
+use Redis;
+use RedisCluster;
 use RedisClusterException;
 
 class RedisClusterSessionHandler extends RedisSessionHandler
@@ -33,11 +34,11 @@ class RedisClusterSessionHandler extends RedisSessionHandler
         if ($auth) {
             $args[] = $auth;
         }
-        $this->redis = new \RedisCluster(...$args);
+        $this->redis = new RedisCluster(...$args);
         if (empty($config['prefix'])) {
             $config['prefix'] = 'redis_session_';
         }
-        $this->redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
+        $this->redis->setOption(Redis::OPT_PREFIX, $config['prefix']);
     }
 
     /**

+ 6 - 5
src/Protocols/Http/Session/RedisSessionHandler.php

@@ -15,10 +15,11 @@
 namespace Workerman\Protocols\Http\Session;
 
 use Redis;
+use RedisException;
+use RuntimeException;
 use Throwable;
 use Workerman\Protocols\Http\Session;
 use Workerman\Timer;
-use RedisException;
 
 /**
  * Class RedisSessionHandler
@@ -52,7 +53,7 @@ class RedisSessionHandler implements SessionHandlerInterface
     public function __construct(array $config)
     {
         if (false === extension_loaded('redis')) {
-            throw new \RuntimeException('Please install redis extension.');
+            throw new RuntimeException('Please install redis extension.');
         }
 
         if (!isset($config['timeout'])) {
@@ -74,7 +75,7 @@ class RedisSessionHandler implements SessionHandlerInterface
 
         $this->redis = new Redis();
         if (false === $this->redis->connect($config['host'], $config['port'], $config['timeout'])) {
-            throw new \RuntimeException("Redis connect {$config['host']}:{$config['port']} fail.");
+            throw new RuntimeException("Redis connect {$config['host']}:{$config['port']} fail.");
         }
         if (!empty($config['auth'])) {
             $this->redis->auth($config['auth']);
@@ -125,9 +126,9 @@ class RedisSessionHandler implements SessionHandlerInterface
     /**
      * {@inheritdoc}
      */
-    public function updateTimestamp(string $id, string $data = ""): bool
+    public function updateTimestamp(string $sessionId, string $data = ""): bool
     {
-        return true === $this->redis->expire($id, Session::$lifetime);
+        return true === $this->redis->expire($sessionId, Session::$lifetime);
     }
 
     /**