|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the BEAR.Resource package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace BEAR\Resource; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\Resource\Annotation\ResourceParam; |
|
10
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
11
|
|
|
use Doctrine\Common\Cache\Cache; |
|
12
|
|
|
use Ray\Di\Di\Assisted; |
|
13
|
|
|
use Ray\Di\InjectorInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class NamedParameter implements NamedParameterInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Cache |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cache; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Reader |
|
24
|
|
|
*/ |
|
25
|
|
|
private $reader; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var InjectorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $injector; |
|
31
|
|
|
|
|
32
|
87 |
|
public function __construct(Cache $cache, Reader $reader, InjectorInterface $injector) |
|
33
|
|
|
{ |
|
34
|
87 |
|
$this->cache = $cache; |
|
35
|
87 |
|
$this->reader = $reader; |
|
36
|
87 |
|
$this->injector = $injector; |
|
37
|
87 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
55 |
|
public function getParameters(array $callable, array $query) |
|
43
|
|
|
{ |
|
44
|
55 |
|
$id = __CLASS__ . get_class($callable[0]) . $callable[1]; |
|
45
|
55 |
|
$names = $this->cache->fetch($id); |
|
46
|
55 |
|
if (! $names) { |
|
47
|
54 |
|
$names = $this->getNamedParamMetas($callable); |
|
48
|
54 |
|
$this->cache->save($id, $names); |
|
49
|
|
|
} |
|
50
|
55 |
|
$parameters = $this->evaluateParams($query, $names); |
|
51
|
|
|
|
|
52
|
50 |
|
return $parameters; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string[] $query caller value |
|
57
|
|
|
* @param string[] $names default value ['param-name' => 'param-type|param-value'] |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
55 |
|
private function evaluateParams(array $query, array $names) |
|
62
|
|
|
{ |
|
63
|
55 |
|
$parameters = []; |
|
64
|
55 |
|
foreach ($names as $varName => $param) { |
|
65
|
|
|
/* @var $param ParamInterface */ |
|
66
|
47 |
|
$parameters[] = $param($varName, $query, $this->injector); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
50 |
|
return $parameters; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return named parameter information |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $callable |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
54 |
|
private function getNamedParamMetas(array $callable) |
|
80
|
|
|
{ |
|
81
|
54 |
|
$method = new \ReflectionMethod($callable[0], $callable[1]); |
|
82
|
54 |
|
$parameters = $method->getParameters(); |
|
83
|
54 |
|
$names = []; |
|
84
|
54 |
|
foreach ($parameters as $parameter) { |
|
85
|
46 |
|
$names[$parameter->name] = $parameter->isDefaultValueAvailable() === true ? new OptionalParam($parameter->getDefaultValue()) : new RequiredParam; |
|
86
|
|
|
} |
|
87
|
54 |
|
$names = $this->overrideAssistedParam($method, $names); |
|
88
|
|
|
|
|
89
|
54 |
|
return $names; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
54 |
|
private function overrideAssistedParam(\ReflectionMethod $method, array $names) |
|
96
|
|
|
{ |
|
97
|
54 |
|
$annotations = $this->reader->getMethodAnnotations($method); |
|
98
|
54 |
|
foreach ($annotations as $annotation) { |
|
99
|
22 |
|
if ($annotation instanceof ResourceParam) { |
|
100
|
3 |
|
$names[$annotation->param] = new AssistedResourceParam($annotation); |
|
101
|
|
|
} |
|
102
|
22 |
|
if ($annotation instanceof Assisted) { |
|
103
|
1 |
|
$names = $this->setAssistedAnnotation($names, $annotation); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
54 |
|
return $names; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
1 |
|
private function setAssistedAnnotation(array $names, Assisted $assisted) |
|
114
|
|
|
{ |
|
115
|
|
|
/* @var $annotation Assisted */ |
|
116
|
1 |
|
foreach ($assisted->values as $assistedParam) { |
|
117
|
1 |
|
$names[$assistedParam] = new AssistedParam; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
return $names; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|