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 23, 2018
2 parents ba54f45 + bb0c9b3 commit b19d0f8
Show file tree
Hide file tree
Showing 86 changed files with 2,673 additions and 133 deletions.
52 changes: 39 additions & 13 deletions src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
namespace Imi\Bean;

use Imi\Config;
use Imi\Worker;
use Imi\Util\File;
use Imi\RequestContext;
use Imi\Util\ClassObject;
use Imi\Bean\Parser\BeanParser;
Expand All @@ -17,15 +19,40 @@ abstract class BeanFactory
public static function newInstance($class, ...$args)
{
$ref = new \ReflectionClass($class);
$tpl = static::getTpl($ref);
$object = eval($tpl);
if($ref->hasMethod('__init'))

$cacheFileName = static::getCacheFileName($class);
if(!is_file($cacheFileName))
{
$tpl = static::getTpl($ref);
$path = dirname($cacheFileName);
if(!is_dir($path))
{
File::createDir($path);
}
File::writeFile($cacheFileName, '<?php ' . $tpl);
}

$object = include $cacheFileName;

if(method_exists($object, '__init'))
{
$ref->getMethod('__init')->invoke($object, ...$args);
$object->__init(...$args);
}
return $object;
}

/**
* 获取类缓存文件名
*
* @param string $className
* @return string
*/
private static function getCacheFileName($className)
{
$path = Config::get('@app.beanClassCache', sys_get_temp_dir());
return File::path($path, 'imiBeanCache', Worker::getWorkerID(), str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php');
}

/**
* 获取类模版
* @param \ReflectionClass $ref
Expand All @@ -49,8 +76,7 @@ private static function getTpl($ref)
$constructDefine = '...$args';
}
// 匿名类模版定义
// 这里的换行符是为了解决某个不明觉厉的BUG加的,不加有时会有奇怪的问题,原因未知,BUG复现难……
$tpl = PHP_EOL . <<<TPL
$tpl = <<<TPL
return new class(...\$args) extends \\{$class}
{
private \$beanProxy;
Expand Down Expand Up @@ -87,13 +113,13 @@ private static function getMethodsTpl($ref, $class)
$tpl .= <<<TPL
public function {$method->name}({$paramsTpls['define']}){$methodReturnType}
{
\$args = [{$paramsTpls['args']}];{$paramsTpls['args_variadic']}
\$__args__ = [{$paramsTpls['args']}];{$paramsTpls['args_variadic']}
return \$this->beanProxy->call(
'{$method->name}',
function({$paramsTpls['define']}){
return parent::{$method->name}({$paramsTpls['call']});
},
\$args
\$__args__
);
}
Expand Down Expand Up @@ -149,10 +175,10 @@ private static function getMethodParamTpls(\ReflectionMethod $method)
}
$result['args_variadic'] .= <<<STR
if(!isset(\$args[func_num_args() - 1]))
if(!isset(\$__args__[func_num_args() - 1]))
{
\$allArgs = func_get_args();
\$args = array_merge(\$args, array_splice(\$allArgs, count(\$args)));
\$__allArgs__ = func_get_args();
\$__args__ = array_merge(\$__args__, array_splice(\$__allArgs__, count(\$__args__)));
}
STR;
return $result;
Expand Down Expand Up @@ -226,9 +252,9 @@ private static function getMethodArgsVariadicTpl(\ReflectionParameter $param)
{
return <<<TPL
foreach(\${$param->name} as \$item)
foreach(\${$param->name} as \$__item__)
{
\$args[] = \$item;
\$__args__[] = \$__item__;
}
TPL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cache/Handler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function encode($data)
}
else
{
return RequestContext::getBean($this->formatHandlerClass)->encode($data);
return RequestContext::getServerBean($this->formatHandlerClass)->encode($data);
}
}

Expand All @@ -52,7 +52,7 @@ protected function decode($data)
}
else
{
return RequestContext::getBean($this->formatHandlerClass)->decode($data);
return RequestContext::getServerBean($this->formatHandlerClass)->decode($data);
}
}

Expand Down
85 changes: 85 additions & 0 deletions src/ConnectContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Imi;

use Imi\RequestContext;

abstract class ConnectContext
{
private static $context = [];

/**
* 为当前请求创建上下文
* @return void
*/
public static function create($fd)
{
if(!RequestContext::exsits())
{
RequestContext::create();
}
RequestContext::set('fd', $fd);
if(!isset(static::$context[$fd]))
{
static::$context[$fd] = [];
}
}

/**
* 销毁当前请求的上下文
* @return void
*/
public static function destroy($fd)
{
if(isset(static::$context[$fd]))
{
unset(static::$context[$fd]);
}
}

/**
* 判断当前请求上下文是否存在
* @return boolean
*/
public static function exsits()
{
if(RequestContext::exsits())
{
return isset(static::$context[RequestContext::get('fd')]);
}
else
{
return false;
}
}

/**
* 获取上下文数据
* @param string $name
* @param mixed $default
* @return mixed
*/
public static function get($name, $default = null)
{
return static::$context[RequestContext::get('fd')][$name] ?? $default;
}

/**
* 设置上下文数据
* @param string $name
* @param mixed $value
* @return void
*/
public static function set($name, $value)
{
static::$context[RequestContext::get('fd')][$name] = $value;
}

/**
* 获取当前上下文
* @return array
*/
public static function getContext()
{
return static::$context[RequestContext::get('fd')] ?? null;
}
}
3 changes: 3 additions & 0 deletions src/Controller/HttpController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace Imi\Controller;

