Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Aug 24, 2018
2 parents e3d1a3f + 73b87a1 commit 0e50d27
Show file tree
Hide file tree
Showing 42 changed files with 986 additions and 280 deletions.
19 changes: 10 additions & 9 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Imi\Server\Http\Server;
use Imi\Main\Helper as MainHelper;
use Imi\Util\CoroutineChannelManager;
use Imi\Util\Imi;

abstract class App
{
Expand Down Expand Up @@ -87,14 +88,10 @@ private static function initFramework()
private static function clearBeanCache()
{
// 清除框架 Bean类 缓存
$path = Config::get('@app.beanClassCache', sys_get_temp_dir());
$path = File::path($path, 'imiBeanCache', 'imi');
$path = Imi::getImiClassCachePath();
foreach (File::enum($path) as $file)
{
if (is_file($file))
{
unlink($file);
}
unlink($file);
}
}

Expand Down Expand Up @@ -248,11 +245,15 @@ public static function initWorker()
}
}
}

// 缓存初始化
$caches = $main->getConfig()['caches'] ?? [];
foreach($caches as $name => $cache)
foreach($appMains as $main)
{
CacheManager::addName($name, $cache['handlerClass'], $cache['option']);
$caches = $main->getConfig()['caches'] ?? [];
foreach($caches as $name => $cache)
{
CacheManager::addName($name, $cache['handlerClass'], $cache['option']);
}
}
}
}
1 change: 1 addition & 0 deletions src/Bean/AnnotationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function loadModuleAnnotations($namespace, $callback)
$pathLength = strlen($path);
foreach(File::enumPHPFile($path) as $filePath)
{
$filePath = $filePath[0];
$diffPath = substr($filePath, $pathLength);
if(isset($diffPath[0]) && DIRECTORY_SEPARATOR === $diffPath[0])
{
Expand Down
74 changes: 33 additions & 41 deletions src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Imi\RequestContext;
use Imi\Util\ClassObject;
use Imi\Bean\Parser\BeanParser;
use Imi\Util\Imi;

abstract class BeanFactory
{
Expand All @@ -29,7 +30,15 @@ public static function newInstance($class, ...$args)
{
File::createDir($path);
}
File::writeFile($cacheFileName, '<?php ' . $tpl);
if(SWOOLE_VERSION > '4.0.4')
{
File::writeFile($cacheFileName, '<?php ' . $tpl);
}
else
{
// 4.0.4及之前版本由于在AIO线程处理了信号,导致奇怪的问题,所以改为阻塞写入文件,性能影响其实也很低,只在第一次写入
file_put_contents($cacheFileName, '<?php ' . $tpl);
}
}

$object = include $cacheFileName;
Expand All @@ -49,8 +58,15 @@ public static function newInstance($class, ...$args)
*/
private static function getCacheFileName($className)
{
$path = Config::get('@app.beanClassCache', sys_get_temp_dir());
return File::path($path, 'imiBeanCache', Worker::getWorkerID() ?? 'imi', str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php');
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
if(null === ($workerID = Worker::getWorkerID()))
{
return Imi::getImiClassCachePath($fileName);
}
else
{
return Imi::getWorkerClassCachePathByWorkerID($workerID, $fileName);
}
}

/**
Expand Down Expand Up @@ -113,11 +129,14 @@ private static function getMethodsTpl($ref, $class)
$tpl .= <<<TPL
public function {$method->name}({$paramsTpls['define']}){$methodReturnType}
{
\$__args__ = [{$paramsTpls['args']}];{$paramsTpls['args_variadic']}
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
return \$this->beanProxy->call(
'{$method->name}',
function({$paramsTpls['define']}){
return parent::{$method->name}({$paramsTpls['call']});
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
return parent::{$method->name}(...\$__args__);
},
\$__args__
);
Expand All @@ -139,10 +158,11 @@ private static function getMethodParamTpls(\ReflectionMethod $method)
'args' => [],
'define' => [],
'call' => [],
'set_args' => '',
];
foreach($method->getParameters() as $param)
foreach($method->getParameters() as $i => $param)
{
// 数组参数,支持引用传参
// 数组参数,支持可变传参
if(!$param->isVariadic())
{
$result['args'][] = static::getMethodParamArgsTpl($param);
Expand All @@ -151,8 +171,13 @@ private static function getMethodParamTpls(\ReflectionMethod $method)
$result['define'][] = static::getMethodParamDefineTpl($param);
// 调用传参
$result['call'][] = static::getMethodParamCallTpl($param);
// 引用传参
if($param->isPassedByReference())
{
$result['set_args'] .= '$__args__[' . $i . '] = &$' . $param->name . ';';
}
}
foreach($result as &$item)
foreach($result as $key => &$item)
{
if(is_array($item))
{
Expand All @@ -164,23 +189,6 @@ private static function getMethodParamTpls(\ReflectionMethod $method)
{
$result['call'] = '...func_get_args()';
}
// 可变参数
if(isset($param) && $param->isVariadic())
{
$result['args_variadic'] = static::getMethodArgsVariadicTpl($param);
}
else
{
$result['args_variadic'] = '';
}
$result['args_variadic'] .= <<<STR
if(!isset(\$__args__[func_num_args() - 1]))
{
\$__allArgs__ = func_get_args();
\$__args__ = array_merge(\$__args__, array_splice(\$__allArgs__, count(\$__args__)));
}
STR;
return $result;
}

Expand Down Expand Up @@ -243,22 +251,6 @@ private static function getMethodParamCallTpl(\ReflectionParameter $param)
return ($param->isVariadic() ? '...' : '') . '$' . $param->name;
}

/**
* 获取方法可变参数模版
* @param \ReflectionParameter $param
* @return string
*/
private static function getMethodArgsVariadicTpl(\ReflectionParameter $param)
{
return <<<TPL
foreach(\${$param->name} as \$__item__)
{
\$__args__[] = \$__item__;
}
TPL;
}

/**
* 获取方法返回值模版
* @param \ReflectionMethod $method
Expand Down
10 changes: 7 additions & 3 deletions src/Db/Drivers/CoroutineMysql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Driver implements IDb
* 参数格式:
* [
* 'host' => 'MySQL IP地址',
* 'user' => '数据用户',
* 'username' => '数据用户',
* 'password' => '数据库密码',
* 'database' => '数据库名',
* 'port' => 'MySQL端口 默认3306 可选参数',
Expand All @@ -57,9 +57,13 @@ class Driver implements IDb
public function __construct($option = [])
{
$this->option = $option;
if(!isset($this->option['username']))
if(isset($this->option['username']))
{
$this->option['username'] = 'root';
$this->option['user'] = $this->option['username'];
}
else
{
$this->option['user'] = 'root';
}
if(!isset($option['password']))
{
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Drivers/PdoMysql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Driver implements IDb
* 参数格式:
* [
* 'host' => 'MySQL IP地址',
* 'user' => '数据用户',
* 'username' => '数据用户',
* 'password' => '数据库密码',
* 'database' => '数据库名',
* 'port' => 'MySQL端口 默认3306 可选参数',
Expand Down
16 changes: 16 additions & 0 deletions src/Db/Query/Interfaces/IResult.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Imi\Db\Query\Interfaces;

use Imi\Db\Interfaces\IStatement;

interface IResult
{
/**
Expand Down Expand Up @@ -52,4 +54,18 @@ public function getScalar();
* @return int
*/
public function getRowCount();

/**
* 获取执行的SQL语句
*
* @return string
*/
public function getSql();

/**
* 获取结果集对象
*
* @return \Imi\Db\Interfaces\IStatement
*/
public function getStatement(): IStatement;
}
36 changes: 32 additions & 4 deletions src/Db/Query/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function getAffectedRows()
}

/**
* 返回一行数据,数组或对象
* 返回一行数据,数组或对象,失败返回null
* @param string $className 实体类名,为null则返回数组
* @return mixed
* @return mixed|null
*/
public function get($className = null)
{
Expand All @@ -87,6 +87,10 @@ public function get($className = null)
throw new \RuntimeException('Result is not success!');
}
$result = $this->statement->fetch();
if(false === $result)
{
return null;
}

if(null === $className)
{
Expand Down Expand Up @@ -115,9 +119,9 @@ public function get($className = null)
}

/**
* 返回数组
* 返回数组,失败返回null
* @param string $className 实体类名,为null则数组每个成员为数组
* @return array
* @return array|null
*/
public function getArray($className = null)
{
Expand All @@ -126,6 +130,10 @@ public function getArray($className = null)
throw new \RuntimeException('Result is not success!');
}
$result = $this->statement->fetchAll();
if(false === $result)
{
return null;
}

if(null === $className)
{
Expand Down Expand Up @@ -194,4 +202,24 @@ public function getRowCount()
}
return count($this->statement->fetchAll());
}

