Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed May 9, 2020
1 parent f1ddd42 commit a563b97
Show file tree
Hide file tree
Showing 44 changed files with 276 additions and 210 deletions.
8 changes: 5 additions & 3 deletions src/Aop/Aop/FilterArgAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ class FilterArgAop
public function parse(JoinPoint $joinPoint)
{
$class = get_parent_class($joinPoint->getTarget());
$filterArgs = AnnotationManager::getMethodAnnotations($class, $joinPoint->getMethod(), FilterArg::class);
$args = ClassObject::convertArgsToKV($class, $joinPoint->getMethod(), $joinPoint->getArgs());
$method = $joinPoint->getMethod();
$filterArgs = AnnotationManager::getMethodAnnotations($class, $method, FilterArg::class);
$args = ClassObject::convertArgsToKV($class, $method, $joinPoint->getArgs());

foreach($filterArgs as $filterArg)
{
$args[$filterArg->name] = ($filterArg->filter)($args[$filterArg->name]);
$name = $filterArg->name;
$args[$name] = ($filterArg->filter)($args[$name]);
}

$args = array_values($args);
Expand Down
5 changes: 3 additions & 2 deletions src/Aop/Aop/InjectArgAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class InjectArgAop
public function parse(AroundJoinPoint $joinPoint)
{
$class = get_parent_class($joinPoint->getTarget());
$injectArgs = AnnotationManager::getMethodAnnotations($class, $joinPoint->getMethod(), InjectArg::class);
$args = ClassObject::convertArgsToKV($class, $joinPoint->getMethod(), $joinPoint->getArgs());
$method = $joinPoint->getMethod();
$injectArgs = AnnotationManager::getMethodAnnotations($class, $method, InjectArg::class);
$args = ClassObject::convertArgsToKV($class, $method, $joinPoint->getArgs());

foreach($injectArgs as $injectArg)
{
Expand Down
7 changes: 4 additions & 3 deletions src/Bean/Annotation/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function __construct($data = [])

foreach($refClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $property)
{
$value = $this->{$property->name};
unset($this->{$property->name});
$this->{$property->name} = $value;
$propertyName = $property->name;
$value = $this->$propertyName;
unset($this->$propertyName);
$this->$propertyName = $value;
}

}
Expand Down
20 changes: 12 additions & 8 deletions src/Bean/AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public function parseClass(\ReflectionClass $ref)
$thisClasses = &$this->classes;
if($this->checkAnnotations($annotations))
{
$thisClasses[$className] = $ref->getFileName();
$this->files[$ref->getFileName()] = 1;
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->files[$fileName] = 1;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -193,8 +194,9 @@ public function parseMethod(\ReflectionClass $ref, \ReflectionMethod $method)
$thisClasses = &$this->classes;
if($this->checkAnnotations($annotations))
{
$thisClasses[$className] = $ref->getFileName();
$this->files[$ref->getFileName()] = 1;
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->files[$fileName] = 1;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -281,8 +283,9 @@ public function parseProp(\ReflectionClass $ref, \ReflectionProperty $prop)
$thisClasses = &$this->classes;
if($this->checkAnnotations($annotations))
{
$thisClasses[$className] = $ref->getFileName();
$this->files[$ref->getFileName()] = 1;
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->files[$fileName] = 1;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down Expand Up @@ -371,8 +374,9 @@ public function parseConst(\ReflectionClass $ref, \ReflectionClassConstant $cons
$thisClasses = &$this->classes;
if($this->checkAnnotations($annotations))
{
$thisClasses[$className] = $ref->getFileName();
$this->files[$ref->getFileName()] = 1;
$fileName = $ref->getFileName();
$thisClasses[$className] = $fileName;
$this->files[$fileName] = 1;

// @Inherit 注解继承父级的注解
$hasInherit = false;
Expand Down
18 changes: 10 additions & 8 deletions src/Bean/BeanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static function getTpl($ref, $newClassName)
$aopConstruct = <<<TPL
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
\$__result__ = \$this->beanProxy->call(
\$__result__ = \$beanProxy->call(
\$this,
'__construct',
function({$paramsTpls['define']}){
Expand Down Expand Up @@ -188,7 +188,7 @@ class {$newClassName} extends {$class} implements \Imi\Bean\IBean
public function __construct({$constructDefine})
{
\$this->beanProxy = new \Imi\Bean\BeanProxy(\$this);
\$this->beanProxy = \$beanProxy = new \Imi\Bean\BeanProxy(\$this);
{$aopConstruct}
}
Expand All @@ -214,25 +214,26 @@ private static function getMethodsTpl($ref)
$tpl = '';
foreach($ref->getMethods(\ReflectionMethod::IS_PUBLIC) as $method)
{
if($method->isStatic() || '__construct' === $method->name || $method->isFinal() || !static::hasAop($ref, $method))
$methodName = $method->name;
if($method->isStatic() || '__construct' === $methodName || $method->isFinal() || !static::hasAop($ref, $method))
{
continue;
}
$paramsTpls = static::getMethodParamTpls($method);
$methodReturnType = static::getMethodReturnType($method);
$returnsReference = $method->returnsReference() ? '&' : '';
$tpl .= <<<TPL
public function {$returnsReference}{$method->name}({$paramsTpls['define']}){$methodReturnType}
public function {$returnsReference}{$methodName}({$paramsTpls['define']}){$methodReturnType}
{
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
\$__result__ = \$this->beanProxy->call(
\$this,
'{$method->name}',
'{$methodName}',
function({$paramsTpls['define']}){
\$__args__ = func_get_args();
{$paramsTpls['set_args']}
return parent::{$method->name}(...\$__args__);
return parent::{$methodName}(...\$__args__);
},
\$__args__
);
Expand Down Expand Up @@ -275,8 +276,9 @@ private static function getMethodParamTpls(\ReflectionMethod $method)
// 引用传参
if($param->isPassedByReference())
{
$setArgs .= '$__args__[' . $i . '] = &$' . $param->name . ';';
$setArgsBack .= '$' . $param->name . ' = $__args__[' . $i . '];';
$paramName = $param->name;
$setArgs .= '$__args__[' . $i . '] = &$' . $paramName . ';';
$setArgsBack .= '$' . $paramName . ' = $__args__[' . $i . '];';
}
}
foreach($result as &$item)
Expand Down
22 changes: 12 additions & 10 deletions src/Bean/BeanProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ private function init($object)
$aspects = AnnotationManager::getAnnotationPoints(Aspect::class);
foreach($aspects as $item)
{
$itemClass = $item->getClass();
$itemPriority = $item->getAnnotation()->priority;
// 判断是否属于当前类方法的切面
$pointCutsSet = AnnotationManager::getMethodsAnnotations($item->getClass(), PointCut::class);
$pointCutsSet = AnnotationManager::getMethodsAnnotations($itemClass, PointCut::class);
foreach($pointCutsSet as $methodName => $pointCuts)
{
foreach($pointCuts as $pointCut)
Expand All @@ -121,10 +123,10 @@ private function init($object)
if(Imi::checkClassRule($allowItem, $className))
{
static::$aspects[$className]->insert([
'class' => $item->getClass(),
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
], $item->getAnnotation()->priority);
], $itemPriority);
break;
}
}
Expand All @@ -140,10 +142,10 @@ private function init($object)
if($annotation instanceof $allowItem)
{
static::$aspects[$className]->insert([
'class' => $item->getClass(),
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
], $item->getAnnotation()->priority);
], $itemPriority);
break 3;
}
}
Expand All @@ -154,7 +156,7 @@ private function init($object)
}
}
// 判断是否属于当前类的切面
$pointCuts = AnnotationManager::getMethodsAnnotations($item->getClass(), PointCut::class);
$pointCuts = AnnotationManager::getMethodsAnnotations($itemClass, PointCut::class);
foreach($pointCuts as $methodName => $pointCuts)
{
foreach($pointCuts as $pointCut)
Expand All @@ -168,10 +170,10 @@ private function init($object)
if(Imi::checkRuleMatch($allowItem, $className))
{
static::$aspects[$className]->insert([
'class' => $item->getClass(),
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
], $item->getAnnotation()->priority);
], $itemPriority);
break;
}
}
Expand All @@ -186,10 +188,10 @@ private function init($object)
if($annotation instanceof $allowItem)
{
static::$aspects[$className]->insert([
'class' => $item->getClass(),
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
], $item->getAnnotation()->priority);
], $itemPriority);
break 2;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Bean/Parser/ListenerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public function parse(\Imi\Bean\Annotation\Base $annotation, string $className,
{
if($annotation instanceof \Imi\Bean\Annotation\Listener)
{
$this->data[] = [$annotation->eventName, $className, $annotation->priority];
Event::on($annotation->eventName, $className, $annotation->priority);
$eventName = $annotation->eventName;
$priority = $annotation->priority;
$this->data[] = [$eventName, $className, $priority];
Event::on($eventName, $className, $priority);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Cache/Aop/CacheEvictAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ class CacheEvictAop
public function parseCacheEvict(AroundJoinPoint $joinPoint)
{
$class = get_parent_class($joinPoint->getTarget());
$method = $joinPoint->getMethod();

// CacheEvict 注解列表
$cacheEvicts = AnnotationManager::getMethodAnnotations($class, $joinPoint->getMethod(), CacheEvict::class);
$cacheEvicts = AnnotationManager::getMethodAnnotations($class, $method, CacheEvict::class);

// 方法参数
$args = ClassObject::convertArgsToKV($class, $joinPoint->getMethod(), $joinPoint->getArgs());
$args = ClassObject::convertArgsToKV($class, $method, $joinPoint->getArgs());

foreach($cacheEvicts as $index => $cacheEvict)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Cache/Aop/CachePutAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ class CachePutAop
public function parseCachePut(AroundJoinPoint $joinPoint)
{
$methodReturn = $joinPoint->proceed();
$method = $joinPoint->getMethod();

$class = get_parent_class($joinPoint->getTarget());

// CachePut 注解列表
$cachePuts = AnnotationManager::getMethodAnnotations($class, $joinPoint->getMethod(), CachePut::class);
$cachePuts = AnnotationManager::getMethodAnnotations($class, $method, CachePut::class);

// 方法参数
$args = ClassObject::convertArgsToKV($class, $joinPoint->getMethod(), $joinPoint->getArgs());
$args = ClassObject::convertArgsToKV($class, $method, $joinPoint->getArgs());

foreach($cachePuts as $cachePut)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Cache/Aop/CacheableAop.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ class CacheableAop
*/
public function parseCacheable(AroundJoinPoint $joinPoint)
{
$class = get_parent_class($joinPoint->getTarget());
$target = $joinPoint->getTarget();
$class = get_parent_class($target);
$method = $joinPoint->getMethod();
$joinPointArgs = $joinPoint->getArgs();

// Cacheable 注解
$cacheable = AnnotationManager::getMethodAnnotations($class, $joinPoint->getMethod(), Cacheable::class)[0] ?? null;
$cacheable = AnnotationManager::getMethodAnnotations($class, $method, Cacheable::class)[0] ?? null;

// 方法参数
$args = ClassObject::convertArgsToKV($class, $joinPoint->getMethod(), $joinPoint->getArgs());
$args = ClassObject::convertArgsToKV($class, $method, $joinPointArgs);

// 缓存名
$name = $cacheable->name;
Expand Down Expand Up @@ -71,7 +74,7 @@ public function parseCacheable(AroundJoinPoint $joinPoint)
{
// 加锁
$nextProceedExeced = false;
$this->parseLockable($joinPoint->getTarget(), $joinPoint->getMethod(), $joinPoint->getArgs(), $cacheable->lockable, function() use(&$cacheValue, $joinPoint, &$nextProceedExeced){
$this->parseLockable($target, $method, $joinPointArgs, $cacheable->lockable, function() use(&$cacheValue, $joinPoint, &$nextProceedExeced){
$nextProceedExeced = true;
$cacheValue = $joinPoint->proceed();
}, function() use($cacheInstance, $key, &$cacheValue){
Expand Down
8 changes: 4 additions & 4 deletions src/Cron/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct($options = [])
{
throw new \InvalidArgumentException('If you want to use Swoole Shared Memory, you must set the "socketFile" option');
}
$this->socketFile = $this->options['socketFile'];
$this->socketFile = $options['socketFile'];
}

/**
Expand All @@ -54,13 +54,13 @@ public function connect(): bool
{
return true;
}
$this->socket = stream_socket_client('unix://' . $this->socketFile, $errno, $errstr, 10);
if(false === $this->socket)
$this->socket = $socket = stream_socket_client('unix://' . $this->socketFile, $errno, $errstr, 10);
if(false === $socket)
{
$this->connected = false;
return false;
}
stream_set_timeout($this->socket, 10);
stream_set_timeout($socket, 10);
$this->connected = true;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Cron/Listener/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public function handle(EventParam $e)
};
break;
case CronTaskType::PROCESS:
$task = function($id, $data) use($class){
$task = function($id, $data) use($class, $cronManager){
ProcessManager::run('CronWorkerProcess', [
'id' => $id,
'data' => json_encode($data),
'class' => $class,
'cronSock' => $this->cronManager->getSocketFile(),
'cronSock' => $cronManager->getSocketFile(),
]);
};
break;
Expand Down
13 changes: 7 additions & 6 deletions src/Cron/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,22 @@ public function runTask($task)
public function completeTask(Result $result)
{
$runningTasks = &$this->runningTasks;
if(isset($runningTasks[$result->id]))
$resultId = $result->id;
if(isset($runningTasks[$resultId]))
{
if(!$this->cronLock->unlock($runningTasks[$result->id]))
if(!$this->cronLock->unlock($runningTasks[$resultId]))
{
Log::error(sprintf('Task %s unlock failed', $result->id));
Log::error(sprintf('Task %s unlock failed', $resultId));
}
unset($runningTasks[$result->id]);
unset($runningTasks[$resultId]);
}
if($result->success)
{
Log::info(sprintf('Task: %s, Process: %s#%s, Success', $result->id, $result->processType, $result->processId));
Log::info(sprintf('Task: %s, Process: %s#%s, Success', $resultId, $result->processType, $result->processId));
}
else
{
Log::error(sprintf('Task: %s, Process: %s#%s, %s', $result->id, $result->processType, $result->processId, $result->message));
Log::error(sprintf('Task: %s, Process: %s#%s, %s', $resultId, $result->processType, $result->processId, $result->message));
}
}

Expand Down
Loading

0 comments on commit a563b97

Please sign in to comment.