Skip to content

Commit

Permalink
Merge pull request #13 from ndj888/dev
Browse files Browse the repository at this point in the history
修复优化注解
  • Loading branch information
ndj888 authored Nov 30, 2017
2 parents 3537d19 + d687c20 commit bc8eeda
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 78 deletions.
9 changes: 9 additions & 0 deletions src/com_jjcbs/fun/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

}
28 changes: 23 additions & 5 deletions src/com_jjcbs/lib/annotation/Autowired.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 = <<<EOT
$namespace = ServiceFactory::getInstance(AnnotationServiceImpl::class)->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 = <<<PHP
\$this->%s = new %s();
PHP;
if ($parent = $ref->getParentClass()) {
if ( $parent->getName() == 'service'){
static::useNamespace('com_jjcbs\\lib\\ServiceFactory');
$tpl = <<<EOT
\$this->%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)
Expand Down
2 changes: 1 addition & 1 deletion src/com_jjcbs/lib/annotation/OutPutFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 0 additions & 47 deletions src/com_jjcbs/lib/annotation/Rpc.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/com_jjcbs/test/resource/TestAutowired.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

//{use template}

use com_jjcbs\service\AnnotationServiceImpl;

class TestAutowired
{
/**
* @@com_jjcbs\lib\annotation\Autowired(type="com_jjcbs\service\AnnotationServiceImpl")
* @@com_jjcbs\lib\annotation\Autowired()
* @var
*/
private $annotationService;
private $annotationService = AnnotationServiceImpl::class;

public function __construct()
{
Expand Down
23 changes: 0 additions & 23 deletions src/com_jjcbs/test/resource/scan/TestRpcAnnotation.php

This file was deleted.

0 comments on commit bc8eeda

Please sign in to comment.