1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Accessible; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
8
|
|
|
use Doctrine\Common\Cache\Cache; |
9
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
10
|
|
|
use Symfony\Component\Validator\Validation; |
11
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
12
|
|
|
|
13
|
|
|
class Configuration |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The annotations reader used to know the access. |
17
|
|
|
* |
18
|
|
|
* @var Doctrine\Common\Annotations\Reader |
19
|
|
|
*/ |
20
|
|
|
private static $annotationReader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The constraints validator. |
24
|
|
|
* |
25
|
|
|
* @var Symfony\Component\Validator\ConstraintValidator |
26
|
|
|
*/ |
27
|
|
|
private static $constraintsValidator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Says if the setter values have to be validated. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private static $constraintsValidationEnabled = true; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Says if the Initialize and InitializeObject values have to |
38
|
|
|
* be validated with constraints. |
39
|
|
|
* |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
private static $initializeValuesValidationEnabled = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The cache driver the library will use. |
46
|
|
|
* |
47
|
|
|
* @var Cache |
48
|
|
|
*/ |
49
|
|
|
private static $cacheDriver; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The namespace of the cache driver. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private static $cacheDefaultNamespace = 'antares_accessible_'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The in-memory cache that will be used. |
60
|
|
|
* @var ArrayCache |
61
|
|
|
*/ |
62
|
|
|
private static $arrayCache; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the annotation reader that is used. |
66
|
|
|
* Initializes it if it doesn't already exists. |
67
|
|
|
* |
68
|
|
|
* @return Reader The annotation reader. |
69
|
|
|
*/ |
70
|
9 |
|
public static function getAnnotationReader() |
71
|
|
|
{ |
72
|
9 |
|
if (self::$annotationReader === null) { |
73
|
1 |
|
self::$annotationReader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
9 |
|
return self::$annotationReader; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the annotation reader that will be used. |
81
|
|
|
* |
82
|
|
|
* @param Reader $annotationReader The annotation reader. |
83
|
|
|
*/ |
84
|
1 |
|
public static function setAnnotationReader(Reader $annotationReader) |
85
|
|
|
{ |
86
|
1 |
|
self::$annotationReader = $annotationReader; |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the constraints validator that is used. |
91
|
|
|
* Initializes it if it doesn't already exists. |
92
|
|
|
* |
93
|
|
|
* @return ConstraintValidator The annotation reader. |
94
|
|
|
*/ |
95
|
39 |
|
public static function getConstraintsValidator() |
96
|
|
|
{ |
97
|
39 |
|
if (self::$constraintsValidator === null) { |
98
|
1 |
|
self::$constraintsValidator = Validation::createValidatorBuilder() |
99
|
1 |
|
->enableAnnotationMapping(self::getAnnotationReader()) |
100
|
1 |
|
->getValidator(); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
39 |
|
return self::$constraintsValidator; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Set the constraints validator that will be used. |
108
|
|
|
* |
109
|
|
|
* @param ValidatorInterface $constraintsValidator The annotation reader. |
110
|
|
|
*/ |
111
|
1 |
|
public static function setConstraintsValidator(ValidatorInterface $constraintsValidator) |
112
|
|
|
{ |
113
|
1 |
|
self::$constraintsValidator = $constraintsValidator; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Indicates if the constraints validation is enabled for the setters. |
118
|
|
|
* |
119
|
|
|
* @return boolean True if enabled, else false. |
120
|
|
|
*/ |
121
|
8 |
|
public static function isConstraintsValidationEnabled() |
122
|
|
|
{ |
123
|
8 |
|
return self::$constraintsValidationEnabled; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Enable or disable the constraints validation for setters. |
128
|
|
|
* |
129
|
|
|
* @param bool $enabled True for enable, false for disable. |
130
|
|
|
*/ |
131
|
1 |
|
public static function setConstraintsValidationEnabled($enabled) |
132
|
|
|
{ |
133
|
1 |
|
if (!is_bool($enabled)) { |
134
|
|
|
throw new \InvalidArgumentException("This value must be a boolean."); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
self::$constraintsValidationEnabled = $enabled; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Indicates if the constraints validation for Initialize and |
142
|
|
|
* InitializeObject values is enabled or not. |
143
|
|
|
* |
144
|
|
|
* @return boolean True if enabled, else false. |
145
|
|
|
*/ |
146
|
39 |
|
public static function isInitializeValuesValidationEnabled() |
147
|
|
|
{ |
148
|
39 |
|
return self::$initializeValuesValidationEnabled; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Enable or disable the constraints validation for Initialize and |
153
|
|
|
* InitializeObject values. |
154
|
|
|
* |
155
|
|
|
* @param bool $enabled True for enable, false for disable. |
156
|
|
|
*/ |
157
|
1 |
|
public static function setInitializeValuesValidationEnabled($enabled) |
158
|
|
|
{ |
159
|
1 |
|
if (!is_bool($enabled)) { |
160
|
|
|
throw new \InvalidArgumentException("This value must be a boolean."); |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
self::$initializeValuesValidationEnabled = $enabled; |
164
|
1 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set a cache driver. |
168
|
|
|
* |
169
|
|
|
* @param Cache $cacheToChange The cache to change. |
170
|
|
|
* @param Cache $cache The cache driver. |
171
|
|
|
* @param string $namespace The cache namespace. |
172
|
|
|
*/ |
173
|
3 |
|
private static function setCache(Cache &$cacheToChange = null, Cache $cache = null, $namespace = null) |
174
|
|
|
{ |
175
|
3 |
|
if ($namespace === null) { |
176
|
3 |
|
$namespace = self::$cacheDefaultNamespace; |
177
|
3 |
|
} |
178
|
3 |
|
if (!is_string($namespace)) { |
179
|
|
|
throw new \InvalidArgumentException("The namespace must be a string."); |
180
|
|
|
} |
181
|
|
|
|
182
|
3 |
|
$cacheToChange = $cache; |
183
|
3 |
|
if ($cache !== null) { |
184
|
3 |
|
$cacheToChange->setNamespace($namespace); |
185
|
3 |
|
} |
186
|
3 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the cache driver that will be used. |
190
|
|
|
* |
191
|
|
|
* @return Cache The cache driver. |
192
|
|
|
*/ |
193
|
8 |
|
public static function getCacheDriver() |
194
|
|
|
{ |
195
|
8 |
|
return self::$cacheDriver; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the cache driver that will be used. |
200
|
|
|
* |
201
|
|
|
* @param Cache $cache The cache driver. |
202
|
|
|
* @param string $namespace The cache namespace. |
203
|
|
|
*/ |
204
|
1 |
|
public static function setCacheDriver(Cache $cache = null, $namespace = null) |
205
|
|
|
{ |
206
|
1 |
|
self::setCache(self::$cacheDriver, $cache, $namespace); |
207
|
1 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the array cache that will be used. |
211
|
|
|
* Initialize it if it doesn't already exist. |
212
|
|
|
* |
213
|
|
|
* @return Cache The cache driver. |
214
|
|
|
*/ |
215
|
39 |
|
public static function getArrayCache() |
216
|
|
|
{ |
217
|
39 |
|
if (self::$arrayCache === null) { |
218
|
1 |
|
self::setArrayCache(new ArrayCache()); |
219
|
1 |
|
} |
220
|
|
|
|
221
|
39 |
|
return self::$arrayCache; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the array cache that will be used. |
226
|
|
|
* |
227
|
|
|
* @param Cache $cache The cache driver. |
228
|
|
|
* @param string $namespace The cache namespace. |
229
|
|
|
*/ |
230
|
2 |
|
public static function setArrayCache(Cache $cache, $namespace = null) { |
231
|
2 |
|
self::setCache(self::$arrayCache, $cache, $namespace); |
232
|
2 |
|
} |
233
|
|
|
} |
234
|
|
|
|