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 30, 2018
2 parents b19d0f8 + 188fdea commit 98d99bd
Show file tree
Hide file tree
Showing 99 changed files with 3,278 additions and 230 deletions.
2 changes: 1 addition & 1 deletion src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ 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(), str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php');
return File::path($path, 'imiBeanCache', Worker::getWorkerID() ?? 'imi', str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php');
}

/**
Expand Down
113 changes: 90 additions & 23 deletions src/Bean/BeanProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,22 @@ private function init()
}
}

/**
* 注入属性
*
* @return void
*/
private function injectProps()
{
$annotations = $this->getInjectAnnotations();
$configs = $this->getConfigInjects();
foreach(array_keys($configs) as $key)
{
if(isset($annotations[$key]))
{
unset($annotations[$key]);
}
}

$className = $this->refClass->getParentClass()->getName();
list($annotations, $configs) = static::getInjects($className);

// @inject()和@requestInject()注入
foreach($annotations as $propName => $option)
foreach($annotations as $propName => $annotation)
{
$propRef = $this->refClass->getProperty($propName);
$propRef->setAccessible(true);
if(isset($option['requestInject']) && Coroutine::isIn())
{
$propRef->setValue($this->object, RequestContext::getBean($option['requestInject']->name, ...$option['requestInject']->args));
}
else if(isset($option['inject']))
{
$propRef->setValue($this->object, App::getBean($option['inject']->name, ...$option['inject']->args));
}
$propRef->setValue($this->object, static::getInjectValueByAnnotation($annotation));
}

// 配置注入
Expand All @@ -135,9 +125,36 @@ private function injectProps()
}
}

private function getInjectAnnotations()
/**
* 根据注解获取注入值
*
* @param \Imi\Aop\Annotation\Inject $annotation
* @return mixed
*/
public static function getInjectValueByAnnotation($annotation)
{
if(isset($annotation['requestInject']) && Coroutine::isIn())
{
return RequestContext::getBean($annotation['requestInject']->name, ...$annotation['requestInject']->args);
}
else if(isset($annotation['inject']))
{
return App::getBean($annotation['inject']->name, ...$annotation['inject']->args);
}
else
{
return null;
}
}

/**
* 获取注入属性的注解们
*
* @param string $className
* @return array
*/
public static function getInjectAnnotations($className)
{
$className = $this->refClass->getParentClass()->getName();
$aopData = AopParser::getInstance()->getData();
if(!isset($aopData[$className]))
{
Expand All @@ -146,9 +163,14 @@ private function getInjectAnnotations()
return $aopData[$className]['property'];
}

private function getConfigInjects()
/**
* 获取注入属性的配置们
*
* @param string $className
* @return array
*/
public static function getConfigInjects($className)
{
$className = $this->refClass->getParentClass()->getName();
// 配置文件注入
$beanData = BeanParser::getInstance()->getData();
if(isset($beanData[$className]))
Expand Down Expand Up @@ -176,6 +198,26 @@ private function getConfigInjects()
return $beanProperties ?? [];
}

/**
* 获取注入类属性的注解和配置
*
* @param string $className
* @return [$annotations, $configs]
*/
public static function getInjects($className)
{
$annotations = static::getInjectAnnotations($className);
$configs = static::getConfigInjects($className);
foreach(array_keys($configs) as $key)
{
if(isset($annotations[$key]))
{
unset($annotations[$key]);
}
}
return [$annotations, $configs];
}

/**
* 判断是否属于当前类的切面
* @param array $option
Expand Down Expand Up @@ -406,4 +448,29 @@ private function doAspect($method, $pointType, $callback)
}
}
}

/**
* 获取注入类属性的值
*
* @param string $className
* @param string $propertyName
* @return mixed
*/
public static function getInjectValue($className, $propertyName)
{
list($annotations, $configs) = static::getInjects($className);
if(isset($configs[$propertyName]))
{
return $configs[$propertyName];
}
else if(isset($annotations[$propertyName]))
{
return static::getInjectValueByAnnotation($annotations[$propertyName]);
}
else
{
return null;
}
}

}
50 changes: 36 additions & 14 deletions src/ConnectContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ abstract class ConnectContext
* 为当前请求创建上下文
* @return void
*/
public static function create($fd)
public static function create()
{
if(!RequestContext::exsits())
$key = static::getContextKey();
if(!isset(static::$context[$key]))
{
RequestContext::create();
}
RequestContext::set('fd', $fd);
if(!isset(static::$context[$fd]))
{
static::$context[$fd] = [];
static::$context[$key] = RequestContext::getServerBean('ConnectContextStore')->read($key);
}
}

Expand All @@ -30,10 +26,12 @@ public static function create($fd)
*/
public static function destroy($fd)
{
if(isset(static::$context[$fd]))
$key = static::getContextKey();
if(isset(static::$context[$key]))
{
unset(static::$context[$fd]);
unset(static::$context[$key]);
}
RequestContext::getServerBean('ConnectContextStore')->destroy($key);
}