/**
* 获取执行的SQL语句
*
* @return string
*/
public function getSql()
{
return $this->statement->getSql();
}

/**
* 获取结果集对象
*
* @return \Imi\Db\Interfaces\IStatement
*/
public function getStatement(): IStatement
{
return $this->statement;
}
}
11 changes: 4 additions & 7 deletions src/HotUpdate/HotUpdateProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use Imi\Process\BaseProcess;
use Imi\Bean\Annotation\Bean;
use Imi\Process\Annotation\Process;
use Imi\Util\Args;

/**
* @Bean("hotUpdate")
* @Process("hotUpdate")
* @Process(name="hotUpdate", unique=true)
*/
class HotUpdateProcess extends BaseProcess
{
Expand Down Expand Up @@ -65,12 +64,10 @@ public function run(\Swoole\Process $process)
];
}
go(function(){
echo 'Process [hotUpdate] start', PHP_EOL;
$monitor = BeanFactory::newInstance($this->monitorClass, array_merge($this->defaultPath, $this->includePaths), $this->excludePaths);
$reloadCmd = 'php ' . $_SERVER['argv'][0] . ' server/reload';
if(null !== ($appNamespace = Args::get('appNamespace')))
{
$reloadCmd .= ' -appNamespace "' . $appNamespace . '"';
}

$reloadCmd = Imi::getImiCmd('server', 'reload');
$time = 0;
while(true)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Listener/OnManagerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ class OnManagerStart implements IManagerStartEventListener
*/
public function handle(ManagerStartEventParam $e)
{
// 随机数播种
mt_srand();

// 进程PID记录
$fileName = File::path(dirname($_SERVER['SCRIPT_NAME']), str_replace('\\', '-', App::getNamespace()) . '.pid');
File::writeFile($fileName, json_encode([
'masterPID' => Swoole::getMasterPID(),
'managerPID' => Swoole::getManagerPID(),
]));

// 热更新
$process = ProcessManager::create('hotUpdate');
$process->start();
}
}
24 changes: 24 additions & 0 deletions src/Listener/OnManagerStop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Imi\Listener;

use Imi\Process\ProcessManager;
use Imi\Bean\Annotation\Listener;
use Imi\Server\Event\Param\ManagerStopEventParam;
use Imi\Server\Event\Listener\IManagerStopEventListener;
use Imi\App;

/**
* @Listener(eventName="IMI.MAIN_SERVER.MANAGER.STOP",priority=PHP_INT_MIN)
*/
class OnManagerStop implements IManagerStopEventListener
{
/**
* 事件处理方法
* @param ManagerStopEventParam $e
* @return void
*/
public function handle(ManagerStopEventParam $e)
{
App::getBean('Logger')->save();
}
}
Loading

0 comments on commit 0e50d27

Please sign in to comment.