diff --git a/src/com_jjcbs/fun/Main.php b/src/com_jjcbs/fun/Main.php index 9699d79..770b513 100644 --- a/src/com_jjcbs/fun/Main.php +++ b/src/com_jjcbs/fun/Main.php @@ -65,4 +65,13 @@ public static function getClassNameFromFile(&$input){ return ''; } + /** + * get short namespace name + * @param string $namespace + */ + public static function getShortName(string $namespace){ + $tempArr = explode('\\' , $namespace); + return $tempArr[count($tempArr)-1]; + } + } \ No newline at end of file diff --git a/src/com_jjcbs/lib/annotation/Autowired.php b/src/com_jjcbs/lib/annotation/Autowired.php index f586828..334efc5 100644 --- a/src/com_jjcbs/lib/annotation/Autowired.php +++ b/src/com_jjcbs/lib/annotation/Autowired.php @@ -10,7 +10,10 @@ use com_jjcbs\exceptions\AnnotationException; +use com_jjcbs\fun\Main; use com_jjcbs\lib\AnnotationMethodAbstract; +use com_jjcbs\lib\ServiceFactory; +use com_jjcbs\service\AnnotationServiceImpl; class Autowired extends AnnotationMethodAbstract { @@ -35,13 +38,28 @@ static protected function parsedVar($data = null) static protected function do() { // TODO: Implement do() method. - static::useNamespace('com_jjcbs\\lib\\ServiceFactory'); - static::useNamespace(static::$param['type']); - $tpl = <<getSrcClass(); + $reflectionClass = new \ReflectionClass($namespace); + $obj = $reflectionClass->newInstanceWithoutConstructor(); + $property = $reflectionClass->getProperty(self::$argv['varName']); + $property->setAccessible(true); + $val = $property->getValue($obj); + if (empty($val)) return ''; + static::useNamespace($val); + //check is service + $ref = new \ReflectionClass($val); + $tpl = <<%s = new %s(); +PHP; + if ($parent = $ref->getParentClass()) { + if ( $parent->getName() == 'service'){ + static::useNamespace('com_jjcbs\\lib\\ServiceFactory'); + $tpl = <<%s = ServiceFactory::getInstance(%s::class); EOT; - $tempArr = explode('\\' , static::$param['type']); - return sprintf($tpl, static::$argv['varName'],$tempArr[count($tempArr)-1]); + } + } + return sprintf($tpl, static::$argv['varName'], Main::getShortName($val)); } static protected function exception(AnnotationException $exception) diff --git a/src/com_jjcbs/lib/annotation/OutPutFormat.php b/src/com_jjcbs/lib/annotation/OutPutFormat.php index 2ebaeb1..0750188 100644 --- a/src/com_jjcbs/lib/annotation/OutPutFormat.php +++ b/src/com_jjcbs/lib/annotation/OutPutFormat.php @@ -45,7 +45,7 @@ static protected function do() switch (self::$param['type']){ case 'json': if(isset(self::$argv['varName'])) return sprintf('return json_decode($this->%s , true);' , self::$argv['varName']); - if(isset(self::$argv['methodName'])) return 'return json_decode(%s , true);'; + if(isset(self::$argv['methodName'])) return ';return json_decode(%s , true);'; break; default:break; diff --git a/src/com_jjcbs/lib/annotation/Rpc.php b/src/com_jjcbs/lib/annotation/Rpc.php deleted file mode 100644 index 772aa28..0000000 --- a/src/com_jjcbs/lib/annotation/Rpc.php +++ /dev/null @@ -1,47 +0,0 @@ - 1, - 'age' => 2 - ] ; - } -} \ No newline at end of file