1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Console\Annotations; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Annotation\AnnotationBlock; |
6
|
|
|
use PhpBoot\Annotation\AnnotationTag; |
7
|
|
|
use PhpBoot\Console\ConsoleContainer; |
8
|
|
|
use PhpBoot\Controller\ControllerContainer; |
9
|
|
|
use PhpBoot\Entity\ContainerFactory; |
10
|
|
|
use PhpBoot\Entity\EntityContainerBuilder; |
11
|
|
|
use PhpBoot\Exceptions\AnnotationSyntaxException; |
12
|
|
|
use PhpBoot\Utils\AnnotationParams; |
13
|
|
|
use PhpBoot\Utils\Logger; |
14
|
|
|
use PhpBoot\Utils\TypeHint; |
15
|
|
|
|
16
|
|
|
class ParamAnnotationHandler |
17
|
|
|
{ |
18
|
|
|
|
19
|
1 |
View Code Duplication |
static public function getParamInfo($text) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
|
22
|
1 |
|
$paramType = null; |
23
|
1 |
|
$paramName = null; |
24
|
1 |
|
$paramDoc = ''; |
25
|
1 |
|
if(substr($text, 0, 1) == '$'){ //带$前缀的是变量 |
26
|
|
|
$params = new AnnotationParams($text, 2); |
27
|
|
|
$paramName = substr($params->getParam(0), 1); |
28
|
|
|
$paramDoc = $params->getRawParam(1, ''); |
29
|
|
|
}else{ |
30
|
1 |
|
$params = new AnnotationParams($text, 3); |
31
|
1 |
|
if ($params->count() >=2 && substr($params->getParam(1), 0, 1) == '$'){ |
32
|
1 |
|
$paramType = $params->getParam(0); //TODO 检测类型是否合法 |
33
|
1 |
|
$paramName = substr($params->getParam(1), 1); |
34
|
1 |
|
$paramDoc = $params->getRawParam(2, ''); |
35
|
1 |
|
}else{ |
36
|
|
|
\PhpBoot\abort(new AnnotationSyntaxException("@param $text syntax error")); |
37
|
|
|
} |
38
|
|
|
} |
39
|
1 |
|
return [$paramType, $paramName, $paramDoc]; |
40
|
|
|
} |
41
|
|
|
/** |
42
|
|
|
* @param ConsoleContainer $container |
43
|
|
|
* @param AnnotationBlock|AnnotationTag $ann |
44
|
|
|
* @param EntityContainerBuilder $entityBuilder |
45
|
|
|
*/ |
46
|
1 |
|
public function __invoke(ConsoleContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
47
|
|
|
{ |
48
|
1 |
|
if(!$ann->parent){ |
49
|
|
|
//Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
1 |
|
$target = $ann->parent->name; |
53
|
1 |
|
$command = $container->getCommand($target); |
54
|
1 |
|
if(!$command){ |
55
|
|
|
//Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
|
|
|
|
56
|
|
|
return ; |
57
|
|
|
} |
58
|
1 |
|
$className = $container->getClassName(); |
59
|
|
|
|
60
|
1 |
|
list($paramType, $paramName, $paramDoc) = self::getParamInfo($ann->description); |
61
|
|
|
|
62
|
1 |
|
$paramMeta = $command->getParamMeta($paramName); |
63
|
1 |
|
$paramMeta or \PhpBoot\abort(new AnnotationSyntaxException("$className::$target param $paramName not exist ")); |
|
|
|
|
64
|
|
|
//TODO 检测声明的类型和注释的类型是否匹配 |
65
|
1 |
View Code Duplication |
if($paramType){ |
|
|
|
|
66
|
1 |
|
$paramMeta->type = TypeHint::normalize($paramType, $className);//or \PhpBoot\abort(new AnnotationSyntaxException("{$container->getClassName()}::{$ann->parent->name} @{$ann->name} syntax error, param $paramName unknown type:$paramType ")); |
|
|
|
|
67
|
1 |
|
$container = ContainerFactory::create($entityBuilder, $paramMeta->type); |
68
|
1 |
|
$paramMeta->container = $container; |
69
|
1 |
|
} |
70
|
1 |
|
$paramMeta->description = $paramDoc; |
71
|
|
|
} |
72
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.