/**
* Http 控制器
*/
abstract class HttpController
{
/**
Expand Down
20 changes: 20 additions & 0 deletions src/Controller/WebSocketController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Imi\Controller;

/**
* WebSocket 控制器
*/
abstract class WebSocketController
{
/**
* 请求
* @var \Imi\Server\WebSocket\Server
*/
public $server;

/**
* 桢
* @var \Imi\Server\WebSocket\Message\IFrame
*/
public $frame;
}
19 changes: 17 additions & 2 deletions src/Listener/OnStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,27 @@ class OnStart implements IManagerStartEventListener
*/
public function handle(ManagerStartEventParam $e)
{
// 进程PID记录
$fileName = File::path(dirname($_SERVER['SCRIPT_NAME']), 'imi.pid');
File::writeFile($fileName, json_encode([
'masterPID' => $e->server->master_pid,
'managerPID' => $e->server->manager_pid,
'masterPID' => $e->server->getSwooleServer()->master_pid,
'managerPID' => $e->server->getSwooleServer()->manager_pid,
]));

// 清除所有 worker 进程的 Bean 类缓存
$path = Config::get('@app.beanClassCache');
if(null !== $path)
{
$path = File::path($path, 'imiBeanCache');
foreach (File::enum($path) as $file)
{
if (is_file($file))
{
unlink($file);
}
}
}

// 热更新
$process = ProcessManager::create('hotUpdate');
$process->start();
Expand Down
28 changes: 23 additions & 5 deletions src/Listener/WorkerInit.php
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
<?php
namespace Imi\Listener;

use Imi\App;
use Imi\Config;
use Imi\Worker;
use Imi\Util\File;
use Imi\Main\Helper;
use Imi\Util\Coroutine;
use Imi\Bean\Annotation;
use Imi\Pool\PoolConfig;
use Imi\Event\EventParam;
use Imi\Pool\PoolManager;
use Imi\Cache\CacheManager;
use Imi\Event\IEventListener;
use Imi\Bean\Annotation\Listener;
use Imi\Util\CoroutineChannelManager;
use Imi\App;
use Imi\Server\Event\Param\WorkStartEventParam;
use Imi\Server\Event\Listener\IWorkStartEventListener;

/**
* @Listener(eventName="IMI.MAIN_SERVER.WORKER.START",priority=PHP_INT_MAX)
*/
class WorkerInit implements IEventListener
class WorkerInit implements IWorkStartEventListener
{
/**
* 事件处理方法
* @param EventParam $e
* @return void
*/
public function handle(EventParam $e)
public function handle(WorkStartEventParam $e)
{
// 当前进程的 WorkerID 设置
Worker::setWorkerID($e->server->getSwooleServer()->worker_id);

// 清除当前 worker 进程的 Bean 类缓存
$path = Config::get('@app.beanClassCache', sys_get_temp_dir());
$path = File::path($path, 'imiBeanCache', Worker::getWorkerID());
foreach (File::enum($path) as $file)
{
if (is_file($file))
{
unlink($file);
}
}

// 初始化 worker
App::initWorker();
}
}
10 changes: 8 additions & 2 deletions src/Pool/PoolManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public static function getResource(string $name): IPoolResource
{
$resource = static::getInstance($name)->getResource();

static::pushResourceToRequestContext($resource);
if(RequestContext::exsits())
{
static::pushResourceToRequestContext($resource);
}

return $resource;
}
Expand Down Expand Up @@ -118,7 +121,10 @@ public static function tryGetResource(string $name)
public static function releaseResource(IPoolResource $resource)
{
$resource->getPool()->release($resource);
static::removeResourceFromRequestContext($resource);
if(RequestContext::exsits())
{
static::removeResourceFromRequestContext($resource);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Process/ProcessManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static function create($name, $args = [], $redirectStdinStdout = null, $p
}
$processInstance = BeanFactory::newInstance($processOption['className'], $args);
$process = new \Swoole\Process(function(\Swoole\Process $swooleProcess) use($processInstance, $name){
// 设置进程名称
$swooleProcess->name($name);
// 进程开始事件
Event::trigger('IMI.PROCESS.BEGIN', [
'name' => $name,
Expand All @@ -47,8 +49,6 @@ public static function create($name, $args = [], $redirectStdinStdout = null, $p
'process' => $swooleProcess,
]);
}, $redirectStdinStdout, $pipeType);
// 设置进程名称
$process->name($name);
return $process;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Redis/SyncRedisResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(\Imi\Pool\Interfaces\IPool $pool, \Redis $redis, $co
*/
public function open($callback = null)
{
$result = $this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379);
$result = $this->redis->connect($this->config['host'] ?? '127.0.0.1', $this->config['port'] ?? 6379, $this->config['timeout'] ?? 2147483647);
if(isset($this->config['password']))
{
$result = $result && $this->redis->auth($this->config['password']);
Expand Down
Loading

0 comments on commit b19d0f8

Please sign in to comment.