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