|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* A base class for all SilverStripe objects to inherit from. |
|
4
|
|
|
* |
|
5
|
|
|
* This class provides a number of pattern implementations, as well as methods and fixes to add extra psuedo-static |
|
6
|
|
|
* and method functionality to PHP. |
|
7
|
|
|
* |
|
8
|
|
|
* See {@link Extension} on how to implement a custom multiple |
|
9
|
|
|
* inheritance for object instances based on PHP5 method call overloading. |
|
10
|
|
|
* |
|
11
|
|
|
* @todo Create instance-specific removeExtension() which removes an extension from $extension_instances, |
|
12
|
|
|
* but not from static $extensions, and clears everything added through defineMethods(), mainly $extra_methods. |
|
13
|
|
|
* |
|
14
|
|
|
* @package framework |
|
15
|
|
|
* @subpackage core |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class SS_Object { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* An array of extension names and parameters to be applied to this object upon construction. |
|
21
|
|
|
* |
|
22
|
|
|
* Example: |
|
23
|
|
|
* <code> |
|
24
|
|
|
* private static $extensions = array ( |
|
25
|
|
|
* 'Hierarchy', |
|
26
|
|
|
* "Version('Stage', 'Live')" |
|
27
|
|
|
* ); |
|
28
|
|
|
* </code> |
|
29
|
|
|
* |
|
30
|
|
|
* Use {@link Object::add_extension()} to add extensions without access to the class code, |
|
31
|
|
|
* e.g. to extend core classes. |
|
32
|
|
|
* |
|
33
|
|
|
* Extensions are instanciated together with the object and stored in {@link $extension_instances}. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array $extensions |
|
36
|
|
|
* @config |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $extensions = null; |
|
39
|
|
|
|
|
40
|
|
|
private static |
|
41
|
|
|
$classes_constructed = array(), |
|
|
|
|
|
|
42
|
|
|
$extra_methods = array(), |
|
43
|
|
|
$built_in_methods = array(); |
|
44
|
|
|
|
|
45
|
|
|
private static |
|
46
|
|
|
$custom_classes = array(), |
|
|
|
|
|
|
47
|
|
|
$strong_classes = array(); |
|
48
|
|
|
|
|
49
|
|
|
/**#@-*/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string the class name |
|
53
|
|
|
*/ |
|
54
|
|
|
public $class; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....). |
|
58
|
|
|
* @return Config_ForClass|null |
|
59
|
|
|
*/ |
|
60
|
|
|
static public function config() { |
|
|
|
|
|
|
61
|
|
|
return Config::inst()->forClass(get_called_class()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var array all current extension instances. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $extension_instances = array(); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* List of callbacks to call prior to extensions having extend called on them, |
|
71
|
|
|
* each grouped by methodName. |
|
72
|
|
|
* |
|
73
|
|
|
* @var array[callable] |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $beforeExtendCallbacks = array(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Allows user code to hook into Object::extend prior to control |
|
79
|
|
|
* being delegated to extensions. Each callback will be reset |
|
80
|
|
|
* once called. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $method The name of the method to hook into |
|
83
|
|
|
* @param callable $callback The callback to execute |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function beforeExtending($method, $callback) { |
|
86
|
|
|
if(empty($this->beforeExtendCallbacks[$method])) { |
|
87
|
|
|
$this->beforeExtendCallbacks[$method] = array(); |
|
88
|
|
|
} |
|
89
|
|
|
$this->beforeExtendCallbacks[$method][] = $callback; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* List of callbacks to call after extensions having extend called on them, |
|
94
|
|
|
* each grouped by methodName. |
|
95
|
|
|
* |
|
96
|
|
|
* @var array[callable] |
|
97
|
|
|
*/ |
|
98
|
|
|
protected $afterExtendCallbacks = array(); |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Allows user code to hook into Object::extend after control |
|
102
|
|
|
* being delegated to extensions. Each callback will be reset |
|
103
|
|
|
* once called. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $method The name of the method to hook into |
|
106
|
|
|
* @param callable $callback The callback to execute |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function afterExtending($method, $callback) { |
|
109
|
|
|
if(empty($this->afterExtendCallbacks[$method])) { |
|
110
|
|
|
$this->afterExtendCallbacks[$method] = array(); |
|
111
|
|
|
} |
|
112
|
|
|
$this->afterExtendCallbacks[$method][] = $callback; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* An implementation of the factory method, allows you to create an instance of a class |
|
117
|
|
|
* |
|
118
|
|
|
* This method first for strong class overloads (singletons & DB interaction), then custom class overloads. If an |
|
119
|
|
|
* overload is found, an instance of this is returned rather than the original class. To overload a class, use |
|
120
|
|
|
* {@link Object::useCustomClass()} |
|
121
|
|
|
* |
|
122
|
|
|
* This can be called in one of two ways - either calling via the class directly, |
|
123
|
|
|
* or calling on Object and passing the class name as the first parameter. The following |
|
124
|
|
|
* are equivalent: |
|
125
|
|
|
* $list = DataList::create('SiteTree'); |
|
126
|
|
|
* $list = SiteTree::get(); |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $class the class name |
|
|
|
|
|
|
129
|
|
|
* @param mixed $arguments,... arguments to pass to the constructor |
|
|
|
|
|
|
130
|
|
|
* @return static |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function create() { |
|
133
|
|
|
$args = func_get_args(); |
|
134
|
|
|
|
|
135
|
|
|
// Class to create should be the calling class if not Object, |
|
136
|
|
|
// otherwise the first parameter |
|
137
|
|
|
$class = get_called_class(); |
|
138
|
|
|
if($class == 'SS_Object' || $class == 'Object') $class = array_shift($args); |
|
139
|
|
|
|
|
140
|
|
|
$class = self::getCustomClass($class); |
|
141
|
|
|
|
|
142
|
|
|
return Injector::inst()->createWithArgs($class, $args); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Creates a class instance by the "singleton" design pattern. |
|
147
|
|
|
* It will always return the same instance for this class, |
|
148
|
|
|
* which can be used for performance reasons and as a simple |
|
149
|
|
|
* way to access instance methods which don't rely on instance |
|
150
|
|
|
* data (e.g. the custom SilverStripe static handling). |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $className Optional classname (if called on Object directly) |
|
|
|
|
|
|
153
|
|
|
* @return static The singleton instance |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function singleton() { |
|
156
|
|
|
$args = func_get_args(); |
|
157
|
|
|
|
|
158
|
|
|
// Singleton to create should be the calling class if not Object, |
|
159
|
|
|
// otherwise the first parameter |
|
160
|
|
|
$class = get_called_class(); |
|
161
|
|
|
if($class === 'SS_Object') $class = array_shift($args); |
|
162
|
|
|
|
|
163
|
|
|
return Injector::inst()->get($class); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private static $_cache_inst_args = array(); |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Create an object from a string representation. It treats it as a PHP constructor without the |
|
170
|
|
|
* 'new' keyword. It also manages to construct the object without the use of eval(). |
|
171
|
|
|
* |
|
172
|
|
|
* Construction itself is done with Object::create(), so that Object::useCustomClass() calls |
|
173
|
|
|
* are respected. |
|
174
|
|
|
* |
|
175
|
|
|
* `Object::create_from_string("Versioned('Stage','Live')")` will return the result of |
|
176
|
|
|
* `Versioned::create('Stage', 'Live);` |
|
177
|
|
|
* |
|
178
|
|
|
* It is designed for simple, clonable objects. The first time this method is called for a given |
|
179
|
|
|
* string it is cached, and clones of that object are returned. |
|
180
|
|
|
* |
|
181
|
|
|
* If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's |
|
182
|
|
|
* impossible to pass null as the firstArg argument. |
|
183
|
|
|
* |
|
184
|
|
|
* `Object::create_from_string("Varchar(50)", "MyField")` will return the result of |
|
185
|
|
|
* `Vachar::create('MyField', '50');` |
|
186
|
|
|
* |
|
187
|
|
|
* Arguments are always strings, although this is a quirk of the current implementation rather |
|
188
|
|
|
* than something that can be relied upon. |
|
189
|
|
|
*/ |
|
190
|
|
|
public static function create_from_string($classSpec, $firstArg = null) { |
|
191
|
|
|
if(!isset(self::$_cache_inst_args[$classSpec.$firstArg])) { |
|
192
|
|
|
// an $extension value can contain parameters as a string, |
|
193
|
|
|
// e.g. "Versioned('Stage','Live')" |
|
194
|
|
|
if(strpos($classSpec,'(') === false) { |
|
195
|
|
|
if($firstArg === null) self::$_cache_inst_args[$classSpec.$firstArg] = SS_Object::create($classSpec); |
|
|
|
|
|
|
196
|
|
|
else self::$_cache_inst_args[$classSpec.$firstArg] = SS_Object::create($classSpec, $firstArg); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
} else { |
|
199
|
|
|
list($class, $args) = self::parse_class_spec($classSpec); |
|
200
|
|
|
|
|
201
|
|
|
if($firstArg !== null) array_unshift($args, $firstArg); |
|
202
|
|
|
array_unshift($args, $class); |
|
203
|
|
|
|
|
204
|
|
|
self::$_cache_inst_args[$classSpec.$firstArg] = call_user_func_array(array('SS_Object','create'), $args); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return clone self::$_cache_inst_args[$classSpec.$firstArg]; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string(). |
|
213
|
|
|
* Returns a 2-elemnent array, with classname and arguments |
|
214
|
|
|
*/ |
|
215
|
|
|
public static function parse_class_spec($classSpec) { |
|
216
|
|
|
$tokens = token_get_all("<?php $classSpec"); |
|
217
|
|
|
$class = null; |
|
218
|
|
|
$args = array(); |
|
219
|
|
|
|
|
220
|
|
|
// Keep track of the current bucket that we're putting data into |
|
221
|
|
|
$bucket = &$args; |
|
222
|
|
|
$bucketStack = array(); |
|
223
|
|
|
$hadNamespace = false; |
|
224
|
|
|
$currentKey = null; |
|
225
|
|
|
|
|
226
|
|
|
foreach($tokens as $token) { |
|
227
|
|
|
// $forceResult used to allow null result to be detected |
|
228
|
|
|
$result = $forceResult = null; |
|
229
|
|
|
$tokenName = is_array($token) ? $token[0] : $token; |
|
230
|
|
|
|
|
231
|
|
|
// Get the class name |
|
232
|
|
|
if($class === null && is_array($token) && $token[0] === T_STRING) { |
|
233
|
|
|
$class = $token[1]; |
|
234
|
|
|
} elseif(is_array($token) && $token[0] === T_NS_SEPARATOR) { |
|
235
|
|
|
$class .= $token[1]; |
|
236
|
|
|
$hadNamespace = true; |
|
237
|
|
|
} elseif($hadNamespace && is_array($token) && $token[0] === T_STRING) { |
|
238
|
|
|
$class .= $token[1]; |
|
239
|
|
|
$hadNamespace = false; |
|
240
|
|
|
// Get arguments |
|
241
|
|
|
} else if(is_array($token)) { |
|
242
|
|
|
switch($token[0]) { |
|
243
|
|
|
case T_CONSTANT_ENCAPSED_STRING: |
|
244
|
|
|
$argString = $token[1]; |
|
245
|
|
|
switch($argString[0]) { |
|
246
|
|
|
case '"': |
|
247
|
|
|
$result = stripcslashes(substr($argString,1,-1)); |
|
248
|
|
|
break; |
|
249
|
|
|
case "'": |
|
250
|
|
|
$result = str_replace(array("\\\\", "\\'"),array("\\", "'"), substr($argString,1,-1)); |
|
251
|
|
|
break; |
|
252
|
|
|
default: |
|
253
|
|
|
throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString"); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
break; |
|
257
|
|
|
|
|
258
|
|
|
case T_DNUMBER: |
|
259
|
|
|
$result = (double)$token[1]; |
|
260
|
|
|
break; |
|
261
|
|
|
|
|
262
|
|
|
case T_LNUMBER: |
|
263
|
|
|
$result = (int)$token[1]; |
|
264
|
|
|
break; |
|
265
|
|
|
|
|
266
|
|
|
case T_DOUBLE_ARROW: |
|
267
|
|
|
// We've encountered an associative array (the array itself has already been |
|
268
|
|
|
// added to the bucket), so the previous item added to the bucket is the key |
|
269
|
|
|
end($bucket); |
|
270
|
|
|
$currentKey = current($bucket); |
|
271
|
|
|
array_pop($bucket); |
|
272
|
|
|
break; |
|
273
|
|
|
|
|
274
|
|
|
case T_STRING: |
|
275
|
|
|
switch($token[1]) { |
|
276
|
|
|
case 'true': $result = true; break; |
|
|
|
|
|
|
277
|
|
|
case 'false': $result = false; break; |
|
|
|
|
|
|
278
|
|
|
case 'null': $result = null; $forceResult = true; break; |
|
|
|
|
|
|
279
|
|
|
default: throw new Exception("Bad T_STRING arg '{$token[1]}'"); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
break; |
|
283
|
|
|
|
|
284
|
|
|
case T_ARRAY: |
|
285
|
|
|
$result = array(); |
|
286
|
|
|
break; |
|
287
|
|
|
} |
|
288
|
|
|
} else { |
|
289
|
|
|
if($tokenName === '[') { |
|
290
|
|
|
$result = array(); |
|
291
|
|
|
} elseif(($tokenName === ')' || $tokenName === ']') && ! empty($bucketStack)) { |
|
292
|
|
|
// Store the bucket we're currently working on |
|
293
|
|
|
$oldBucket = $bucket; |
|
294
|
|
|
// Fetch the key for the bucket at the top of the stack |
|
295
|
|
|
end($bucketStack); |
|
296
|
|
|
$key = key($bucketStack); |
|
297
|
|
|
reset($bucketStack); |
|
298
|
|
|
// Re-instate the bucket from the top of the stack |
|
299
|
|
|
$bucket = &$bucketStack[$key]; |
|
300
|
|
|
// Add our saved, "nested" bucket to the bucket we just popped off the stack |
|
301
|
|
|
$bucket[$key] = $oldBucket; |
|
302
|
|
|
// Remove the bucket we just popped off the stack |
|
303
|
|
|
array_pop($bucketStack); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
// If we've got something to add to the bucket, add it |
|
308
|
|
|
if($result !== null || $forceResult) { |
|
309
|
|
|
if($currentKey) { |
|
310
|
|
|
$bucket[$currentKey] = $result; |
|
311
|
|
|
$currentKey = null; |
|
312
|
|
|
} else { |
|
313
|
|
|
$bucket[] = $result; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
// If we've just pushed an array, that becomes our new bucket |
|
317
|
|
|
if($result === array()) { |
|
318
|
|
|
// Fetch the key that the array was pushed to |
|
319
|
|
|
end($bucket); |
|
320
|
|
|
$key = key($bucket); |
|
321
|
|
|
reset($bucket); |
|
322
|
|
|
// Store reference to "old" bucket in the stack |
|
323
|
|
|
$bucketStack[$key] = &$bucket; |
|
324
|
|
|
// Set the active bucket to be our newly-pushed, empty array |
|
325
|
|
|
$bucket = &$bucket[$key]; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
return array($class, $args); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to |
|
335
|
|
|
* TRUE when using {@link Object::useCustomClass()} |
|
336
|
|
|
* |
|
337
|
|
|
* @param string $class the class name |
|
|
|
|
|
|
338
|
|
|
* @param mixed $arguments,... arguments to pass to the constructor |
|
|
|
|
|
|
339
|
|
|
* @return static |
|
340
|
|
|
*/ |
|
341
|
|
|
public static function strong_create() { |
|
342
|
|
|
$args = func_get_args(); |
|
343
|
|
|
$class = array_shift($args); |
|
344
|
|
|
|
|
345
|
|
|
if(isset(self::$strong_classes[$class]) && ClassInfo::exists(self::$strong_classes[$class])) { |
|
346
|
|
|
$class = self::$strong_classes[$class]; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
return Injector::inst()->createWithArgs($class, $args); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* This class allows you to overload classes with other classes when they are constructed using the factory method |
|
354
|
|
|
* {@link Object::create()} |
|
355
|
|
|
* |
|
356
|
|
|
* @param string $oldClass the class to replace |
|
357
|
|
|
* @param string $newClass the class to replace it with |
|
358
|
|
|
* @param bool $strong allows you to enforce a certain class replacement under all circumstances. This is used in |
|
359
|
|
|
* singletons and DB interaction classes |
|
360
|
|
|
*/ |
|
361
|
|
|
public static function useCustomClass($oldClass, $newClass, $strong = false) { |
|
362
|
|
|
if($strong) { |
|
363
|
|
|
self::$strong_classes[$oldClass] = $newClass; |
|
364
|
|
|
} else { |
|
365
|
|
|
self::$custom_classes[$oldClass] = $newClass; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
/** |
|
370
|
|
|
* If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name |
|
371
|
|
|
* |
|
372
|
|
|
* @param string $class the class to check |
|
373
|
|
|
* @return string the class that would be created if you called {@link Object::create()} with the class |
|
374
|
|
|
*/ |
|
375
|
|
|
public static function getCustomClass($class) { |
|
376
|
|
|
if(isset(self::$strong_classes[$class]) && ClassInfo::exists(self::$strong_classes[$class])) { |
|
377
|
|
|
return self::$strong_classes[$class]; |
|
378
|
|
|
} elseif(isset(self::$custom_classes[$class]) && ClassInfo::exists(self::$custom_classes[$class])) { |
|
379
|
|
|
return self::$custom_classes[$class]; |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
return $class; |
|
383
|
|
|
} |
|
384
|
|
|
|
|
385
|
|
|
/** |
|
386
|
|
|
* Get the value of a static property of a class, even in that property is declared protected (but not private), |
|
387
|
|
|
* without any inheritance, merging or parent lookup if it doesn't exist on the given class. |
|
388
|
|
|
* |
|
389
|
|
|
* @static |
|
390
|
|
|
* @param $class - The class to get the static from |
|
391
|
|
|
* @param $name - The property to get from the class |
|
392
|
|
|
* @param null $default - The value to return if property doesn't exist on class |
|
393
|
|
|
* @return any - The value of the static property $name on class $class, or $default if that property is not |
|
394
|
|
|
* defined |
|
395
|
|
|
*/ |
|
396
|
|
|
public static function static_lookup($class, $name, $default = null) { |
|
397
|
|
|
if (is_subclass_of($class, 'SS_Object')) { |
|
398
|
|
|
if (isset($class::$$name)) { |
|
399
|
|
|
$parent = get_parent_class($class); |
|
400
|
|
|
if (!$parent || !isset($parent::$$name) || $parent::$$name !== $class::$$name) return $class::$$name; |
|
401
|
|
|
} |
|
402
|
|
|
return $default; |
|
403
|
|
|
} else { |
|
404
|
|
|
// TODO: This gets set once, then not updated, so any changes to statics after this is called the first |
|
405
|
|
|
// time for any class won't be exposed |
|
406
|
|
|
static $static_properties = array(); |
|
407
|
|
|
|
|
408
|
|
|
if (!isset($static_properties[$class])) { |
|
409
|
|
|
$reflection = new ReflectionClass($class); |
|
410
|
|
|
$static_properties[$class] = $reflection->getStaticProperties(); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
if (isset($static_properties[$class][$name])) { |
|
414
|
|
|
$value = $static_properties[$class][$name]; |
|
415
|
|
|
|
|
416
|
|
|
$parent = get_parent_class($class); |
|
417
|
|
|
if (!$parent) return $value; |
|
418
|
|
|
|
|
419
|
|
|
if (!isset($static_properties[$parent])) { |
|
420
|
|
|
$reflection = new ReflectionClass($parent); |
|
421
|
|
|
$static_properties[$parent] = $reflection->getStaticProperties(); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
if (!isset($static_properties[$parent][$name]) || $static_properties[$parent][$name] !== $value) { |
|
425
|
|
|
return $value; |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
return $default; |
|
431
|
|
|
} |
|
432
|
|
|
|
|
433
|
|
|
/** |
|
434
|
|
|
* @deprecated |
|
435
|
|
|
*/ |
|
436
|
|
|
public static function get_static($class, $name, $uncached = false) { |
|
|
|
|
|
|
437
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get'); |
|
438
|
|
|
return Config::inst()->get($class, $name, Config::FIRST_SET); |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
/** |
|
442
|
|
|
* @deprecated |
|
443
|
|
|
*/ |
|
444
|
|
|
public static function set_static($class, $name, $value) { |
|
445
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#update'); |
|
446
|
|
|
Config::inst()->update($class, $name, $value); |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* @deprecated |
|
451
|
|
|
*/ |
|
452
|
|
|
public static function uninherited_static($class, $name, $uncached = false) { |
|
|
|
|
|
|
453
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get'); |
|
454
|
|
|
return Config::inst()->get($class, $name, Config::UNINHERITED); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
/** |
|
458
|
|
|
* @deprecated |
|
459
|
|
|
*/ |
|
460
|
|
|
public static function combined_static($class, $name, $ceiling = false) { |
|
461
|
|
|
if ($ceiling) throw new Exception('Ceiling argument to combined_static is no longer supported'); |
|
462
|
|
|
|
|
463
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get'); |
|
464
|
|
|
return Config::inst()->get($class, $name); |
|
465
|
|
|
} |
|
466
|
|
|
|
|
467
|
|
|
/** |
|
468
|
|
|
* @deprecated |
|
469
|
|
|
*/ |
|
470
|
|
|
public static function addStaticVars($class, $properties, $replace = false) { |
|
471
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#update'); |
|
472
|
|
|
foreach($properties as $prop => $value) self::add_static_var($class, $prop, $value, $replace); |
|
|
|
|
|
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
/** |
|
476
|
|
|
* @deprecated |
|
477
|
|
|
*/ |
|
478
|
|
|
public static function add_static_var($class, $name, $value, $replace = false) { |
|
479
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#remove and Config#update'); |
|
480
|
|
|
|
|
481
|
|
|
if ($replace) Config::inst()->remove($class, $name); |
|
482
|
|
|
Config::inst()->update($class, $name, $value); |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Return TRUE if a class has a specified extension. |
|
487
|
|
|
* This supports backwards-compatible format (static Object::has_extension($requiredExtension)) |
|
488
|
|
|
* and new format ($object->has_extension($class, $requiredExtension)) |
|
489
|
|
|
* @param string $classOrExtension if 1 argument supplied, the class name of the extension to |
|
490
|
|
|
* check for; if 2 supplied, the class name to test |
|
491
|
|
|
* @param string $requiredExtension used only if 2 arguments supplied |
|
492
|
|
|
* @param boolean $strict if the extension has to match the required extension and not be a subclass |
|
493
|
|
|
*/ |
|
494
|
|
|
public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false) { |
|
495
|
|
|
//BC support |
|
496
|
|
|
if(func_num_args() > 1){ |
|
497
|
|
|
$class = $classOrExtension; |
|
498
|
|
|
$requiredExtension = $requiredExtension; |
|
|
|
|
|
|
499
|
|
|
} |
|
500
|
|
|
else { |
|
501
|
|
|
$class = get_called_class(); |
|
502
|
|
|
$requiredExtension = $classOrExtension; |
|
503
|
|
|
} |
|
504
|
|
|
|
|
505
|
|
|
$requiredExtension = strtolower($requiredExtension); |
|
506
|
|
|
$extensions = Config::inst()->get($class, 'extensions'); |
|
507
|
|
|
|
|
508
|
|
|
if($extensions) foreach($extensions as $extension) { |
|
|
|
|
|
|
509
|
|
|
$left = strtolower(Extension::get_classname_without_arguments($extension)); |
|
510
|
|
|
$right = strtolower(Extension::get_classname_without_arguments($requiredExtension)); |
|
511
|
|
|
if($left == $right) return true; |
|
512
|
|
|
if (!$strict && is_subclass_of($left, $right)) return true; |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
return false; |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
/** |
|
519
|
|
|
* Add an extension to a specific class. |
|
520
|
|
|
* |
|
521
|
|
|
* The preferred method for adding extensions is through YAML config, |
|
522
|
|
|
* since it avoids autoloading the class, and is easier to override in |
|
523
|
|
|
* more specific configurations. |
|
524
|
|
|
* |
|
525
|
|
|
* As an alternative, extensions can be added to a specific class |
|
526
|
|
|
* directly in the {@link Object::$extensions} array. |
|
527
|
|
|
* See {@link SiteTree::$extensions} for examples. |
|
528
|
|
|
* Keep in mind that the extension will only be applied to new |
|
529
|
|
|
* instances, not existing ones (including all instances created through {@link singleton()}). |
|
530
|
|
|
* |
|
531
|
|
|
* @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension |
|
532
|
|
|
* @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object} |
|
533
|
|
|
* @param string $extension Subclass of {@link Extension} with optional parameters |
|
534
|
|
|
* as a string, e.g. "Versioned" or "Translatable('Param')" |
|
535
|
|
|
*/ |
|
536
|
|
|
public static function add_extension($classOrExtension, $extension = null) { |
|
537
|
|
|
if(func_num_args() > 1) { |
|
538
|
|
|
$class = $classOrExtension; |
|
539
|
|
|
} else { |
|
540
|
|
|
$class = get_called_class(); |
|
541
|
|
|
$extension = $classOrExtension; |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
if(!preg_match('/^([^(]*)/', $extension, $matches)) { |
|
545
|
|
|
return false; |
|
546
|
|
|
} |
|
547
|
|
|
$extensionClass = $matches[1]; |
|
548
|
|
|
if(!class_exists($extensionClass)) { |
|
549
|
|
|
user_error( |
|
550
|
|
|
sprintf('Object::add_extension() - Can\'t find extension class for "%s"', $extensionClass), |
|
551
|
|
|
E_USER_ERROR |
|
552
|
|
|
); |
|
553
|
|
|
} |
|
554
|
|
|
|
|
555
|
|
|
if(!is_subclass_of($extensionClass, 'Extension')) { |
|
556
|
|
|
user_error( |
|
557
|
|
|
sprintf('Object::add_extension() - Extension "%s" is not a subclass of Extension', $extensionClass), |
|
558
|
|
|
E_USER_ERROR |
|
559
|
|
|
); |
|
560
|
|
|
} |
|
561
|
|
|
|
|
562
|
|
|
// unset some caches |
|
563
|
|
|
$subclasses = ClassInfo::subclassesFor($class); |
|
564
|
|
|
$subclasses[] = $class; |
|
565
|
|
|
|
|
566
|
|
|
if($subclasses) foreach($subclasses as $subclass) { |
|
|
|
|
|
|
567
|
|
|
unset(self::$classes_constructed[$subclass]); |
|
568
|
|
|
unset(self::$extra_methods[$subclass]); |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
Config::inst()->update($class, 'extensions', array($extension)); |
|
572
|
|
|
Config::inst()->extraConfigSourcesChanged($class); |
|
573
|
|
|
|
|
574
|
|
|
Injector::inst()->unregisterNamedObject($class); |
|
575
|
|
|
|
|
576
|
|
|
// load statics now for DataObject classes |
|
577
|
|
|
if(is_subclass_of($class, 'DataObject')) { |
|
578
|
|
|
if(!is_subclass_of($extensionClass, 'DataExtension')) { |
|
579
|
|
|
user_error("$extensionClass cannot be applied to $class without being a DataExtension", E_USER_ERROR); |
|
580
|
|
|
} |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
|
|
585
|
|
|
/** |
|
586
|
|
|
* Remove an extension from a class. |
|
587
|
|
|
* |
|
588
|
|
|
* Keep in mind that this won't revert any datamodel additions |
|
589
|
|
|
* of the extension at runtime, unless its used before the |
|
590
|
|
|
* schema building kicks in (in your _config.php). |
|
591
|
|
|
* Doesn't remove the extension from any {@link Object} |
|
592
|
|
|
* instances which are already created, but will have an |
|
593
|
|
|
* effect on new extensions. |
|
594
|
|
|
* Clears any previously created singletons through {@link singleton()} |
|
595
|
|
|
* to avoid side-effects from stale extension information. |
|
596
|
|
|
* |
|
597
|
|
|
* @todo Add support for removing extensions with parameters |
|
598
|
|
|
* |
|
599
|
|
|
* @param string $extension Classname of an {@link Extension} subclass, without parameters |
|
600
|
|
|
*/ |
|
601
|
|
|
public static function remove_extension($extension) { |
|
602
|
|
|
$class = get_called_class(); |
|
603
|
|
|
|
|
604
|
|
|
Config::inst()->remove($class, 'extensions', Config::anything(), $extension); |
|
605
|
|
|
|
|
606
|
|
|
// remove any instances of the extension with parameters |
|
607
|
|
|
$config = Config::inst()->get($class, 'extensions'); |
|
608
|
|
|
|
|
609
|
|
|
if($config) { |
|
610
|
|
|
foreach($config as $k => $v) { |
|
|
|
|
|
|
611
|
|
|
// extensions with parameters will be stored in config as |
|
612
|
|
|
// ExtensionName("Param"). |
|
|
|
|
|
|
613
|
|
|
if(preg_match(sprintf("/^(%s)\(/", preg_quote($extension, '/')), $v)) { |
|
614
|
|
|
Config::inst()->remove($class, 'extensions', Config::anything(), $v); |
|
615
|
|
|
} |
|
616
|
|
|
} |
|
617
|
|
|
} |
|
618
|
|
|
|
|
619
|
|
|
Config::inst()->extraConfigSourcesChanged($class); |
|
620
|
|
|
|
|
621
|
|
|
// unset singletons to avoid side-effects |
|
622
|
|
|
Injector::inst()->unregisterAllObjects(); |
|
623
|
|
|
|
|
624
|
|
|
// unset some caches |
|
625
|
|
|
$subclasses = ClassInfo::subclassesFor($class); |
|
626
|
|
|
$subclasses[] = $class; |
|
627
|
|
|
if($subclasses) foreach($subclasses as $subclass) { |
|
|
|
|
|
|
628
|
|
|
unset(self::$classes_constructed[$subclass]); |
|
629
|
|
|
unset(self::$extra_methods[$subclass]); |
|
630
|
|
|
} |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
/** |
|
634
|
|
|
* @param string $class |
|
635
|
|
|
* @param bool $includeArgumentString Include the argument string in the return array, |
|
636
|
|
|
* FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')"). |
|
637
|
|
|
* @return array Numeric array of either {@link DataExtension} classnames, |
|
638
|
|
|
* or eval'ed classname strings with constructor arguments. |
|
639
|
|
|
*/ |
|
640
|
|
|
public static function get_extensions($class, $includeArgumentString = false) { |
|
641
|
|
|
$extensions = Config::inst()->get($class, 'extensions'); |
|
642
|
|
|
|
|
643
|
|
|
if($includeArgumentString) { |
|
644
|
|
|
return $extensions; |
|
645
|
|
|
} else { |
|
646
|
|
|
$extensionClassnames = array(); |
|
647
|
|
|
if($extensions) foreach($extensions as $extension) { |
|
|
|
|
|
|
648
|
|
|
$extensionClassnames[] = Extension::get_classname_without_arguments($extension); |
|
649
|
|
|
} |
|
650
|
|
|
return $extensionClassnames; |
|
651
|
|
|
} |
|
652
|
|
|
} |
|
653
|
|
|
|
|
654
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
655
|
|
|
|
|
656
|
|
|
private static $unextendable_classes = array('SS_Object', 'ViewableData', 'RequestHandler'); |
|
657
|
|
|
|
|
658
|
|
|
static public function get_extra_config_sources($class = null) { |
|
|
|
|
|
|
659
|
|
|
if($class === null) $class = get_called_class(); |
|
660
|
|
|
|
|
661
|
|
|
// If this class is unextendable, NOP |
|
662
|
|
|
if(in_array($class, self::$unextendable_classes)) return; |
|
663
|
|
|
|
|
664
|
|
|
// Variable to hold sources in |
|
665
|
|
|
$sources = null; |
|
666
|
|
|
|
|
667
|
|
|
// Get a list of extensions |
|
668
|
|
|
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES); |
|
669
|
|
|
|
|
670
|
|
|
if($extensions) { |
|
671
|
|
|
// Build a list of all sources; |
|
672
|
|
|
$sources = array(); |
|
673
|
|
|
|
|
674
|
|
|
foreach($extensions as $extension) { |
|
|
|
|
|
|
675
|
|
|
list($extensionClass, $extensionArgs) = self::parse_class_spec($extension); |
|
676
|
|
|
$sources[] = $extensionClass; |
|
677
|
|
|
|
|
678
|
|
|
if(!ClassInfo::has_method_from($extensionClass, 'add_to_class', 'Extension')) { |
|
679
|
|
|
Deprecation::notice('4.0', |
|
680
|
|
|
"add_to_class deprecated on $extensionClass. Use get_extra_config instead"); |
|
681
|
|
|
} |
|
682
|
|
|
|
|
683
|
|
|
call_user_func(array($extensionClass, 'add_to_class'), $class, $extensionClass, $extensionArgs); |
|
684
|
|
|
|
|
685
|
|
|
foreach(array_reverse(ClassInfo::ancestry($extensionClass)) as $extensionClassParent) { |
|
686
|
|
|
if (ClassInfo::has_method_from($extensionClassParent, 'get_extra_config', $extensionClassParent)) { |
|
687
|
|
|
$extras = $extensionClassParent::get_extra_config($class, $extensionClass, $extensionArgs); |
|
688
|
|
|
if ($extras) $sources[] = $extras; |
|
689
|
|
|
} |
|
690
|
|
|
} |
|
691
|
|
|
} |
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
return $sources; |
|
695
|
|
|
} |
|
696
|
|
|
|
|
697
|
|
|
public function __construct() { |
|
698
|
|
|
$this->class = get_class($this); |
|
699
|
|
|
|
|
700
|
|
|
foreach(ClassInfo::ancestry(get_called_class()) as $class) { |
|
701
|
|
|
if(in_array($class, self::$unextendable_classes)) continue; |
|
702
|
|
|
$extensions = Config::inst()->get($class, 'extensions', |
|
703
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES); |
|
704
|
|
|
|
|
705
|
|
|
if($extensions) foreach($extensions as $extension) { |
|
|
|
|
|
|
706
|
|
|
$instance = self::create_from_string($extension); |
|
707
|
|
|
$instance->setOwner(null, $class); |
|
708
|
|
|
$this->extension_instances[$instance->class] = $instance; |
|
709
|
|
|
} |
|
710
|
|
|
} |
|
711
|
|
|
|
|
712
|
|
|
if(!isset(self::$classes_constructed[$this->class])) { |
|
713
|
|
|
$this->defineMethods(); |
|
714
|
|
|
self::$classes_constructed[$this->class] = true; |
|
715
|
|
|
} |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
/** |
|
719
|
|
|
* Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located |
|
720
|
|
|
* |
|
721
|
|
|
* You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or |
|
722
|
|
|
* {@link Object::addWrapperMethod()} |
|
723
|
|
|
* |
|
724
|
|
|
* @param string $method |
|
725
|
|
|
* @param array $arguments |
|
726
|
|
|
* @return mixed |
|
727
|
|
|
*/ |
|
728
|
|
|
public function __call($method, $arguments) { |
|
729
|
|
|
$class = get_class($this); |
|
730
|
|
|
// If the method cache was cleared by an an Object::add_extension() / Object::remove_extension() |
|
731
|
|
|
// call, then we should rebuild it. |
|
732
|
|
|
if(empty(self::$extra_methods[$class])) { |
|
733
|
|
|
$this->defineMethods(); |
|
734
|
|
|
} |
|
735
|
|
|
|
|
736
|
|
|
$method = strtolower($method); |
|
737
|
|
|
|
|
738
|
|
|
if(isset(self::$extra_methods[$class][$method])) { |
|
739
|
|
|
$config = self::$extra_methods[$class][$method]; |
|
740
|
|
|
|
|
741
|
|
|
switch(true) { |
|
742
|
|
|
case isset($config['property']) : |
|
|
|
|
|
|
743
|
|
|
$obj = $config['index'] !== null ? |
|
744
|
|
|
$this->{$config['property']}[$config['index']] : |
|
745
|
|
|
$this->{$config['property']}; |
|
746
|
|
|
|
|
747
|
|
|
if($obj) { |
|
748
|
|
|
if(!empty($config['callSetOwnerFirst'])) $obj->setOwner($this); |
|
749
|
|
|
$retVal = call_user_func_array(array($obj, $method), $arguments); |
|
750
|
|
|
if(!empty($config['callSetOwnerFirst'])) $obj->clearOwner(); |
|
751
|
|
|
return $retVal; |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
|
|
if($this->destroyed) { |
|
|
|
|
|
|
755
|
|
|
throw new Exception ( |
|
756
|
|
|
"Object->__call(): attempt to call $method on a destroyed $class object" |
|
757
|
|
|
); |
|
758
|
|
|
} else { |
|
759
|
|
|
throw new Exception ( |
|
760
|
|
|
"Object->__call(): $class cannot pass control to $config[property]($config[index])." |
|
761
|
|
|
. ' Perhaps this object was mistakenly destroyed?' |
|
762
|
|
|
); |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
case isset($config['wrap']) : |
|
|
|
|
|
|
766
|
|
|
array_unshift($arguments, $config['method']); |
|
767
|
|
|
return call_user_func_array(array($this, $config['wrap']), $arguments); |
|
768
|
|
|
|
|
769
|
|
|
case isset($config['function']) : |
|
|
|
|
|
|
770
|
|
|
return $config['function']($this, $arguments); |
|
771
|
|
|
|
|
772
|
|
|
default : |
|
|
|
|
|
|
773
|
|
|
throw new Exception ( |
|
774
|
|
|
"Object->__call(): extra method $method is invalid on $class:" |
|
775
|
|
|
. var_export($config, true) |
|
776
|
|
|
); |
|
777
|
|
|
} |
|
778
|
|
|
} else { |
|
779
|
|
|
// Please do not change the exception code number below. |
|
780
|
|
|
throw new Exception("Object->__call(): the method '$method' does not exist on '$class', or the method is not public.", 2175); |
|
781
|
|
|
} |
|
782
|
|
|
} |
|
783
|
|
|
|
|
784
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
785
|
|
|
|
|
786
|
|
|
/** |
|
787
|
|
|
* Return TRUE if a method exists on this object |
|
788
|
|
|
* |
|
789
|
|
|
* This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via |
|
790
|
|
|
* extensions |
|
791
|
|
|
* |
|
792
|
|
|
* @param string $method |
|
793
|
|
|
* @return bool |
|
794
|
|
|
*/ |
|
795
|
|
|
public function hasMethod($method) { |
|
796
|
|
|
return method_exists($this, $method) || isset(self::$extra_methods[get_class($this)][strtolower($method)]); |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
/** |
|
800
|
|
|
* Return the names of all the methods available on this object |
|
801
|
|
|
* |
|
802
|
|
|
* @param bool $custom include methods added dynamically at runtime |
|
803
|
|
|
* @return array |
|
804
|
|
|
*/ |
|
805
|
|
|
public function allMethodNames($custom = false) { |
|
806
|
|
|
$class = get_class($this); |
|
807
|
|
|
if(!isset(self::$built_in_methods[$class])) { |
|
808
|
|
|
self::$built_in_methods[$class] = array_map('strtolower', get_class_methods($this)); |
|
809
|
|
|
} |
|
810
|
|
|
|
|
811
|
|
|
if($custom && isset(self::$extra_methods[$class])) { |
|
812
|
|
|
return array_merge(self::$built_in_methods[$class], array_keys(self::$extra_methods[$class])); |
|
813
|
|
|
} else { |
|
814
|
|
|
return self::$built_in_methods[$class]; |
|
815
|
|
|
} |
|
816
|
|
|
} |
|
817
|
|
|
|
|
818
|
|
|
/** |
|
819
|
|
|
* Adds any methods from {@link Extension} instances attached to this object. |
|
820
|
|
|
* All these methods can then be called directly on the instance (transparently |
|
821
|
|
|
* mapped through {@link __call()}), or called explicitly through {@link extend()}. |
|
822
|
|
|
* |
|
823
|
|
|
* @uses addMethodsFrom() |
|
824
|
|
|
*/ |
|
825
|
|
|
protected function defineMethods() { |
|
|
|
|
|
|
826
|
|
|
if($this->extension_instances) foreach(array_keys($this->extension_instances) as $key) { |
|
|
|
|
|
|
827
|
|
|
$this->addMethodsFrom('extension_instances', $key); |
|
828
|
|
|
} |
|
829
|
|
|
|
|
830
|
|
|
$class = get_class($this); |
|
831
|
|
|
if(isset($_REQUEST['debugmethods']) && isset(self::$built_in_methods[$class])) { |
|
832
|
|
|
Debug::require_developer_login(); |
|
833
|
|
|
|
|
834
|
|
|
echo "<h2>Methods defined on $class</h2><ul>"; |
|
835
|
|
|
foreach(self::$built_in_methods[$class] as $method) { |
|
836
|
|
|
echo "<li>$method</li>"; |
|
837
|
|
|
} |
|
838
|
|
|
echo '</ul>'; |
|
839
|
|
|
} |
|
840
|
|
|
} |
|
841
|
|
|
|
|
842
|
|
|
/** |
|
843
|
|
|
* @param SS_Object $extension |
|
844
|
|
|
* @return array |
|
845
|
|
|
*/ |
|
846
|
|
|
protected function findMethodsFromExtension($extension) { |
|
847
|
|
|
if (method_exists($extension, 'allMethodNames')) { |
|
848
|
|
|
if ($extension instanceof Extension) $extension->setOwner($this); |
|
849
|
|
|
$methods = $extension->allMethodNames(true); |
|
850
|
|
|
if ($extension instanceof Extension) $extension->clearOwner(); |
|
851
|
|
|
} else { |
|
852
|
|
|
if (!isset(self::$built_in_methods[$extension->class])) { |
|
853
|
|
|
self::$built_in_methods[$extension->class] = array_map('strtolower', get_class_methods($extension)); |
|
854
|
|
|
} |
|
855
|
|
|
$methods = self::$built_in_methods[$extension->class]; |
|
856
|
|
|
} |
|
857
|
|
|
|
|
858
|
|
|
return $methods; |
|
859
|
|
|
} |
|
860
|
|
|
|
|
861
|
|
|
/** |
|
862
|
|
|
* Add all the methods from an object property (which is an {@link Extension}) to this object. |
|
863
|
|
|
* |
|
864
|
|
|
* @param string $property the property name |
|
865
|
|
|
* @param string|int $index an index to use if the property is an array |
|
866
|
|
|
*/ |
|
867
|
|
|
protected function addMethodsFrom($property, $index = null) { |
|
868
|
|
|
$class = get_class($this); |
|
869
|
|
|
$extension = ($index !== null) ? $this->{$property}[$index] : $this->$property; |
|
870
|
|
|
|
|
871
|
|
|
if(!$extension) { |
|
872
|
|
|
throw new InvalidArgumentException ( |
|
873
|
|
|
"Object->addMethodsFrom(): could not add methods from {$class}->{$property}[$index]" |
|
874
|
|
|
); |
|
875
|
|
|
} |
|
876
|
|
|
|
|
877
|
|
|
$methods = $this->findMethodsFromExtension($extension); |
|
878
|
|
|
if ($methods) { |
|
|
|
|
|
|
879
|
|
|
$methodInfo = array( |
|
880
|
|
|
'property' => $property, |
|
881
|
|
|
'index' => $index, |
|
882
|
|
|
'callSetOwnerFirst' => $extension instanceof Extension, |
|
883
|
|
|
); |
|
884
|
|
|
|
|
885
|
|
|
$newMethods = array_fill_keys($methods, $methodInfo); |
|
886
|
|
|
|
|
887
|
|
|
if(isset(self::$extra_methods[$class])) { |
|
888
|
|
|
self::$extra_methods[$class] = |
|
889
|
|
|
array_merge(self::$extra_methods[$class], $newMethods); |
|
890
|
|
|
} else { |
|
891
|
|
|
self::$extra_methods[$class] = $newMethods; |
|
892
|
|
|
} |
|
893
|
|
|
} |
|
894
|
|
|
} |
|
895
|
|
|
|
|
896
|
|
|
/** |
|
897
|
|
|
* Add all the methods from an object property (which is an {@link Extension}) to this object. |
|
898
|
|
|
* |
|
899
|
|
|
* @param string $property the property name |
|
900
|
|
|
* @param string|int $index an index to use if the property is an array |
|
901
|
|
|
*/ |
|
902
|
|
|
protected function removeMethodsFrom($property, $index = null) { |
|
903
|
|
|
$extension = ($index !== null) ? $this->{$property}[$index] : $this->$property; |
|
904
|
|
|
|
|
905
|
|
|
if(!$extension) { |
|
906
|
|
|
throw new InvalidArgumentException ( |
|
907
|
|
|
"Object->removeMethodsFrom(): could not remove methods from {$this->class}->{$property}[$index]" |
|
908
|
|
|
); |
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
$methods = $this->findMethodsFromExtension($extension); |
|
912
|
|
|
if ($methods) { |
|
|
|
|
|
|
913
|
|
|
foreach ($methods as $method) { |
|
914
|
|
|
$methodInfo = self::$extra_methods[$this->class][$method]; |
|
915
|
|
|
|
|
916
|
|
|
if ($methodInfo['property'] === $property && $methodInfo['index'] === $index) { |
|
917
|
|
|
unset(self::$extra_methods[$this->class][$method]); |
|
918
|
|
|
} |
|
919
|
|
|
} |
|
920
|
|
|
|
|
921
|
|
|
if (empty(self::$extra_methods[$this->class])) { |
|
922
|
|
|
unset(self::$extra_methods[$this->class]); |
|
923
|
|
|
} |
|
924
|
|
|
} |
|
925
|
|
|
} |
|
926
|
|
|
|
|
927
|
|
|
/** |
|
928
|
|
|
* Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x) |
|
929
|
|
|
* can be wrapped to generateThumbnail(x) |
|
930
|
|
|
* |
|
931
|
|
|
* @param string $method the method name to wrap |
|
932
|
|
|
* @param string $wrap the method name to wrap to |
|
933
|
|
|
*/ |
|
934
|
|
|
protected function addWrapperMethod($method, $wrap) { |
|
935
|
|
|
self::$extra_methods[get_class($this)][strtolower($method)] = array ( |
|
936
|
|
|
'wrap' => $wrap, |
|
937
|
|
|
'method' => $method |
|
938
|
|
|
); |
|
939
|
|
|
} |
|
940
|
|
|
|
|
941
|
|
|
/** |
|
942
|
|
|
* Add an extra method using raw PHP code passed as a string |
|
943
|
|
|
* |
|
944
|
|
|
* @param string $method the method name |
|
945
|
|
|
* @param string $code the PHP code - arguments will be in an array called $args, while you can access this object |
|
946
|
|
|
* by using $obj. Note that you cannot call protected methods, as the method is actually an external |
|
947
|
|
|
* function |
|
948
|
|
|
*/ |
|
949
|
|
|
protected function createMethod($method, $code) { |
|
950
|
|
|
self::$extra_methods[get_class($this)][strtolower($method)] = array ( |
|
951
|
|
|
'function' => function($obj, $args) use ($code) { |
|
|
|
|
|
|
952
|
|
|
return eval($code); |
|
|
|
|
|
|
953
|
|
|
} |
|
954
|
|
|
); |
|
955
|
|
|
} |
|
956
|
|
|
|
|
957
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
958
|
|
|
|
|
959
|
|
|
/** |
|
960
|
|
|
* @see SS_Object::get_static() |
|
961
|
|
|
*/ |
|
962
|
|
|
public function stat($name, $uncached = false) { |
|
|
|
|
|
|
963
|
|
|
return Config::inst()->get(($this->class ? $this->class : get_class($this)), $name, Config::FIRST_SET); |
|
964
|
|
|
} |
|
965
|
|
|
|
|
966
|
|
|
/** |
|
967
|
|
|
* @see SS_Object::set_static() |
|
968
|
|
|
*/ |
|
969
|
|
|
public function set_stat($name, $value) { |
|
970
|
|
|
Config::inst()->update(($this->class ? $this->class : get_class($this)), $name, $value); |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
/** |
|
974
|
|
|
* @see SS_Object::uninherited_static() |
|
975
|
|
|
*/ |
|
976
|
|
|
public function uninherited($name) { |
|
977
|
|
|
return Config::inst()->get(($this->class ? $this->class : get_class($this)), $name, Config::UNINHERITED); |
|
978
|
|
|
} |
|
979
|
|
|
|
|
980
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
981
|
|
|
|
|
982
|
|
|
/** |
|
983
|
|
|
* Return true if this object "exists" i.e. has a sensible value |
|
984
|
|
|
* |
|
985
|
|
|
* This method should be overriden in subclasses to provide more context about the classes state. For example, a |
|
986
|
|
|
* {@link DataObject} class could return false when it is deleted from the database |
|
987
|
|
|
* |
|
988
|
|
|
* @return bool |
|
989
|
|
|
*/ |
|
990
|
|
|
public function exists() { |
|
991
|
|
|
return true; |
|
992
|
|
|
} |
|
993
|
|
|
|
|
994
|
|
|
/** |
|
995
|
|
|
* @return string this classes parent class |
|
996
|
|
|
*/ |
|
997
|
|
|
public function parentClass() { |
|
998
|
|
|
return get_parent_class($this); |
|
999
|
|
|
} |
|
1000
|
|
|
|
|
1001
|
|
|
/** |
|
1002
|
|
|
* Check if this class is an instance of a specific class, or has that class as one of its parents |
|
1003
|
|
|
* |
|
1004
|
|
|
* @param string $class |
|
1005
|
|
|
* @return bool |
|
1006
|
|
|
*/ |
|
1007
|
|
|
public function is_a($class) { |
|
1008
|
|
|
return $this instanceof $class; |
|
1009
|
|
|
} |
|
1010
|
|
|
|
|
1011
|
|
|
/** |
|
1012
|
|
|
* @return string the class name |
|
1013
|
|
|
*/ |
|
1014
|
|
|
public function __toString() { |
|
1015
|
|
|
return $this->class; |
|
1016
|
|
|
} |
|
1017
|
|
|
|
|
1018
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
1019
|
|
|
|
|
1020
|
|
|
/** |
|
1021
|
|
|
* Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge |
|
1022
|
|
|
* all results into an array |
|
1023
|
|
|
* |
|
1024
|
|
|
* @param string $method the method name to call |
|
1025
|
|
|
* @param mixed $argument a single argument to pass |
|
1026
|
|
|
* @return mixed |
|
1027
|
|
|
* @todo integrate inheritance rules |
|
1028
|
|
|
*/ |
|
1029
|
|
|
public function invokeWithExtensions($method, $argument = null) { |
|
1030
|
|
|
$result = method_exists($this, $method) ? array($this->$method($argument)) : array(); |
|
1031
|
|
|
$extras = $this->extend($method, $argument); |
|
1032
|
|
|
|
|
1033
|
|
|
return $extras ? array_merge($result, $extras) : $result; |
|
1034
|
|
|
} |
|
1035
|
|
|
|
|
1036
|
|
|
/** |
|
1037
|
|
|
* Run the given function on all of this object's extensions. Note that this method originally returned void, so if |
|
1038
|
|
|
* you wanted to return results, you're hosed |
|
1039
|
|
|
* |
|
1040
|
|
|
* Currently returns an array, with an index resulting every time the function is called. Only adds returns if |
|
1041
|
|
|
* they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for |
|
1042
|
|
|
* permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't |
|
1043
|
|
|
* do type checking, an included NULL return would fail the permission checks. |
|
1044
|
|
|
* |
|
1045
|
|
|
* The extension methods are defined during {@link __construct()} in {@link defineMethods()}. |
|
1046
|
|
|
* |
|
1047
|
|
|
* @param string $method the name of the method to call on each extension |
|
1048
|
|
|
* @param mixed $a1,... up to 7 arguments to be passed to the method |
|
|
|
|
|
|
1049
|
|
|
* @return array |
|
1050
|
|
|
*/ |
|
1051
|
|
|
public function extend($method, &$a1=null, &$a2=null, &$a3=null, &$a4=null, &$a5=null, &$a6=null, &$a7=null) { |
|
1052
|
|
|
$values = array(); |
|
1053
|
|
|
|
|
1054
|
|
|
if(!empty($this->beforeExtendCallbacks[$method])) { |
|
1055
|
|
|
foreach(array_reverse($this->beforeExtendCallbacks[$method]) as $callback) { |
|
1056
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7)); |
|
1057
|
|
|
if($value !== null) $values[] = $value; |
|
1058
|
|
|
} |
|
1059
|
|
|
$this->beforeExtendCallbacks[$method] = array(); |
|
1060
|
|
|
} |
|
1061
|
|
|
|
|
1062
|
|
|
if($this->extension_instances) foreach($this->extension_instances as $instance) { |
|
|
|
|
|
|
1063
|
|
|
if(method_exists($instance, $method)) { |
|
1064
|
|
|
$instance->setOwner($this); |
|
1065
|
|
|
$value = $instance->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7); |
|
1066
|
|
|
if($value !== null) $values[] = $value; |
|
1067
|
|
|
$instance->clearOwner(); |
|
1068
|
|
|
} |
|
1069
|
|
|
} |
|
1070
|
|
|
|
|
1071
|
|
|
if(!empty($this->afterExtendCallbacks[$method])) { |
|
1072
|
|
|
foreach(array_reverse($this->afterExtendCallbacks[$method]) as $callback) { |
|
1073
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7)); |
|
1074
|
|
|
if($value !== null) $values[] = $value; |
|
1075
|
|
|
} |
|
1076
|
|
|
$this->afterExtendCallbacks[$method] = array(); |
|
1077
|
|
|
} |
|
1078
|
|
|
|
|
1079
|
|
|
return $values; |
|
1080
|
|
|
} |
|
1081
|
|
|
|
|
1082
|
|
|
/** |
|
1083
|
|
|
* Get an extension instance attached to this object by name. |
|
1084
|
|
|
* |
|
1085
|
|
|
* @uses hasExtension() |
|
1086
|
|
|
* |
|
1087
|
|
|
* @param string $extension |
|
1088
|
|
|
* @return Extension |
|
1089
|
|
|
*/ |
|
1090
|
|
|
public function getExtensionInstance($extension) { |
|
1091
|
|
|
if($this->hasExtension($extension)) return $this->extension_instances[$extension]; |
|
1092
|
|
|
} |
|
1093
|
|
|
|
|
1094
|
|
|
/** |
|
1095
|
|
|
* Returns TRUE if this object instance has a specific extension applied |
|
1096
|
|
|
* in {@link $extension_instances}. Extension instances are initialized |
|
1097
|
|
|
* at constructor time, meaning if you use {@link add_extension()} |
|
1098
|
|
|
* afterwards, the added extension will just be added to new instances |
|
1099
|
|
|
* of the extended class. Use the static method {@link has_extension()} |
|
1100
|
|
|
* to check if a class (not an instance) has a specific extension. |
|
1101
|
|
|
* Caution: Don't use singleton(<class>)->hasExtension() as it will |
|
1102
|
|
|
* give you inconsistent results based on when the singleton was first |
|
1103
|
|
|
* accessed. |
|
1104
|
|
|
* |
|
1105
|
|
|
* @param string $extension Classname of an {@link Extension} subclass without parameters |
|
1106
|
|
|
* @return bool |
|
1107
|
|
|
*/ |
|
1108
|
|
|
public function hasExtension($extension) { |
|
1109
|
|
|
return isset($this->extension_instances[$extension]); |
|
1110
|
|
|
} |
|
1111
|
|
|
|
|
1112
|
|
|
/** |
|
1113
|
|
|
* Get all extension instances for this specific object instance. |
|
1114
|
|
|
* See {@link get_extensions()} to get all applied extension classes |
|
1115
|
|
|
* for this class (not the instance). |
|
1116
|
|
|
* |
|
1117
|
|
|
* @return array Map of {@link DataExtension} instances, keyed by classname. |
|
1118
|
|
|
*/ |
|
1119
|
|
|
public function getExtensionInstances() { |
|
1120
|
|
|
return $this->extension_instances; |
|
1121
|
|
|
} |
|
1122
|
|
|
|
|
1123
|
|
|
// -------------------------------------------------------------------------------------------------------------- |
|
1124
|
|
|
|
|
1125
|
|
|
/** |
|
1126
|
|
|
* Cache the results of an instance method in this object to a file, or if it is already cache return the cached |
|
1127
|
|
|
* results |
|
1128
|
|
|
* |
|
1129
|
|
|
* @param string $method the method name to cache |
|
1130
|
|
|
* @param int $lifetime the cache lifetime in seconds |
|
1131
|
|
|
* @param string $ID custom cache ID to use |
|
1132
|
|
|
* @param array $arguments an optional array of arguments |
|
1133
|
|
|
* @return mixed the cached data |
|
1134
|
|
|
*/ |
|
1135
|
|
|
public function cacheToFile($method, $lifetime = 3600, $ID = false, $arguments = array()) { |
|
1136
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.'); |
|
1137
|
|
|
|
|
1138
|
|
|
if(!$this->hasMethod($method)) { |
|
1139
|
|
|
throw new InvalidArgumentException("Object->cacheToFile(): the method $method does not exist to cache"); |
|
1140
|
|
|
} |
|
1141
|
|
|
|
|
1142
|
|
|
$cacheName = $this->class . '_' . $method; |
|
1143
|
|
|
|
|
1144
|
|
|
if(!is_array($arguments)) $arguments = array($arguments); |
|
1145
|
|
|
|
|
1146
|
|
|
if($ID) $cacheName .= '_' . $ID; |
|
|
|
|
|
|
1147
|
|
|
if(count($arguments)) $cacheName .= '_' . md5(serialize($arguments)); |
|
1148
|
|
|
|
|
1149
|
|
|
$data = $this->loadCache($cacheName, $lifetime); |
|
1150
|
|
|
|
|
1151
|
|
|
if($data !== false) { |
|
1152
|
|
|
return $data; |
|
1153
|
|
|
} |
|
1154
|
|
|
|
|
1155
|
|
|
$data = call_user_func_array(array($this, $method), $arguments); |
|
1156
|
|
|
$this->saveCache($cacheName, $data); |
|
1157
|
|
|
|
|
1158
|
|
|
return $data; |
|
1159
|
|
|
} |
|
1160
|
|
|
|
|
1161
|
|
|
/** |
|
1162
|
|
|
* Clears the cache for the given cacheToFile call |
|
1163
|
|
|
*/ |
|
1164
|
|
|
public function clearCache($method, $ID = false, $arguments = array()) { |
|
1165
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.'); |
|
1166
|
|
|
|
|
1167
|
|
|
$cacheName = $this->class . '_' . $method; |
|
1168
|
|
|
if(!is_array($arguments)) $arguments = array($arguments); |
|
1169
|
|
|
if($ID) $cacheName .= '_' . $ID; |
|
1170
|
|
|
if(count($arguments)) $cacheName .= '_' . md5(serialize($arguments)); |
|
1171
|
|
|
|
|
1172
|
|
|
$file = TEMP_FOLDER . '/' . $this->sanitiseCachename($cacheName); |
|
1173
|
|
|
if(file_exists($file)) unlink($file); |
|
1174
|
|
|
} |
|
1175
|
|
|
|
|
1176
|
|
|
/** |
|
1177
|
|
|
* Loads a cache from the filesystem if a valid on is present and within the specified lifetime |
|
1178
|
|
|
* |
|
1179
|
|
|
* @param string $cache the cache name |
|
1180
|
|
|
* @param int $lifetime the lifetime (in seconds) of the cache before it is invalid |
|
1181
|
|
|
* @return mixed |
|
1182
|
|
|
*/ |
|
1183
|
|
|
protected function loadCache($cache, $lifetime = 3600) { |
|
|
|
|
|
|
1184
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.'); |
|
1185
|
|
|
|
|
1186
|
|
|
$path = TEMP_FOLDER . '/' . $this->sanitiseCachename($cache); |
|
1187
|
|
|
|
|
1188
|
|
|
if(!isset($_REQUEST['flush']) && file_exists($path) && (filemtime($path) + $lifetime) > time()) { |
|
1189
|
|
|
return unserialize(file_get_contents($path)); |
|
1190
|
|
|
} |
|
1191
|
|
|
|
|
1192
|
|
|
return false; |
|
1193
|
|
|
} |
|
1194
|
|
|
|
|
1195
|
|
|
/** |
|
1196
|
|
|
* Save a piece of cached data to the file system |
|
1197
|
|
|
* |
|
1198
|
|
|
* @param string $cache the cache name |
|
1199
|
|
|
* @param mixed $data data to save (must be serializable) |
|
1200
|
|
|
*/ |
|
1201
|
|
|
protected function saveCache($cache, $data) { |
|
1202
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.'); |
|
1203
|
|
|
file_put_contents(TEMP_FOLDER . '/' . $this->sanitiseCachename($cache), serialize($data)); |
|
1204
|
|
|
} |
|
1205
|
|
|
|
|
1206
|
|
|
/** |
|
1207
|
|
|
* Strip a file name of special characters so it is suitable for use as a cache file name |
|
1208
|
|
|
* |
|
1209
|
|
|
* @param string $name |
|
1210
|
|
|
* @return string the name with all special cahracters replaced with underscores |
|
1211
|
|
|
*/ |
|
1212
|
|
|
protected function sanitiseCachename($name) { |
|
1213
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.'); |
|
1214
|
|
|
return str_replace(array('~', '.', '/', '!', ' ', "\n", "\r", "\t", '\\', ':', '"', '\'', ';'), '_', $name); |
|
1215
|
|
|
} |
|
1216
|
|
|
|
|
1217
|
|
|
} |
|
1218
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.