Giter Club home page Giter Club logo

hot-reload's Introduction

热重载监视器

!!!警告!!! 仅供开发使用,请勿在生产环境使用,以免意外重启造成逻辑异常

方便的热重载组件,在Linux环境下使用Inotify,其他环境或虚拟机内使用iNode脏检测扫描实现开发过程中的代码热重载,可自定义重载处理逻辑,由于可以自定义,也可以作为一个方便的文件变更事件触发器使用,在文件变更时执行一些逻辑

安装

composer require easyswoole/hot-reload

单独使用

<?php

// 创建一个服务
require_once 'vendor/autoload.php';
$server = new \Swoole\Server('0.0.0.0', '9801');
$server->on('receive', function () {});

// 设置监视器的选项
$hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
// 虚拟机中可以关闭Inotify检测
$hotReloadOptions->disableInotify(true);
// 可以设置多个监控目录的绝对路径
$hotReloadOptions->setMonitorFolder([dirname(__FILE__)]);
// 忽略某些后缀名不去检测
$hotReloadOptions->setIgnoreSuffix(['php', 'txt']);
// 自定义检测到变更后的事件
$hotReloadOptions->setReloadCallback(function (\Swoole\Server $server) {
    echo "File change event triggered";  // 可以执行如清理临时目录等逻辑
    $server->reload();  // 接管变更事件 需要自己执行重启
});

$hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
$hotReload->attachToServer($server);
$server->start();

EasySwoole框架中使用

请在全局事件(EasySwooleEvent)的mainServerCreate方法中进行注册

<?php

class EasySwooleEvent {

    // 省略部分代码 ...

    /**
     * 服务启动时
     * @param EventRegister $register
     * @throws Exception
     */
    public static function mainServerCreate(EventRegister $register)
    {
        // 配置同上别忘了添加要检视的目录
        $hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
        $hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
        $hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);
        
        $server = ServerManager::getInstance()->getSwooleServer();
        $hotReload->attachToServer($server);
    }

    // 省略部分代码 ...

}

hot-reload's People

Contributors

evalor avatar kiss291323003 avatar player626 avatar pluveto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.