## ** win环境热更新代码** nodemon 热更新PHP代码 使用文档 https://sergeyzhuk.me/2019/09/16/live-reload-php-applications/ ~~~ 1. 安装nodemon npm install -g nodemon 2. 配置nodemon.json文件 { "verbose": false, "ignore": [ ".git", ".idea" ], "execMap": { "php": "php" }, "restartable": "r", "ext": "php" } 3. 执行 nodemon start.php ~~~ ### 在phpstorm中配置热更新 ![](https://img.kancloud.cn/c7/14/c714298796368c274cd5141cae9dcb80_1107x759.png) ![](https://img.kancloud.cn/9e/5a/9e5a4a9828a1844b6d48e32918f235f1_987x912.png) ``` ~~~ { "name": "api-webman", "version": "1.0.0", "dependencies": { }, "scripts":{ "debug":"nodemon start.php" } } ~~~ ``` 下面代码详细文档地址 https://www.workerman.net/q/7679 ``` load(); } else { Dotenv::createMutable(base\_path())->load(); } } Config::load(config\_path(), \['route', 'container'\]); if ($timezone \= config('app.default\_timezone')) { date\_default\_timezone\_set($timezone); } $config \= config('server'); $pUrl \= parse\_url($config\['listen'\]); $localUrl \= 'http://127.0.0.1'.(isset($pUrl\['port'\])?':'.$pUrl\['port'\]:''); class M { /\*\* \* @var array \*/ protected $\_paths \= \[\]; /\*\* \* @var array \*/ protected $\_extensions \= \[\]; public function \_\_construct($monitor\_dir, $monitor\_extensions) { $this\->\_extensions \= $monitor\_extensions; $this\->\_paths \= $monitor\_dir; exec('php -v ', $out, $var); $this\->checkMode \= ($var \=== 0); } public function checkAll() { foreach ($this\->\_paths as $path) { if ($this\->check\_files\_change($path)) { return true; } } return false; } public function check\_files\_change($monitor\_dir) { static $last\_mtime; if (!$last\_mtime) { $last\_mtime \= time(); } clearstatcache(); if (!is\_dir($monitor\_dir)) { if (!is\_file($monitor\_dir)) { return false; } $iterator \= \[new \\SplFileInfo($monitor\_dir)\]; } else { // recursive traversal directory $dir\_iterator \= new \\RecursiveDirectoryIterator($monitor\_dir); $iterator \= new \\RecursiveIteratorIterator($dir\_iterator); } foreach ($iterator as $file) { /\*\* var SplFileInfo $file \*/ if (is\_dir($file)) { continue; } if ($file\->getFilename() \=== 'log.php') { // echo ($file->getFilename()) . PHP\_EOL; // echo date('Y-m-d H:i:s', $file->getMTime()) . PHP\_EOL; } // check mtime if ($last\_mtime getMTime() && in\_array($file\->getExtension(), $this\->\_extensions, true)) { $var \= 0; if ($this\->checkMode) { $phpBin \= PHP\_BINARY; exec('"' . $phpBin . '" -l ' . $file, $out, $var); } else { exec(PHP\_BINARY . " -l " . $file, $out, $var); } if ($var) { $last\_mtime \= $file\->getMTime(); continue; } echo $file . " update and reload\\n"; // send SIGUSR1 signal to master process for reload $last\_mtime \= $file\->getMTime(); return true; } } return false; } } $m \= new M(\[ \_\_DIR\_\_ . '/app', \_\_DIR\_\_ . '/config', \_\_DIR\_\_ . '/database', \_\_DIR\_\_ . '/process', \_\_DIR\_\_ . '/resource', \_\_DIR\_\_ . '/support', \], \[ 'php', 'html', 'htm', 'env' \]); $phpBin \= '"' . PHP\_BINARY . '"'; $descriptorspec \= \[STDIN, STDOUT, STDOUT\]; $run \= 'php start.php start'; $resource \= proc\_open('php start.php start', $descriptorspec, $pipes); exec("start $localUrl"); while (true) { $r \= $m\->checkAll(); if ($r) { $pStatus \= proc\_get\_status($resource); $PID \= $pStatus\['pid'\]; kill($PID); proc\_close($resource); $resource \= proc\_open('php start.php start', $descriptorspec, $pipes); } sleep(1); } function pln($data) { echo $data . PHP\_EOL; } function kill($pid) { return stripos(php\_uname('s'), 'win') \> \-1 ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid"); } ```