|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Accessible\Reader; |
|
4
|
|
|
|
|
5
|
|
|
use \Accessible\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
class Reader |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The name of the annotation classes that define a collection behavior. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array<string> |
|
13
|
|
|
*/ |
|
14
|
|
|
protected static $collectionAnnotationClasses = array( |
|
15
|
|
|
"list" => "Accessible\\Annotation\\ListBehavior", |
|
16
|
|
|
"map" => "Accessible\\Annotation\\MapBehavior", |
|
17
|
|
|
"set" => "Accessible\\Annotation\\SetBehavior", |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get a list of classes and traits to analyze. |
|
22
|
|
|
* |
|
23
|
|
|
* @param \ReflectionObject $reflectionObject The object to get the parents from. |
|
24
|
|
|
* |
|
25
|
|
|
* @return array The list of classes to read. |
|
26
|
|
|
*/ |
|
27
|
6 |
|
public static function getClassesToRead(\ReflectionObject $reflectionObject) |
|
28
|
|
|
{ |
|
29
|
6 |
|
$cacheId = md5("classesToRead:" . $reflectionObject->getName()); |
|
30
|
6 |
|
$objectClasses = self::getFromCache($cacheId); |
|
31
|
6 |
|
if ($objectClasses !== null) { |
|
32
|
|
|
return $objectClasses; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
6 |
|
$objectClasses = array($reflectionObject); |
|
36
|
6 |
|
$objectTraits = $reflectionObject->getTraits(); |
|
37
|
6 |
|
if (!empty($objectTraits)) { |
|
38
|
2 |
|
foreach ($objectTraits as $trait) { |
|
39
|
2 |
|
$objectClasses[] = $trait; |
|
40
|
2 |
|
} |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
6 |
|
$parentClass = $reflectionObject->getParentClass(); |
|
44
|
6 |
|
while ($parentClass) { |
|
45
|
4 |
|
$objectClasses[] = $parentClass; |
|
46
|
|
|
|
|
47
|
4 |
|
$parentTraits = $parentClass->getTraits(); |
|
48
|
4 |
|
if (!empty($parentTraits)) { |
|
49
|
4 |
|
foreach ($parentTraits as $trait) { |
|
50
|
4 |
|
$objectClasses[] = $trait; |
|
51
|
4 |
|
} |
|
52
|
4 |
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
$parentClass = $parentClass->getParentClass(); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
6 |
|
self::saveToCache($cacheId, $objectClasses); |
|
58
|
|
|
|
|
59
|
6 |
|
return $objectClasses; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the properties from a list of classes. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $classes |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public static function getProperties($classes) |
|
70
|
|
|
{ |
|
71
|
6 |
|
array_reverse($classes); |
|
72
|
6 |
|
$properties = array(); |
|
73
|
6 |
|
foreach ($classes as $class) { |
|
74
|
6 |
|
foreach ($class->getProperties() as $property) { |
|
75
|
6 |
|
$properties[$property->getName()] = $property; |
|
76
|
6 |
|
} |
|
77
|
6 |
|
} |
|
78
|
|
|
|
|
79
|
6 |
|
return $properties; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the information on a class from its instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @param object $object |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
29 |
|
public static function getClassInformation($object) |
|
90
|
|
|
{ |
|
91
|
29 |
|
$reflectionObject = new \ReflectionObject($object); |
|
92
|
29 |
|
$cacheId = md5("classInformation:" . $reflectionObject->getName()); |
|
93
|
29 |
|
$classInfo = self::getFromCache($cacheId); |
|
94
|
29 |
|
if ($classInfo !== null) { |
|
95
|
24 |
|
return $classInfo; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
$objectClasses = self::getClassesToRead($reflectionObject); |
|
99
|
6 |
|
$objectProperties = self::getProperties($objectClasses); |
|
100
|
6 |
|
$annotationReader = Configuration::getAnnotationReader(); |
|
101
|
|
|
|
|
102
|
|
|
$classInfo = array( |
|
103
|
6 |
|
'accessProperties' => AccessReader::getAccessProperties($objectProperties, $annotationReader), |
|
104
|
6 |
|
'collectionsItemNames' => CollectionsReader::getCollectionsItemNames($objectProperties, $annotationReader), |
|
105
|
6 |
|
'associationsList' => AssociationReader::getAssociations($objectProperties, $annotationReader), |
|
106
|
6 |
|
'constraintsValidationEnabled' => ConstraintsReader::isConstraintsValidationEnabled($objectClasses, $annotationReader), |
|
107
|
6 |
|
'initialPropertiesValues' => AutoConstructReader::getPropertiesToInitialize($objectProperties, $annotationReader), |
|
108
|
6 |
|
'initializationNeededArguments' => AutoConstructReader::getConstructArguments($objectClasses, $annotationReader) |
|
109
|
6 |
|
); |
|
110
|
|
|
|
|
111
|
6 |
|
self::saveToCache($cacheId, $classInfo); |
|
112
|
|
|
|
|
113
|
6 |
|
return $classInfo; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get a value from the cache. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $id |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
29 |
|
public static function getFromCache($id) |
|
124
|
|
|
{ |
|
125
|
29 |
|
$arrayCache = Configuration::getArrayCache(); |
|
126
|
29 |
|
if ($arrayCache->contains($id)) { |
|
127
|
24 |
|
return $arrayCache->fetch($id); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
6 |
|
$cacheDriver = Configuration::getCacheDriver(); |
|
131
|
6 |
|
if ($cacheDriver !== null) { |
|
132
|
|
|
$cacheResult = $cacheDriver->fetch($id); |
|
133
|
|
|
if ($cacheResult !== false) { |
|
134
|
|
|
$arrayCache->save($id, $cacheResult); |
|
135
|
|
|
return $cacheResult; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
6 |
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Save a value to the cache. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $id |
|
146
|
|
|
* @param mixed $value |
|
147
|
|
|
*/ |
|
148
|
6 |
|
public static function saveToCache($id, $value) |
|
149
|
|
|
{ |
|
150
|
6 |
|
$arrayCache = Configuration::getArrayCache(); |
|
151
|
6 |
|
$cacheDriver = Configuration::getCacheDriver(); |
|
152
|
|
|
|
|
153
|
6 |
|
$arrayCache->save($id, $value); |
|
154
|
6 |
|
if ($cacheDriver !== null) { |
|
155
|
|
|
$cacheDriver->save($id, $value); |
|
156
|
|
|
} |
|
157
|
6 |
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|