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 31, 2018
2 parents 0e50d27 + 83be7ba commit c9be434
Show file tree
Hide file tree
Showing 39 changed files with 750 additions and 253 deletions.
77 changes: 77 additions & 0 deletions bin/build-phar
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env php
<?php
$imiPath = dirname(__DIR__) . '/';
$pharFile = $imiPath . 'imi.phar';

class FileRegexIterator implements Iterator
{
private $iterator;

public function __construct(Iterator $iterator)
{
$this->iterator = $iterator;
}

public function current ()
{
return $this->iterator->current();
}

public function key ()
{
return $this->iterator->key();
}

public function next ()
{
do{
$this->iterator->next();
$current = $this->iterator->current();
if(false === $current)
{
return false;
}
}while($this->isDot($current));
}

public function rewind ()
{
$this->iterator->rewind();
if($this->isDot($this->iterator->current()))
{
$this->next();
}
}

public function valid ()
{
return $this->iterator->valid();
}

private function isDot($fileName)
{
$basename = basename($fileName);
return '.' === $basename || '..' === $basename;
}
}

if(is_file($pharFile))
{
echo 'deleting file: ', $pharFile, PHP_EOL;
unlink($pharFile);
}

$phar = new Phar($pharFile);
$phar->stopBuffering();
echo 'building from directory: ', $imiPath, PHP_EOL;

$directory = new \RecursiveDirectoryIterator($imiPath);
$iterator = new \RecursiveIteratorIterator($directory);
$regex = new \RegexIterator($iterator, '/^((?!(\/.git\/)).)*$/');
$fileIterator = new FileRegexIterator($regex);

$phar->buildFromIterator($fileIterator, $imiPath);

$phar->setStub($phar->createDefaultStub('src/PharMain.php'));

echo 'build OK!', PHP_EOL;
5 changes: 5 additions & 0 deletions bin/imi.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
if(null === $namespace)
{
$config = include File::path(dirname($_SERVER['SCRIPT_NAME'], 2), 'config/config.php');
if(!isset($config['namespace']))
{
echo 'has no namespace, please add arg: -appNamespace "Your App Namespace"', PHP_EOL;
exit;
}
$namespace = $config['namespace'];
}

Expand Down
38 changes: 36 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ abstract class App
*/
private static $isDebug = false;

/**
* Composer ClassLoader
*
* @var \Composer\Autoload\ClassLoader
*/
private static $loader;

/**
* 框架服务运行入口
* @param string $namespace 应用命名空间
Expand Down Expand Up @@ -109,7 +116,7 @@ private static function initMains()
$servers = array_merge(['main'=>Config::get('@app.mainServer')], Config::get('@app.subServers', []));
foreach($servers as $serverName => $item)
{
MainHelper::getMain($item['namespace'], 'server_' . $serverName);
MainHelper::getMain($item['namespace'], 'server.' . $serverName);
}
}

Expand Down Expand Up @@ -186,12 +193,15 @@ public static function isInited()
}

/**
* 初始化worker
* 初始化 Worker,但不一定是 Worker 进程
*
* @return void
*/
public static function initWorker()
{
// Worker 进程初始化前置
Event::trigger('IMI.INIT.WORKER.BEFORE');

$appMains = MainHelper::getAppMains();
// 加载服务器注解
Annotation::getInstance()->init($appMains);
Expand Down Expand Up @@ -255,5 +265,29 @@ public static function initWorker()
CacheManager::addName($name, $cache['handlerClass'], $cache['option']);
}
}

// Worker 进程初始化后置
Event::trigger('IMI.INIT.WORKER.AFTER');
}

/**
* 设置 Composer ClassLoader
*
* @param \Composer\Autoload\ClassLoader $loader
* @return void
*/
public static function setLoader(\Composer\Autoload\ClassLoader $loader)
{
static::$loader = $loader;
}

