From 834538ad354771d12aa07211615549111424924e Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 11:05:07 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=BE=85=E5=8A=A9?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functions.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/functions.php b/src/functions.php index 84cfbfe..92fc312 100644 --- a/src/functions.php +++ b/src/functions.php @@ -44,3 +44,37 @@ function env($key, $default = null) return container()->get(\Simps\Utils\Env::class)->get($key, $default); } } + +if(!function_exists('printEol')){ + /** + * printEol + * @param $expression + */ + function printEol($expression) + { + print_r($expression); + echo PHP_EOL; + } +} + +if(!function_exists('echoSuccess')){ + /** + * printEol + * @param $msg + */ + function echoSuccess($msg) + { + printEol('[' . date('Y-m-d H:i:s') . '] [INFO] ' . "\033[32m{$msg}\033[0m"); + } +} + +if(!function_exists('echoError')){ + /** + * printEol + * @param $msg + */ + function echoError($msg) + { + printEol('[' . date('Y-m-d H:i:s') . '] [ERROR] ' . "\033[31m{$msg}\033[0m"); + } +} \ No newline at end of file From 1d299e6dcf85a70bdc1dc2a56d9bff7c8a9999b7 Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 11:05:17 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8A=A9=E6=89=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FileHelper.php | 192 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) create mode 100644 src/FileHelper.php diff --git a/src/FileHelper.php b/src/FileHelper.php new file mode 100644 index 0000000..dc40b6a --- /dev/null +++ b/src/FileHelper.php @@ -0,0 +1,192 @@ + $value) { + if (substr($value, -1) == '/') { + mkdir($value); + } else { + file_put_contents($value, ''); + } + } + } + + /** + * removeDirectory + * @param $dir + * @param array $options + */ + public static function removeDirectory($dir, $options = []) + { + if (!is_dir($dir)) { + return; + } + if (!empty($options['traverseSymlinks']) || !is_link($dir)) { + if (!($handle = opendir($dir))) { + return; + } + while (($file = readdir($handle)) !== false) { + if ($file === '.' || $file === '..') { + continue; + } + $path = $dir . DIRECTORY_SEPARATOR . $file; + if (is_dir($path)) { + static::removeDirectory($path, $options); + } else { + static::unlink($path); + } + } + closedir($handle); + } + if (is_link($dir)) { + static::unlink($dir); + } else { + rmdir($dir); + } + } + + /** + * unlink + * @param $path + * @return bool + */ + public static function unlink($path) + { + $isWindows = DIRECTORY_SEPARATOR === '\\'; + + if (!$isWindows) { + return unlink($path); + } + + if (is_link($path) && is_dir($path)) { + return rmdir($path); + } + + try { + return unlink($path); + } catch (\Exception $e) { + return false; + } + } + + /** + * getMimeType + * @param $file + * @return mixed|null + */ + public static function getMimeType($file) + { + if (extension_loaded('fileinfo')) { + $info = finfo_open(FILEINFO_MIME_TYPE); + if ($info) { + $result = finfo_file($info, $file); + finfo_close($info); + + if ($result !== false) { + return $result; + } + } + } + return null; + } +} From 0182dbc6638bb7183ade0ad64054ac7a1a811fbb Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 11:05:27 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E6=97=B6=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BD=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AutoReload.php | 166 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 src/AutoReload.php diff --git a/src/AutoReload.php b/src/AutoReload.php new file mode 100644 index 0000000..58a2030 --- /dev/null +++ b/src/AutoReload.php @@ -0,0 +1,166 @@ + [ + * [\Simps\Utils\AutoReload::class, 'start'], + * ], + * Class AutoReload + * @package Simps\Utils + */ +class AutoReload +{ + + /** + * @var Server + */ + protected $server; + + /** + * @var Process + */ + protected $hotReloadProcess; + + /** + * 文件类型 + * @var array + */ + protected $reloadFileTypes = ['.php' => true]; + + /** + * 监听文件 + * @var array + */ + protected $lastFileList = []; + + /** + * 是否正在重载 + */ + protected $reloading = false; + + + /** + * AutoReload constructor. + * @param Server $server + */ + public function __construct(Server $server) + { + $this->server = $server; + } + + /** + * start + * @return Process + */ + public static function start(Server $server) + { + $autoLoad = new self($server); + + $autoLoad->hotReloadProcess = new Process([$autoLoad,'hotReloadProcessCallBack'], false, 2, false); + + return $autoLoad->hotReloadProcess; + } + + /** + * hotReloadProcessCallBack + * @param Process $worker + */ + public function hotReloadProcessCallBack(Process $worker) + { + + $this->hotReloadProcess->signal(SIGUSR1, [$this,'signalCallBack']); + + $this->run(BASE_PATH); + + $currentOS = PHP_OS; + + $currentPID = $this->hotReloadProcess->pid; + + echoSuccess("自动重载代码初始化({$currentOS})PID: {$currentPID} ..."); + } + + public function signalCallBack() + { + + echoSuccess( "重载时间: " . date('Y-m-d H:i:s')); + + $res = $this->server->reload(); + + $res?echoSuccess('重载成功'):echoError('重载失败'); + + } + + /** + * sendReloadSignal + */ + protected function sendReloadSignal() + { + Process::kill($this->hotReloadProcess->pid,SIGUSR1); + } + + /** + * 添加文件类型 + * addFileType + * @param $type + * @return $this + */ + public function addFileType($type) + { + $type = trim($type, '.'); + + $this->reloadFileTypes['.' . $type] = true; + + return $this; + } + + /** + * watch + * @param $dir + */ + public function watch($dir) + { + $files = FileHelper::scanDirectory($dir); + + $dirtyList = []; + + foreach ($files as $file) { + //检测文件类型 + $fileType = strrchr($file, '.'); + if (isset($this->reloadFileTypes[$fileType])) { + $fileInfo = new \SplFileInfo($file); + $mtime = $fileInfo->getMTime(); + $inode = $fileInfo->getInode(); + $dirtyList[$inode] = $mtime; + } + } + + // 当数组中出现脏值则发生了文件变更 + if (array_diff_assoc($dirtyList, $this->lastFileList)) { + $this->lastFileList = $dirtyList; + if ($this->reloading) { + $this->sendReloadSignal(); + } + } + + $this->reloading = true; + } + + /** + * run + */ + public function run($dir) + { + $this->watch($dir); + + Timer::tick(1000, function () use ($dir) { + $this->watch($dir); + }); + } +} \ No newline at end of file From c7db17ebda3639d51fd117705eca1b3646336f72 Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 11:06:10 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FileHelper.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FileHelper.php b/src/FileHelper.php index dc40b6a..f1626ac 100644 --- a/src/FileHelper.php +++ b/src/FileHelper.php @@ -26,6 +26,8 @@ public static function mkdir($catalogue) * * @param $path * @param $content + * @param int $flags + * @param null $context * @return bool|int */ public static function writeLog($path, $content,$flags = FILE_APPEND, $context = null) From 31cd0d409e88f7f4b827c6ef28da4cca83dcd4dd Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 17:50:29 +0800 Subject: [PATCH 5/5] composer cs-fix --- src/AutoReload.php | 72 +++++++++++++++++++++++----------------------- src/FileHelper.php | 65 +++++++++++++++++++++++------------------ src/functions.php | 14 ++++----- 3 files changed, 80 insertions(+), 71 deletions(-) diff --git a/src/AutoReload.php b/src/AutoReload.php index 58a2030..d091814 100644 --- a/src/AutoReload.php +++ b/src/AutoReload.php @@ -1,7 +1,13 @@ [ * [\Simps\Utils\AutoReload::class, 'start'], * ], - * Class AutoReload - * @package Simps\Utils + * Class AutoReload. */ class AutoReload { - /** * @var Server */ @@ -29,26 +33,25 @@ class AutoReload protected $hotReloadProcess; /** - * 文件类型 + * 文件类型. * @var array */ protected $reloadFileTypes = ['.php' => true]; /** - * 监听文件 + * 监听文件. * @var array */ - protected $lastFileList = []; + protected $lastFileList = []; /** - * 是否正在重载 + * 是否正在重载. + * @var bool */ protected $reloading = false; - /** * AutoReload constructor. - * @param Server $server */ public function __construct(Server $server) { @@ -56,30 +59,28 @@ public function __construct(Server $server) } /** - * start + * start. * @return Process */ public static function start(Server $server) { $autoLoad = new self($server); - $autoLoad->hotReloadProcess = new Process([$autoLoad,'hotReloadProcessCallBack'], false, 2, false); + $autoLoad->hotReloadProcess = new Process([$autoLoad, 'hotReloadProcessCallBack'], false, 2, false); return $autoLoad->hotReloadProcess; } /** - * hotReloadProcessCallBack - * @param Process $worker + * hotReloadProcessCallBack. */ public function hotReloadProcessCallBack(Process $worker) { - - $this->hotReloadProcess->signal(SIGUSR1, [$this,'signalCallBack']); + $this->hotReloadProcess->signal(SIGUSR1, [$this, 'signalCallBack']); $this->run(BASE_PATH); - $currentOS = PHP_OS; + $currentOS = PHP_OS; $currentPID = $this->hotReloadProcess->pid; @@ -88,26 +89,16 @@ public function hotReloadProcessCallBack(Process $worker) public function signalCallBack() { - - echoSuccess( "重载时间: " . date('Y-m-d H:i:s')); + echoSuccess('重载时间: ' . date('Y-m-d H:i:s')); $res = $this->server->reload(); - $res?echoSuccess('重载成功'):echoError('重载失败'); - - } - - /** - * sendReloadSignal - */ - protected function sendReloadSignal() - { - Process::kill($this->hotReloadProcess->pid,SIGUSR1); + $res ? echoSuccess('重载成功') : echoError('重载失败'); } /** * 添加文件类型 - * addFileType + * addFileType. * @param $type * @return $this */ @@ -121,8 +112,8 @@ public function addFileType($type) } /** - * watch - * @param $dir + * watch. + * @param $dir */ public function watch($dir) { @@ -134,8 +125,8 @@ public function watch($dir) //检测文件类型 $fileType = strrchr($file, '.'); if (isset($this->reloadFileTypes[$fileType])) { - $fileInfo = new \SplFileInfo($file); - $mtime = $fileInfo->getMTime(); + $fileInfo = new \SplFileInfo($file); + $mtime = $fileInfo->getMTime(); $inode = $fileInfo->getInode(); $dirtyList[$inode] = $mtime; } @@ -153,7 +144,8 @@ public function watch($dir) } /** - * run + * run. + * @param mixed $dir */ public function run($dir) { @@ -163,4 +155,12 @@ public function run($dir) $this->watch($dir); }); } -} \ No newline at end of file + + /** + * sendReloadSignal. + */ + protected function sendReloadSignal() + { + Process::kill($this->hotReloadProcess->pid, SIGUSR1); + } +} diff --git a/src/FileHelper.php b/src/FileHelper.php index f1626ac..2097950 100644 --- a/src/FileHelper.php +++ b/src/FileHelper.php @@ -1,20 +1,26 @@