1 Unstar Star 7 Fork 0

liangguifeng / hyperf-watchPHP

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
hyperf的热更新,基于go语言编写,开箱即用,由于github太慢,故此备份了一份,供大家使用。 spread retract

Clone or download
watch 3.83 KB
Copy Edit Web IDE Raw Blame History
hanicc authored 2019-10-11 10:18 . Update watch
#!/usr/bin/env php
<?php declare(strict_types=1);
/**
* Hyperf Watch Hot Reload Scripts
* User: hanicc@qq.com
* Date: 2019/10/10
* Time: 下午17:30
* Modify From https://github.com/leocavalcante/dwoole/blob/master/dev/watch.php
*/
# PHP Bin File PHP程序所在路径(默认自动获取)
const PHP_BIN_FILE = 'which php';
# Watch Dir 监听目录(默认监听脚本所在的根目录)
const WATCH_DIR = __DIR__ . '/';
# Watch Ext 监听扩展名(多个可用英文逗号隔开)
const WATCH_EXT = 'php,env';
# Exclude Dir 排除目录(不监听的目录,数组形式)
const EXCLUDE_DIR = ['vendor'];
# Entry Point File 入口文件
const ENTRY_POINT_FILE = './bin/hyperf.php';
# Scan Interval 扫描间隔(毫秒,默认2000)
const SCAN_INTERVAL = 2000;
if (!function_exists('exec')) {
echo "[x] 请取消禁用exec函数" . PHP_EOL;
exit(1);
}
define('PHP', PHP_BIN_FILE == 'which php' ? exec('which php') : PHP_BIN_FILE);
if (!file_exists(PHP)) {
echo "[x] PHP bin (" . PHP . ") 没有找到,请确认路径正确?" . PHP_EOL;
exit(1);
}
if (!file_exists(ENTRY_POINT_FILE)) {
echo "[x] 入口文件 (" . ENTRY_POINT_FILE . ") 没有找到,请确认文件存在?" . PHP_EOL;
exit(1);
}
use Swoole\Process;
use Swoole\Timer;
use Swoole\Event;
swoole_async_set(['enable_coroutine' => false]);
$hashes = [];
$serve = null;
echo "🚀 Start @ " . date('Y-m-d H:i:s') . PHP_EOL;
start();
state();
Timer::tick(SCAN_INTERVAL, 'watch');
function start()
{
global $serve;
$serve = new Process('serve', true);
$serve->start();
if (false === $serve->pid) {
echo swoole_strerror(swoole_errno()) . PHP_EOL;
exit(1);
}
Event::add($serve->pipe, function ($pipe) use (&$serve) {
$message = @$serve->read();
if (!empty($message)) {
echo $message;
}
});
}
function watch()
{
global $hashes;
foreach ($hashes as $pathname => $current_hash) {
if (!file_exists($pathname)) {
unset($hashes[$pathname]);
continue;
}
$new_hash = file_hash($pathname);
if ($new_hash != $current_hash) {
change();
state();
break;
}
}
}
function state()
{
global $hashes;
$files = php_files(WATCH_DIR);
$hashes = array_combine($files, array_map('file_hash', $files));
$count = count($hashes);
echo "📡 Watching $count files..." . PHP_EOL;
}
function change()
{
global $serve;
echo "🔄 Restart @ " . date('Y-m-d H:i:s') . PHP_EOL;
Process::kill($serve->pid);
start();
}
function serve(Process $serve)
{
$opt = getopt('c');
if (isset($opt['c'])) echo exec(PHP . ' ' . ENTRY_POINT_FILE . ' di:init-proxy') . '..' . PHP_EOL;
$serve->exec(PHP, [ENTRY_POINT_FILE, 'start']);
}
function file_hash(string $pathname): string
{
$contents = file_get_contents($pathname);
if (false === $contents) {
return 'deleted';
}
return md5($contents);
}
function php_files(string $dirname): array
{
$directory = new RecursiveDirectoryIterator($dirname);
$filter = new Filter($directory);
$iterator = new RecursiveIteratorIterator($filter);
return array_map(function ($fileInfo) {
return $fileInfo->getPathname();
}, iterator_to_array($iterator));
}
class Filter extends RecursiveFilterIterator
{
public function accept()
{
if ($this->current()->isDir()) {
if (preg_match('/^\./', $this->current()->getFilename())) {
return false;
}
return !in_array($this->current()->getFilename(), EXCLUDE_DIR);
}
$list = array_map(function (string $item): string {
return "\.$item";
}, explode(',', WATCH_EXT));
$list = implode('|', $list);
return preg_match("/($list)$/", $this->current()->getFilename());
}
}

Comment ( 0 )

Sign in for post a comment

PHP
1
https://gitee.com/liangguifeng/hyperf-watch.git
git@gitee.com:liangguifeng/hyperf-watch.git
liangguifeng
hyperf-watch
hyperf-watch
master

Search