/**
* 获取 Composer ClassLoader
*
* @return \Composer\Autoload\ClassLoader|null
*/
public static function getLoader()
{
return static::$loader;
}
}
2 changes: 1 addition & 1 deletion src/Bean/BeanProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static function getConfigInjects($className)
$beanProperties = null;
// 优先从服务器bean配置获取
try{
$beanProperties = Config::get('@server_' . RequestContext::getServer()->getName() . '.beans.' . $beanName, null);
$beanProperties = Config::get('@server.' . RequestContext::getServer()->getName() . '.beans.' . $beanName, null);
}
catch(\Throwable $ex)
{
Expand Down
27 changes: 22 additions & 5 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,32 @@ abstract class Config
*/
public static function addConfig($name, array $config)
{
if(isset(static::$configs[$name]))
$nameSplit = explode('.', $name);

$first = array_shift($nameSplit);
if(!isset(static::$configs[$first]))
{
return false;
static::$configs[$first] = new ArrayData([]);
}
static::$configs[$name] = new ArrayData($config);
if(static::$configs[$name]->exists('configs'))

if(isset($nameSplit[0]))
{
static::load($name, static::$configs[$name]->get('configs', []));
$configName = implode('.', $nameSplit);
static::$configs[$first]->set($configName, $config);
if(false !== ($configs = static::$configs[$first]->get($configName . '.configs')))
{
static::load($name, $configs);
}
}
else
{
static::$configs[$first]->set($config);
if(static::$configs[$first]->exists('configs'))
{
static::load($name, static::$configs[$first]->get('configs', []));
}
}

return true;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Db/Aop/TransactionAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function defer(AroundJoinPoint $joinPoint)
catch(\Throwable $ex)
{
// 回滚事务
$db->rollBack();
if($db->inTransaction())
{
$db->rollBack();
}
throw $ex;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Db/Drivers/CoroutineMysql/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ public function getInstance(): \Swoole\Coroutine\MySQL
*/
public function beginTransaction(): bool
{
$this->inTransaction = true;
return $this->instance->begin();
$result = $this->instance->begin();
if($result)
{
$this->inTransaction = true;
}
return $result;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Query/Interfaces/IQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getDb(): IDb;
* @param string $database 数据库名
* @return static
*/
public function table(string $table, string $alias = null, string $database);
public function table(string $table, string $alias = null, string $database = null);

/**
* 设置表名,使用SQL原生语句
Expand Down
6 changes: 5 additions & 1 deletion src/Listener/OnServerCreateAfter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Imi\Listener;

use Imi\Config;
use Imi\Event\EventParam;
use Imi\Event\IEventListener;
use Imi\Process\ProcessManager;
Expand All @@ -19,6 +20,9 @@ class OnServerCreateAfter implements IEventListener
public function handle(EventParam $e)
{
// 热更新
ProcessManager::runWithManager('hotUpdate');
if(Config::get('@app.beans.hotUpdate.status', true))
{
ProcessManager::runWithManager('hotUpdate');
}
}
}
15 changes: 15 additions & 0 deletions src/Main/BaseMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Imi\Main;

use Imi\Config;
use Imi\Main\Helper;

/**
* 主类基类
Expand Down Expand Up @@ -30,6 +31,7 @@ public function __construct(string $moduleName)
{
$this->moduleName = $moduleName;
$this->loadConfig();
$this->loadComponents();
$this->__init();
}

Expand Down Expand Up @@ -99,4 +101,17 @@ public function getModuleName(): string
{
return $this->moduleName;
}

/**
* 加载组件
*
* @return void
*/
protected function loadComponents()
{
foreach(Config::get('@' . $this->moduleName . '.components', []) as $componentName => $namespace)
{
Helper::getMain($namespace, $componentName);
}
}
}
19 changes: 10 additions & 9 deletions src/Main/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ abstract class Helper
/**
* 获取主类实例对象
* @param string $namespace
* @param string $componentName
* @return \Imi\Main\BaseMain
*/
public static function getMain($namespace, $serverName = null)
public static function getMain($namespace, $componentName = null)
{
if(null !== $namespace)
{
if(null === $serverName)
if(null === $componentName)
{
// 获取
if(isset(static::$mains[$namespace]))
Expand All @@ -40,17 +41,17 @@ public static function getMain($namespace, $serverName = null)
}
else
{
return static::newInstance($namespace, $serverName);
return static::newInstance($namespace, $componentName);
}
}
}
else if(null !== $serverName)
else if(null !== $componentName)
{
if(!isset(static::$nameMap[$serverName], static::$mains[static::$nameMap[$serverName]]))
if(!isset(static::$nameMap[$componentName], static::$mains[static::$nameMap[$componentName]]))
{
return null;
}
return static::$mains[static::$nameMap[$serverName]];
return static::$mains[static::$nameMap[$componentName]];
}
else
{
Expand Down Expand Up @@ -78,13 +79,13 @@ public static function getAppMains()
return $mains;
}

private static function newInstance($namespace, $serverName)
private static function newInstance($namespace, $componentName)
{
$className = $namespace . '\\Main';
if(class_exists($className))
{
static::$mains[$namespace] = new $className($serverName);
static::$nameMap[$serverName] = $namespace;
static::$mains[$namespace] = new $className($componentName);
static::$nameMap[$componentName] = $namespace;
return static::$mains[$namespace];
}
else
Expand Down
7 changes: 7 additions & 0 deletions src/Model/Annotation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ class Column extends Base
* @var boolean
*/
public $isAutoIncrement = false;

/**
* 虚拟字段,不参与数据库操作
*
* @var boolean
*/
public $virtual = false;
}
17 changes: 17 additions & 0 deletions src/Model/Event/Listener/IAfterFindEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Imi\Model\Event\Listener;

use Imi\Model\Event\Param\AfterFindEventParam;

/**
* 模型 查找后 事件监听接口
*/
interface IAfterFindEventListener
{
/**
* 事件处理方法
* @param AfterFindEventParam $e
* @return void
*/
public function handle(AfterFindEventParam $e);
}
Loading

0 comments on commit c9be434

Please sign in to comment.