Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jul 2, 2018
2 parents f7f0eb2 + eb27760 commit d67bb18
Show file tree
Hide file tree
Showing 45 changed files with 1,065 additions and 128 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

[![Latest Version](https://img.shields.io/packagist/v/yurunsoft/imi.svg)](https://packagist.org/packages/yurunsoft/imi)
[![Travis](https://img.shields.io/travis/Yurunsoft/IMI.svg)](https://travis-ci.org/Yurunsoft/IMI)
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg)](https://secure.php.net/)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=2.2.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://secure.php.net/)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.0.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
[![Hiredis Version](https://img.shields.io/badge/hiredis-%3E=0.1-brightgreen.svg)](https://github.com/redis/hiredis)
[![IMI Doc](https://img.shields.io/badge/docs-passing-green.svg)](https://doc.imiphp.com)
[![IMI License](https://img.shields.io/hexpm/l/plug.svg)](https://github.com/Yurunsoft/imi/blob/master/LICENSE)
Expand Down Expand Up @@ -58,7 +58,7 @@ IMI 框架底层开发使用了强类型,易维护,性能更强。支持 Aop
- [x] Task 异步任务
- [x] 命令行开发辅助工具
- [ ] 图形化管理工具
- [ ] 项目热更新
- [x] 业务代码热更新
- [ ] RPC 远程调用
- [ ] WebSocket 服务器相关……
- [ ] TCP 服务器相关……
Expand Down
3 changes: 3 additions & 0 deletions bin/imi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
require __DIR__ . '/imi.php';
15 changes: 15 additions & 0 deletions bin/imi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use Imi\App;
use Imi\Util\Args;
use Imi\Util\File;

Args::init();

$namespace = Args::get('appNamespace');
if(null === $namespace)
{
$config = include File::path(dirname($_SERVER['SCRIPT_NAME'], 2), 'config/config.php');
$namespace = $config['namespace'];
}

App::run($namespace);
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'Imi\Model',
'Imi\Task',
'Imi\Tool',
'Imi\Process',
'Imi\HotUpdate',
],
'atomics' => [
'session'
Expand Down
37 changes: 21 additions & 16 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ abstract class App
*/
private static $container;

/**
* 框架是否已初始化
* @var boolean
*/
private static $isInited = false;

/**
* 当前是否为调试模式
* @var boolean
Expand All @@ -44,19 +50,6 @@ public static function run($namespace)
{
static::$namespace = $namespace;
static::initFramework();
static::createServers();
ServerManage::getServer('main')->getSwooleServer()->start();
}

/**
* 框架命令行工具运行入口
* @return void
*/
public static function runTool($namespace)
{
static::$namespace = $namespace;
static::initFramework();
Event::trigger('IMI.RUNTOOL');
}

/**
Expand All @@ -69,9 +62,12 @@ private static function initFramework()
// 初始化Main类
static::initMains();
// 注解处理
static::$annotation = new Annotation;
static::$annotation->init();
static::$annotation = Annotation::getInstance();
static::$annotation->init([
MainHelper::getMain('Imi', 'Imi'),
]);
Event::trigger('IMI.INITED');
static::$isInited = true;
}

/**
Expand All @@ -96,7 +92,7 @@ private static function initMains()
* 创建服务器对象们
* @return void
*/
private static function createServers()
public static function createServers()
{
// 创建服务器对象们前置操作
Event::trigger('IMI.SERVERS.CREATE.BEFORE');
Expand Down Expand Up @@ -154,4 +150,13 @@ public static function setDebug($isDebug)
{
static::$isDebug = $isDebug;
}

/**
* 框架是否已初始化
* @return boolean
*/
public static function isInited()
{
return static::$isInited;
}
}
8 changes: 6 additions & 2 deletions src/Bean/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
use Imi\App;
use Imi\Main\Helper as MainHelper;
use Imi\Config;
use Imi\Util\Traits\TSingleton;

/**
* 注解处理类
*/
class Annotation
{
use TSingleton;

/**
* 加载器
* @var AnnotationLoader
Expand All @@ -30,11 +33,12 @@ public function __construct()

/**
* 初始化
* @param \Imi\Main\BaseMain[] $mains
* @return void
*/
public function init()
public function init($mains = null)
{
foreach(MainHelper::getMains() as $main)
foreach($mains ?? MainHelper::getMains() as $main)
{
// 扫描注解
$this->loadModuleAnnotations($main->getNamespace());
Expand Down
13 changes: 10 additions & 3 deletions src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ private static function getTpl($ref)
$class = $ref->getName();
$methodsTpl = static::getMethodsTpl($ref, $class);
$construct = '';
if(null !== $ref->getConstructor())
$constructMethod = $ref->getConstructor();
if(null !== $constructMethod)
{
$construct = 'parent::__construct(...$args);';
$paramsTpls = static::getMethodParamTpls($constructMethod);
$constructDefine = $paramsTpls['define'];
$construct = "parent::__construct({$paramsTpls['call']});";
}
else
{
$constructDefine = '...$args';
}
// 匿名类模版定义
// 这里的换行符是为了解决某个不明觉厉的BUG加的,不加有时会有奇怪的问题,原因未知,BUG复现难……
Expand All @@ -47,7 +54,7 @@ private static function getTpl($ref)
{
private \$beanProxy;
public function __construct(...\$args)
public function __construct({$constructDefine})
{
\$this->beanProxy = new \Imi\Bean\BeanProxy(\$this);
{$construct}
Expand Down
2 changes: 1 addition & 1 deletion src/Bean/Parser/ListenerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function parse(\Imi\Bean\Annotation\Base $annotation, string $className,
{
if($annotation instanceof \Imi\Bean\Annotation\Listener)
{
Event::on($annotation->eventName, $className);
Event::on($annotation->eventName, $className, $annotation->priority);
}
}
}
84 changes: 84 additions & 0 deletions src/HotUpdate/HotUpdateProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Imi\HotUpdate;

use Imi\App;
use Imi\Util\Imi;
use Imi\Util\Coroutine;
use Imi\Bean\BeanFactory;
use Imi\Process\BaseProcess;
use Imi\Bean\Annotation\Bean;
use Imi\Process\Annotation\Process;

/**
* @Bean("hotUpdate")
* @Process("hotUpdate")
*/
class HotUpdateProcess extends BaseProcess
{
/**
* 监视器类
* @var \Imi\HotUpdate\Monitor\BaseMonitor
*/
protected $monitorClass = \Imi\HotUpdate\Monitor\FileMTime::class;

/**
* 每次检测时间间隔,单位:秒(有可能真实时间会大于设定的时间)
* @var integer
*/
protected $timespan = 1;

/**
* 包含的路径
* @var array
*/
protected $includePaths = [];

/**
* 排除的路径
* @var array
*/
protected $excludePaths = [];

/**
* 默认监视路径
* @var array
*/
protected $defaultPath = null;

/**
* 是否开启热更新,默认开启
* @var boolean
*/
protected $status = true;

public function run(\Swoole\Process $process)
{
if(!$this->status)
{
return;
}
if(null === $this->defaultPath)
{
$this->defaultPath = [
Imi::getNamespacePath(App::getNamespace()),
];
}
go(function(){
$monitor = BeanFactory::newInstance($this->monitorClass, array_merge($this->defaultPath, $this->includePaths), $this->excludePaths);
$reloadCmd = 'php ' . $_SERVER['argv'][0] . ' server/reload';
$time = 0;
while(true)
{
// 检测间隔延时
sleep(min(max($this->timespan - (microtime(true) - $time), $this->timespan), $this->timespan));
$time = microtime(true);
// 检查文件是否有修改
if($monitor->isChanged())
{
// 执行重新加载
Coroutine::exec($reloadCmd);
}
}
});
}
}
35 changes: 35 additions & 0 deletions src/HotUpdate/Monitor/BaseMonitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Imi\HotUpdate\Monitor;

abstract class BaseMonitor implements IMonitor
{
/**
* 包含的路径
* @var array
*/
protected $includePaths;

/**
* 排除的路径
* @var array
*/
protected $excludePaths;

/**
* 构造方法
* @param array $includePaths 包含的路径
* @param array $excludePaths 排除的路径
*/
public function __construct(array $includePaths, array $excludePaths = [])
{
$this->includePaths = $includePaths;
$this->excludePaths = $excludePaths;
$this->init();
}

/**
* 初始化
* @return void
*/
abstract protected function init();
}
Loading

0 comments on commit d67bb18

Please sign in to comment.