|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Minime\Annotations\Types; |
|
4
|
|
|
|
|
5
|
|
|
use stdClass; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use Minime\Annotations\ParserException; |
|
8
|
|
|
|
|
9
|
|
|
class ConcreteType extends AbstractType |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var TypeInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private static $instance; |
|
15
|
|
|
|
|
16
|
|
|
public static function getType() |
|
17
|
|
|
{ |
|
18
|
|
|
if (!isset(self::$instance)) { |
|
19
|
|
|
self::$instance = new ConcreteType(); |
|
|
|
|
|
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
return self::$instance; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $namespaceLookup = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set of user defined namespaces to lookup for class autoloading. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $namespaces |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setNamespaces(array $namespaces) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->namespaceLookup = $namespaces; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $class |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
* |
|
45
|
|
|
* @throws ParserException |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function checkClassExistence($class) |
|
48
|
|
|
{ |
|
49
|
|
|
$found = class_exists($class); |
|
50
|
|
|
$classname = $class; |
|
51
|
|
|
$i = 0; |
|
52
|
|
|
|
|
53
|
|
|
while (!$found && $i < count($this->namespaceLookup)) { |
|
54
|
|
|
$classname = $this->namespaceLookup[$i] . $class; |
|
55
|
|
|
$found = class_exists($classname); |
|
56
|
|
|
$i++; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!$found) { |
|
60
|
|
|
throw new ParserException("Concrete annotation expects '{$class}' to exist."); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $classname; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Process a value to be a concrete annotation |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $value json string |
|
70
|
|
|
* @param string $class name of concrete annotation type (class) |
|
71
|
|
|
* |
|
72
|
|
|
* @throws ParserException |
|
73
|
|
|
* |
|
74
|
|
|
* @return object |
|
75
|
|
|
*/ |
|
76
|
|
|
public function parse($value, $class = null) |
|
77
|
|
|
{ |
|
78
|
|
|
$classname = $this->checkClassExistence($class); |
|
79
|
|
|
|
|
80
|
|
|
$prototype = (new JsonType)->parse($value); |
|
81
|
|
|
|
|
82
|
|
|
if ($prototype instanceof stdClass) { |
|
83
|
|
|
if (!$this->isPrototypeSchemaValid($prototype)) { |
|
84
|
|
|
throw new ParserException("Only arrays should be used to configure concrete annotation method calls."); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->makeInstance($classname, $prototype); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (is_array($prototype)) { |
|
91
|
|
|
return $this->makeConstructSugarInjectionInstance($classname, $prototype); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
throw new ParserException("Json value for annotation({$classname}) must be of type object or array."); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function makeConstructSugarInjectionInstance($class, array $prototype) { |
|
98
|
|
|
$reflection = new ReflectionClass($class); |
|
99
|
|
|
$instance = $reflection->newInstanceArgs($prototype); |
|
100
|
|
|
|
|
101
|
|
|
return $instance; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Creates and hydrates a concrete annotation class |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $class full qualified class name |
|
108
|
|
|
* @param stdClass $prototype object prototype |
|
109
|
|
|
* @return object hydrated concrete annotation class |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function makeInstance($class, stdClass $prototype) |
|
112
|
|
|
{ |
|
113
|
|
|
$reflection = new ReflectionClass($class); |
|
114
|
|
|
if (isset($prototype->__construct)) { |
|
115
|
|
|
$instance = $reflection->newInstanceArgs($prototype->__construct); |
|
116
|
|
|
unset($prototype->__construct); |
|
117
|
|
|
} else { |
|
118
|
|
|
$instance = $reflection->newInstance(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->doMethodConfiguration($instance, $prototype); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Do configuration injection through method calls |
|
126
|
|
|
* |
|
127
|
|
|
* @param object $instance concrete annotation instance |
|
128
|
|
|
* @param stdClass $prototype object prototype |
|
129
|
|
|
* @return object hydrated concrete annotation class |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function doMethodConfiguration($instance, stdClass $prototype) |
|
132
|
|
|
{ |
|
133
|
|
|
foreach ($prototype as $method => $args) { |
|
134
|
|
|
call_user_func_array([$instance, $method], $args); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $instance; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Validates a prototype object |
|
142
|
|
|
* |
|
143
|
|
|
* @param stdClass $prototype object prototype |
|
144
|
|
|
* @return boolean true if prototype is valid |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function isPrototypeSchemaValid(stdclass $prototype) |
|
147
|
|
|
{ |
|
148
|
|
|
foreach ($prototype as $args) { |
|
149
|
|
|
if (! is_array($args)) { |
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return true; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..