1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBoot\Controller\Annotations; |
4
|
|
|
|
5
|
|
|
use PhpBoot\Annotation\AnnotationBlock; |
6
|
|
|
use PhpBoot\Annotation\AnnotationTag; |
7
|
|
|
use PhpBoot\Controller\ControllerContainer; |
8
|
|
|
use PhpBoot\Entity\ContainerFactory; |
9
|
|
|
use PhpBoot\Entity\EntityContainerBuilder; |
10
|
|
|
use PhpBoot\Exceptions\AnnotationSyntaxException; |
11
|
|
|
use PhpBoot\Metas\ReturnMeta; |
12
|
|
|
use PhpBoot\Utils\AnnotationParams; |
13
|
|
|
use PhpBoot\Utils\Logger; |
14
|
|
|
|
15
|
|
|
class BindAnnotationHandler |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param ControllerContainer $container |
19
|
|
|
* @param AnnotationBlock|AnnotationTag $ann |
20
|
|
|
* @param EntityContainerBuilder $entityBuilder |
21
|
|
|
*/ |
22
|
3 |
|
public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
23
|
|
|
{ |
24
|
3 |
|
if(!$ann->parent || !$ann->parent->parent){ |
25
|
|
|
Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent param/return"); |
26
|
|
|
return; |
27
|
|
|
} |
28
|
3 |
|
$target = $ann->parent->parent->name; |
29
|
3 |
|
$route = $container->getRoute($target); |
30
|
3 |
|
if(!$route){ |
31
|
|
|
Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent param/return"); |
32
|
|
|
return ; |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
$params = new AnnotationParams($ann->description, 2); |
36
|
|
|
|
37
|
3 |
|
$params->count()>0 or \PhpBoot\abort(new AnnotationSyntaxException("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target require 1 param, {$params->count()} given")); |
38
|
|
|
|
39
|
3 |
|
$handler = $route->getResponseHandler(); |
40
|
|
|
|
41
|
3 |
|
if ($ann->parent->name == 'return'){ |
42
|
|
|
list($target, $return) = $handler->getMappingBySource('return'); |
43
|
|
|
if($return){ |
44
|
|
|
$handler->eraseMapping($target); |
45
|
|
|
$handler->setMapping($params[0], $return); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
}elseif($ann->parent->name == 'param'){ |
49
|
3 |
|
list($paramType, $paramName, $paramDoc) = ParamAnnotationHandler::getParamInfo($ann->parent->description); |
|
|
|
|
50
|
|
|
|
51
|
3 |
|
$paramMeta = $route->getRequestHandler()->getParamMeta($paramName); |
52
|
3 |
|
if($paramMeta->isPassedByReference){ |
53
|
3 |
|
list($target, $ori) = $handler->getMappingBySource('params.'.$paramName); |
54
|
3 |
|
if($ori){ |
55
|
3 |
|
$handler->eraseMapping($target); |
56
|
3 |
|
} |
57
|
|
|
//输出绑定 |
58
|
3 |
|
$handler->setMapping( |
59
|
3 |
|
$params[0], |
60
|
3 |
|
new ReturnMeta( |
61
|
3 |
|
'params.'.$paramMeta->name, |
62
|
3 |
|
$paramMeta->type, $paramDoc, |
63
|
3 |
|
ContainerFactory::create($entityBuilder, $paramMeta->type) |
64
|
3 |
|
) |
65
|
3 |
|
); |
66
|
3 |
|
}else{ |
67
|
3 |
|
$paramMeta->source = $params[0]; |
68
|
|
|
} |
69
|
3 |
|
} |
70
|
|
|
} |
71
|
|
|
} |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.