Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed May 27, 2020
1 parent c50d3c1 commit 72c7761
Show file tree
Hide file tree
Showing 17 changed files with 301 additions and 183 deletions.
115 changes: 74 additions & 41 deletions src/Bean/Annotation/AnnotationManager.php
Expand Up @@ -81,11 +81,16 @@ public static function getAnnotationRelation(): AnnotationRelation
*/
public static function addClassAnnotations($className, ...$annotations)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
static::$annotations[$className] = new ClassAnnotation($className);
$classAnnotation = $staticAnnotations[$className];
}
static::$annotations[$className]->addClassAnnotations($annotations);
else
{
$staticAnnotations[$className] = $classAnnotation = new ClassAnnotation($className);
}
$classAnnotation->addClassAnnotations($annotations);
foreach($annotations as $annotation)
{
static::$annotationRelation->addClassRelation(new ClassAnnotationRelation($className, $annotation));
Expand All @@ -101,14 +106,15 @@ public static function addClassAnnotations($className, ...$annotations)
*/
public static function setClassAnnotations($className, ...$annotations)
{
if(isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$tmpAnnotations = static::$annotations[$className]->getClassAnnotations();
$tmpAnnotations = $staticAnnotations[$className]->getClassAnnotations();
foreach($tmpAnnotations as $annotation)
{
static::$annotationRelation->removeClassRelation(get_class($annotation), $className);
}
unset(static::$annotations[$className]);
unset($staticAnnotations[$className]);
}
static::addClassAnnotations($className, ...$annotations);
}
Expand All @@ -123,11 +129,16 @@ public static function setClassAnnotations($className, ...$annotations)
*/
public static function addMethodAnnotations($className, $methodName, ...$annotations)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$classAnnotation = $staticAnnotations[$className];
}
else
{
static::$annotations[$className] = new ClassAnnotation($className);
$staticAnnotations[$className] = $classAnnotation = new ClassAnnotation($className);
}
static::$annotations[$className]->addMethodAnnotations($methodName, $annotations);
$classAnnotation->addMethodAnnotations($methodName, $annotations);
foreach($annotations as $annotation)
{
static::$annotationRelation->addMethodRelation(new MethodAnnotationRelation($className, $methodName, $annotation));
Expand All @@ -144,9 +155,10 @@ public static function addMethodAnnotations($className, $methodName, ...$annotat
*/
public static function setMethodAnnotations($className, $methodName, ...$annotations)
{
if(isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$tmpAnnotations = static::$annotations[$className]->getMethodAnnotations($methodName);
$tmpAnnotations = $staticAnnotations[$className]->getMethodAnnotations($methodName);
foreach($tmpAnnotations as $annotation)
{
static::$annotationRelation->removeMethodRelation(get_class($annotation), $className, $methodName);
Expand All @@ -165,11 +177,16 @@ public static function setMethodAnnotations($className, $methodName, ...$annotat
*/
public static function addPropertyAnnotations($className, $propertyName, ...$annotations)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
static::$annotations[$className] = new ClassAnnotation($className);
$classAnnotation = $staticAnnotations[$className];
}
static::$annotations[$className]->addpropertyAnnotations($propertyName, $annotations);
else
{
$staticAnnotations[$className] = $classAnnotation = new ClassAnnotation($className);
}
$classAnnotation->addpropertyAnnotations($propertyName, $annotations);
foreach($annotations as $annotation)
{
static::$annotationRelation->addPropertyRelation(new PropertyAnnotationRelation($className, $propertyName, $annotation));
Expand All @@ -186,9 +203,10 @@ public static function addPropertyAnnotations($className, $propertyName, ...$ann
*/
public static function setPropertyAnnotations($className, $propertyName, ...$annotations)
{
if(isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$tmpAnnotations = static::$annotations[$className]->getPropertyAnnotations($propertyName);
$tmpAnnotations = $staticAnnotations[$className]->getPropertyAnnotations($propertyName);
foreach($tmpAnnotations as $annotation)
{
static::$annotationRelation->removePropertyRelation(get_class($annotation), $className, $propertyName);
Expand All @@ -207,11 +225,16 @@ public static function setPropertyAnnotations($className, $propertyName, ...$ann
*/
public static function addConstantAnnotations($className, $constantName, ...$annotations)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$classAnnotation = $staticAnnotations[$className];
}
else
{
static::$annotations[$className] = new ClassAnnotation($className);
$staticAnnotations[$className] = $classAnnotation = new Constant($className);
}
static::$annotations[$className]->addConstantAnnotations($constantName, $annotations);
$classAnnotation->addConstantAnnotations($constantName, $annotations);
foreach($annotations as $annotation)
{
static::$annotationRelation->addConstantRelation(new ConstantAnnotationRelation($className, $constantName, $annotation));
Expand All @@ -228,9 +251,10 @@ public static function addConstantAnnotations($className, $constantName, ...$ann
*/
public static function setConstantAnnotations($className, $constantName, ...$annotations)
{
if(isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
$tmpAnnotations = static::$annotations[$className]->getConstantAnnotations($constantName);
$tmpAnnotations = $staticAnnotations[$className]->getConstantAnnotations($constantName);
foreach($tmpAnnotations as $annotation)
{
static::$annotationRelation->removeConstantRelation(get_class($annotation), $className, $constantName);
Expand Down Expand Up @@ -261,11 +285,12 @@ public static function getAnnotationPoints($annotationClassName, $where = null)
*/
public static function getClassAnnotations($className, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotations = static::$annotations[$className]->getClassAnnotations();
$annotations = $staticAnnotations[$className]->getClassAnnotations();
if(null === $annotationClassName)
{
return $annotations;
Expand Down Expand Up @@ -295,11 +320,12 @@ public static function getClassAnnotations($className, $annotationClassName = nu
*/
public static function getMethodAnnotations($className, $methodName, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotations = static::$annotations[$className]->getMethodAnnotations($methodName);
$annotations = $staticAnnotations[$className]->getMethodAnnotations($methodName);
if(null === $annotationClassName)
{
return $annotations;
Expand Down Expand Up @@ -329,11 +355,12 @@ public static function getMethodAnnotations($className, $methodName, $annotation
*/
public static function getPropertyAnnotations($className, $propertyName, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotations = static::$annotations[$className]->getPropertyAnnotations($propertyName);
$annotations = $staticAnnotations[$className]->getPropertyAnnotations($propertyName);
if(null === $annotationClassName)
{
return $annotations;
Expand Down Expand Up @@ -363,11 +390,12 @@ public static function getPropertyAnnotations($className, $propertyName, $annota
*/
public static function getConstantAnnotations($className, $constantName, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotations = static::$annotations[$className]->getConstantAnnotations($constantName);
$annotations = $staticAnnotations[$className]->getConstantAnnotations($constantName);
if(null === $annotationClassName)
{
return $annotations;
Expand Down Expand Up @@ -395,11 +423,12 @@ public static function getConstantAnnotations($className, $constantName, $annota
*/
public static function getMethodsAnnotations($className, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotationList = static::$annotations[$className]->getMethodAnnotations();
$annotationList = $staticAnnotations[$className]->getMethodAnnotations();
if(null === $annotationClassName)
{
return $annotationList;
Expand Down Expand Up @@ -427,11 +456,12 @@ public static function getMethodsAnnotations($className, $annotationClassName =
*/
public static function getPropertiesAnnotations($className, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotationList = static::$annotations[$className]->getPropertyAnnotations();
$annotationList = $staticAnnotations[$className]->getPropertyAnnotations();
if(null === $annotationClassName)
{
return $annotationList;
Expand Down Expand Up @@ -459,11 +489,12 @@ public static function getPropertiesAnnotations($className, $annotationClassName
*/
public static function getConstantsAnnotations($className, $annotationClassName = null)
{
if(!isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(!isset($staticAnnotations[$className]))
{
return [];
}
$annotationList = static::$annotations[$className]->getConstantAnnotations();
$annotationList = $staticAnnotations[$className]->getConstantAnnotations();
if(null === $annotationClassName)
{
return $annotationList;
Expand All @@ -490,34 +521,36 @@ public static function getConstantsAnnotations($className, $annotationClassName
*/
public static function clearClassAllAnnotations($className)
{
if(isset(static::$annotations[$className]))
$staticAnnotations = &static::$annotations;
if(isset($staticAnnotations[$className]))
{
foreach(static::$annotations[$className]->getClassAnnotations() as $annotation)
$classAnnotation = $staticAnnotations[$className];
foreach($classAnnotation->getClassAnnotations() as $annotation)
{
static::$annotationRelation->removeClassRelation(get_class($annotation), $className);
}
foreach(static::$annotations[$className]->getMethodAnnotations() as $methodName => $annotations)
foreach($classAnnotation->getMethodAnnotations() as $methodName => $annotations)
{
foreach($annotations as $annotation)
{
static::$annotationRelation->removeMethodRelation(get_class($annotation), $className, $methodName);
}
}
foreach(static::$annotations[$className]->getPropertyAnnotations() as $propertyName => $annotations)
foreach($classAnnotation->getPropertyAnnotations() as $propertyName => $annotations)
{
foreach($annotations as $annotation)
{
static::$annotationRelation->removePropertyRelation(get_class($annotation), $className, $propertyName);
}
}
foreach(static::$annotations[$className]->getConstantAnnotations() as $constName => $annotations)
foreach($classAnnotation->getConstantAnnotations() as $constName => $annotations)
{
foreach($annotations as $annotation)
{
static::$annotationRelation->removeConstantRelation(get_class($annotation), $className, $constName);
}
}
unset(static::$annotations[$className]);
unset($staticAnnotations[$className]);
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/Bean/BeanFactory.php
Expand Up @@ -39,15 +39,20 @@ abstract class BeanFactory
*/
public static function newInstance($class, ...$args)
{
if(!isset(static::$classNameMap[$class]))
$classNameMap = &static::$classNameMap;
if(isset($classNameMap[$class]))
{
$className = $classNameMap[$class];
}
else
{
$ref = ReflectionContainer::getClassReflection($class);
$className = static::getNewClassName($ref->getShortName());
$tpl = static::getTpl($ref, $className);
Imi::eval($tpl);
static::$classNameMap[$class] = $className;
$classNameMap[$class] = $className;
}
$object = new static::$classNameMap[$class](...$args);
$object = new $className(...$args);
static::initInstance($object, $args);
return $object;
}
Expand All @@ -61,15 +66,20 @@ public static function newInstance($class, ...$args)
*/
public static function newInstanceNoInit($class, ...$args)
{
if(!isset(static::$classNameMap[$class]))
$classNameMap = &static::$classNameMap;
if(isset($classNameMap[$class]))
{
$className = $classNameMap[$class];
}
else
{
$ref = ReflectionContainer::getClassReflection($class);
$className = static::getNewClassName($ref->getShortName());
$tpl = static::getTpl($ref, $className);
Imi::eval($tpl);
static::$classNameMap[$class] = $className;
$classNameMap[$class] = $className;
}
return new static::$classNameMap[$class](...$args);
return new $className(...$args);
}

/**
Expand Down
13 changes: 7 additions & 6 deletions src/Bean/BeanProxy.php
Expand Up @@ -102,9 +102,10 @@ private function init($object)
$this->className = $className = BeanFactory::getObjectClass($object);
$this->refClass = $refClass = ReflectionContainer::getClassReflection($className);
// 每个类只需处理一次
if(!isset(static::$aspects[$className]))
$staticAspects = &static::$aspects;
if(!isset($staticAspects[$className]))
{
static::$aspects[$className] = new \SplPriorityQueue;
$staticAspects[$className] = $aspect = new \SplPriorityQueue;
$aspects = AnnotationManager::getAnnotationPoints(Aspect::class);
foreach($aspects as $item)
{
Expand All @@ -123,7 +124,7 @@ private function init($object)
{
if(Imi::checkClassRule($allowItem, $className))
{
static::$aspects[$className]->insert([
$aspect->insert([
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
Expand All @@ -142,7 +143,7 @@ private function init($object)
{
if($annotation instanceof $allowItem)
{
static::$aspects[$className]->insert([
$aspect->insert([
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
Expand Down Expand Up @@ -170,7 +171,7 @@ private function init($object)
{
if(Imi::checkRuleMatch($allowItem, $className))
{
static::$aspects[$className]->insert([
$aspect->insert([
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
Expand All @@ -188,7 +189,7 @@ private function init($object)
{
if($annotation instanceof $allowItem)
{
static::$aspects[$className]->insert([
$aspect->insert([
'class' => $itemClass,
'method' => $methodName,
'pointCut' => $pointCut,
Expand Down

0 comments on commit 72c7761

Please sign in to comment.