/**
Expand All @@ -44,7 +42,8 @@ public static function exsits()
{
if(RequestContext::exsits())
{
return isset(static::$context[RequestContext::get('fd')]);
$key = static::getContextKey();
return isset(static::$context[$key]) || RequestContext::getServerBean('ConnectContextStore')->exists($key);
}
else
{
Expand All @@ -60,7 +59,12 @@ public static function exsits()
*/
public static function get($name, $default = null)
{
return static::$context[RequestContext::get('fd')][$name] ?? $default;
$key = static::getContextKey();
if(!isset(static::$context[$key]))
{
static::$context[$key] = RequestContext::getServerBean('ConnectContextStore')->read($key);
}
return static::$context[$key][$name] ?? $default;
}

/**
Expand All @@ -71,7 +75,14 @@ public static function get($name, $default = null)
*/
public static function set($name, $value)
{
static::$context[RequestContext::get('fd')][$name] = $value;
$key = static::getContextKey();
$store = RequestContext::getServerBean('ConnectContextStore');
if(!isset(static::$context[$key]))
{
static::$context[$key] = $store->read($key);
}
static::$context[$key][$name] = $value;
$store->save($key, static::$context[$key]);
}

/**
Expand All @@ -80,6 +91,17 @@ public static function set($name, $value)
*/
public static function getContext()
{
return static::$context[RequestContext::get('fd')] ?? null;
$key = static::getContextKey();
return static::$context[$key] ?? null;
}

/**
* 获取上下文的key
*
* @return string
*/
private static function getContextKey()
{
return RequestContext::getServer()->getName() . '-' . RequestContext::get('fd');
}
}
20 changes: 20 additions & 0 deletions src/Controller/TcpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Imi\Controller;

/**
* TCP 控制器
*/
abstract class TcpController
{
/**
* 请求
* @var \Imi\Server\Tcp\Server
*/
public $server;

/**
* 桢
* @var \Imi\Server\TcpServer\Message\IReceiveData
*/
public $data;
}
20 changes: 20 additions & 0 deletions src/Controller/UdpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Imi\Controller;

/**
* UDP 控制器
*/
abstract class UdpController
{
/**
* 请求
* @var \Imi\Server\Udp\Server
*/
public $server;

/**
* 桢
* @var \Imi\Server\UdpServer\Message\IPacketData
*/
public $data;
}
14 changes: 13 additions & 1 deletion src/Db/Query/Where/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function toStringWithoutLogic()
return $this->rawSQL;
}
$result = $this->parseKeyword($this->fieldName) . ' ' . $this->operation . ' ';
switch($this->operation)
switch(strtolower($this->operation))
{
case 'between':
case 'not between':
Expand All @@ -143,6 +143,18 @@ public function toStringWithoutLogic()
$this->binds[$begin] = $this->value[0];
$this->binds[$end] = $this->value[1];
break;
case 'in':
case 'not in':
$result .= '(';
$valueNames = [];
foreach($this->value as $value)
{
$paramName = Query::getAutoParamName();
$valueNames[] = $paramName;
$this->binds[$paramName] = $value;
}
$result .= implode(',', $valueNames) . ')';
break;
default:
$value = Query::getAutoParamName();
$result .= $value;
Expand Down
20 changes: 9 additions & 11 deletions src/Listener/OnStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Imi\Server\Event\Param\ManagerStartEventParam;
use Imi\Server\Event\Listener\IManagerStartEventListener;
use Imi\Process\ProcessManager;
use Imi\Util\Swoole;

/**
* @Listener(eventName="IMI.MAIN_SERVER.MANAGER.START",priority=PHP_INT_MAX)
Expand All @@ -28,21 +29,18 @@ public function handle(ManagerStartEventParam $e)
// 进程PID记录
$fileName = File::path(dirname($_SERVER['SCRIPT_NAME']), 'imi.pid');
File::writeFile($fileName, json_encode([
'masterPID' => $e->server->getSwooleServer()->master_pid,
'managerPID' => $e->server->getSwooleServer()->manager_pid,
'masterPID' => Swoole::getMasterPID(),
'managerPID' => Swoole::getManagerPID(),
]));

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

Expand Down
8 changes: 4 additions & 4 deletions src/Listener/WorkerInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
use Imi\Cache\CacheManager;
use Imi\Bean\Annotation\Listener;
use Imi\Util\CoroutineChannelManager;
use Imi\Server\Event\Param\WorkStartEventParam;
use Imi\Server\Event\Listener\IWorkStartEventListener;
use Imi\Server\Event\Param\WorkerStartEventParam;
use Imi\Server\Event\Listener\IWorkerStartEventListener;

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

0 comments on commit 98d99bd

Please sign in to comment.