2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* A base class for all SilverStripe objects to inherit from.
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
|
|
|
* This class provides a number of pattern implementations, as well as methods and fixes to add extra psuedo-static
|
2009-04-01 00:27:53 +02:00
|
|
|
* and method functionality to PHP.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* See {@link Extension} on how to implement a custom multiple
|
|
|
|
* inheritance for object instances based on PHP5 method call overloading.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @todo Create instance-specific removeExtension() which removes an extension from $extension_instances,
|
|
|
|
* but not from static $extensions, and clears everything added through defineMethods(), mainly $extra_methods.
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage core
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
abstract class Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* An array of extension names and parameters to be applied to this object upon construction.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* Example:
|
2009-03-14 01:16:32 +01:00
|
|
|
* <code>
|
2013-03-21 19:48:54 +01:00
|
|
|
* private static $extensions = array (
|
2011-10-19 00:32:52 +02:00
|
|
|
* 'Hierarchy',
|
2009-03-14 01:16:32 +01:00
|
|
|
* "Version('Stage', 'Live')"
|
|
|
|
* );
|
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* Use {@link Object::add_extension()} to add extensions without access to the class code,
|
|
|
|
* e.g. to extend core classes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* Extensions are instanciated together with the object and stored in {@link $extension_instances}.
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @var array $extensions
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
private static
|
|
|
|
$classes_constructed = array(),
|
|
|
|
$extra_methods = array(),
|
|
|
|
$built_in_methods = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
private static
|
|
|
|
$custom_classes = array(),
|
|
|
|
$strong_classes = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**#@-*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* @var string the class name
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public $class;
|
2011-12-22 03:24:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
|
|
|
|
* @return Config_ForClass|null
|
|
|
|
*/
|
2013-02-27 01:33:36 +01:00
|
|
|
static public function config() {
|
|
|
|
return Config::inst()->forClass(get_called_class());
|
2011-12-22 03:24:05 +01:00
|
|
|
}
|
|
|
|
|
2007-08-16 08:37:49 +02:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* @var array all current extension instances.
|
2007-08-16 08:37:49 +02:00
|
|
|
*/
|
|
|
|
protected $extension_instances = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
/**
|
|
|
|
* List of callbacks to call prior to extensions having extend called on them,
|
|
|
|
* each grouped by methodName.
|
|
|
|
*
|
|
|
|
* @var array[callable]
|
|
|
|
*/
|
|
|
|
protected $beforeExtendCallbacks = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows user code to hook into Object::extend prior to control
|
|
|
|
* being delegated to extensions. Each callback will be reset
|
|
|
|
* once called.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-16 00:34:45 +02:00
|
|
|
* @param string $method The name of the method to hook into
|
|
|
|
* @param callable $callback The callback to execute
|
|
|
|
*/
|
|
|
|
protected function beforeExtending($method, $callback) {
|
|
|
|
if(empty($this->beforeExtendCallbacks[$method])) {
|
|
|
|
$this->beforeExtendCallbacks[$method] = array();
|
|
|
|
}
|
|
|
|
$this->beforeExtendCallbacks[$method][] = $callback;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
/**
|
|
|
|
* List of callbacks to call after extensions having extend called on them,
|
|
|
|
* each grouped by methodName.
|
|
|
|
*
|
|
|
|
* @var array[callable]
|
|
|
|
*/
|
|
|
|
protected $afterExtendCallbacks = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows user code to hook into Object::extend after control
|
|
|
|
* being delegated to extensions. Each callback will be reset
|
|
|
|
* once called.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-05-16 00:34:45 +02:00
|
|
|
* @param string $method The name of the method to hook into
|
|
|
|
* @param callable $callback The callback to execute
|
|
|
|
*/
|
|
|
|
protected function afterExtending($method, $callback) {
|
|
|
|
if(empty($this->afterExtendCallbacks[$method])) {
|
|
|
|
$this->afterExtendCallbacks[$method] = array();
|
|
|
|
}
|
|
|
|
$this->afterExtendCallbacks[$method][] = $callback;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* An implementation of the factory method, allows you to create an instance of a class
|
|
|
|
*
|
|
|
|
* This method first for strong class overloads (singletons & DB interaction), then custom class overloads. If an
|
|
|
|
* overload is found, an instance of this is returned rather than the original class. To overload a class, use
|
|
|
|
* {@link Object::useCustomClass()}
|
|
|
|
*
|
2012-03-27 06:57:42 +02:00
|
|
|
* This can be called in one of two ways - either calling via the class directly,
|
|
|
|
* or calling on Object and passing the class name as the first parameter. The following
|
|
|
|
* are equivalent:
|
|
|
|
* $list = DataList::create('SiteTree');
|
2012-05-28 11:13:42 +02:00
|
|
|
* $list = SiteTree::get();
|
2012-03-27 06:57:42 +02:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $class the class name
|
|
|
|
* @param mixed $arguments,... arguments to pass to the constructor
|
2014-02-04 03:36:24 +01:00
|
|
|
* @return static
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public static function create() {
|
2012-05-28 11:13:42 +02:00
|
|
|
$args = func_get_args();
|
2012-03-27 06:57:42 +02:00
|
|
|
|
|
|
|
// Class to create should be the calling class if not Object,
|
|
|
|
// otherwise the first parameter
|
|
|
|
$class = get_called_class();
|
2012-05-28 11:13:42 +02:00
|
|
|
if($class == 'Object') $class = array_shift($args);
|
|
|
|
|
2012-03-27 06:57:42 +02:00
|
|
|
$class = self::getCustomClass($class);
|
2012-05-28 11:13:42 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
return Injector::inst()->createWithArgs($class, $args);
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-04 03:46:37 +02:00
|
|
|
/**
|
|
|
|
* Creates a class instance by the "singleton" design pattern.
|
|
|
|
* It will always return the same instance for this class,
|
|
|
|
* which can be used for performance reasons and as a simple
|
|
|
|
* way to access instance methods which don't rely on instance
|
|
|
|
* data (e.g. the custom SilverStripe static handling).
|
|
|
|
*
|
|
|
|
* @param string $className Optional classname (if called on Object directly)
|
|
|
|
* @return static The singleton instance
|
|
|
|
*/
|
|
|
|
public static function singleton() {
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
// Singleton to create should be the calling class if not Object,
|
|
|
|
// otherwise the first parameter
|
|
|
|
$class = get_called_class();
|
|
|
|
if($class === 'Object') $class = array_shift($args);
|
|
|
|
|
|
|
|
return Injector::inst()->get($class);
|
|
|
|
}
|
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
private static $_cache_inst_args = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
/**
|
|
|
|
* Create an object from a string representation. It treats it as a PHP constructor without the
|
|
|
|
* 'new' keyword. It also manages to construct the object without the use of eval().
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* Construction itself is done with Object::create(), so that Object::useCustomClass() calls
|
|
|
|
* are respected.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* `Object::create_from_string("Versioned('Stage','Live')")` will return the result of
|
2012-04-04 16:59:30 +02:00
|
|
|
* `Versioned::create('Stage', 'Live);`
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* It is designed for simple, clonable objects. The first time this method is called for a given
|
|
|
|
* string it is cached, and clones of that object are returned.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* If you pass the $firstArg argument, this will be prepended to the constructor arguments. It's
|
|
|
|
* impossible to pass null as the firstArg argument.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* `Object::create_from_string("Varchar(50)", "MyField")` will return the result of
|
2012-04-04 16:59:30 +02:00
|
|
|
* `Vachar::create('MyField', '50');`
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-04 06:46:41 +02:00
|
|
|
* Arguments are always strings, although this is a quirk of the current implementation rather
|
|
|
|
* than something that can be relied upon.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function create_from_string($classSpec, $firstArg = null) {
|
2010-10-04 06:46:41 +02:00
|
|
|
if(!isset(self::$_cache_inst_args[$classSpec.$firstArg])) {
|
|
|
|
// an $extension value can contain parameters as a string,
|
|
|
|
// e.g. "Versioned('Stage','Live')"
|
|
|
|
if(strpos($classSpec,'(') === false) {
|
|
|
|
if($firstArg === null) self::$_cache_inst_args[$classSpec.$firstArg] = Object::create($classSpec);
|
|
|
|
else self::$_cache_inst_args[$classSpec.$firstArg] = Object::create($classSpec, $firstArg);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
} else {
|
|
|
|
list($class, $args) = self::parse_class_spec($classSpec);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
if($firstArg !== null) array_unshift($args, $firstArg);
|
|
|
|
array_unshift($args, $class);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:55 +02:00
|
|
|
self::$_cache_inst_args[$classSpec.$firstArg] = call_user_func_array(array('Object','create'), $args);
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
return clone self::$_cache_inst_args[$classSpec.$firstArg];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
/**
|
|
|
|
* Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().
|
|
|
|
* Returns a 2-elemnent array, with classname and arguments
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function parse_class_spec($classSpec) {
|
2010-10-04 06:46:41 +02:00
|
|
|
$tokens = token_get_all("<?php $classSpec");
|
|
|
|
$class = null;
|
|
|
|
$args = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:47:08 +02:00
|
|
|
// Keep track of the current bucket that we're putting data into
|
|
|
|
$bucket = &$args;
|
|
|
|
$bucketStack = array();
|
2012-12-18 03:00:45 +01:00
|
|
|
$had_ns = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
foreach($tokens as $token) {
|
|
|
|
$tName = is_array($token) ? $token[0] : $token;
|
|
|
|
// Get the class naem
|
|
|
|
if($class == null && is_array($token) && $token[0] == T_STRING) {
|
|
|
|
$class = $token[1];
|
2012-12-18 03:00:45 +01:00
|
|
|
} elseif(is_array($token) && $token[0] == T_NS_SEPARATOR) {
|
|
|
|
$class .= $token[1];
|
|
|
|
$had_ns = true;
|
|
|
|
} elseif ($had_ns && is_array($token) && $token[0] == T_STRING) {
|
|
|
|
$class .= $token[1];
|
|
|
|
$had_ns = false;
|
2010-10-04 06:46:41 +02:00
|
|
|
// Get arguments
|
|
|
|
} else if(is_array($token)) {
|
|
|
|
switch($token[0]) {
|
|
|
|
case T_CONSTANT_ENCAPSED_STRING:
|
|
|
|
$argString = $token[1];
|
|
|
|
switch($argString[0]) {
|
2012-09-26 23:34:00 +02:00
|
|
|
case '"':
|
|
|
|
$argString = stripcslashes(substr($argString,1,-1));
|
|
|
|
break;
|
|
|
|
case "'":
|
|
|
|
$argString = str_replace(array("\\\\", "\\'"),array("\\", "'"), substr($argString,1,-1));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString");
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
2010-10-04 06:47:08 +02:00
|
|
|
$bucket[] = $argString;
|
2010-10-04 06:46:41 +02:00
|
|
|
break;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
case T_DNUMBER:
|
2010-10-04 06:47:08 +02:00
|
|
|
$bucket[] = (double)$token[1];
|
2010-10-04 06:46:41 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LNUMBER:
|
2010-10-04 06:47:08 +02:00
|
|
|
$bucket[] = (int)$token[1];
|
2010-10-04 06:46:41 +02:00
|
|
|
break;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
case T_STRING:
|
|
|
|
switch($token[1]) {
|
2013-03-22 04:34:17 +01:00
|
|
|
case 'true': $bucket[] = true; break;
|
|
|
|
case 'false': $bucket[] = false; break;
|
|
|
|
case 'null': $bucket[] = null; break;
|
2010-10-04 06:46:41 +02:00
|
|
|
default: throw new Exception("Bad T_STRING arg '{$token[1]}'");
|
|
|
|
}
|
2013-03-22 04:34:17 +01:00
|
|
|
break;
|
|
|
|
|
2010-10-04 06:47:08 +02:00
|
|
|
case T_ARRAY:
|
|
|
|
// Add an empty array to the bucket
|
|
|
|
$bucket[] = array();
|
|
|
|
$bucketStack[] = &$bucket;
|
|
|
|
$bucket = &$bucket[sizeof($bucket)-1];
|
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 06:47:08 +02:00
|
|
|
} else {
|
2013-05-05 02:19:31 +02:00
|
|
|
if($tName == '[') {
|
|
|
|
// Add an empty array to the bucket
|
|
|
|
$bucket[] = array();
|
|
|
|
$bucketStack[] = &$bucket;
|
|
|
|
$bucket = &$bucket[sizeof($bucket)-1];
|
|
|
|
} elseif($tName == ')' || $tName == ']') {
|
2010-10-04 06:47:08 +02:00
|
|
|
// Pop-by-reference
|
|
|
|
$bucket = &$bucketStack[sizeof($bucketStack)-1];
|
|
|
|
array_pop($bucketStack);
|
|
|
|
}
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
return array($class, $args);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Similar to {@link Object::create()}, except that classes are only overloaded if you set the $strong parameter to
|
|
|
|
* TRUE when using {@link Object::useCustomClass()}
|
|
|
|
*
|
|
|
|
* @param string $class the class name
|
|
|
|
* @param mixed $arguments,... arguments to pass to the constructor
|
2014-02-04 03:36:24 +01:00
|
|
|
* @return static
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public static function strong_create() {
|
|
|
|
$args = func_get_args();
|
|
|
|
$class = array_shift($args);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(isset(self::$strong_classes[$class]) && ClassInfo::exists(self::$strong_classes[$class])) {
|
|
|
|
$class = self::$strong_classes[$class];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
RECOMMIT This is a recommit of a previously merged, but reverted, commit. The initial commit had problems when running "tests/all flush=all" when dev/build would re-analyse a bunch of stuff; fixes to address this went into https://github.com/silverstripe/sapphire/pull/487, but for some reason the actual base changes didn't, so I'm re-submitting them in a separate pull.
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
2012-06-03 12:40:46 +02:00
|
|
|
return Injector::inst()->createWithArgs($class, $args);
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* This class allows you to overload classes with other classes when they are constructed using the factory method
|
|
|
|
* {@link Object::create()}
|
|
|
|
*
|
|
|
|
* @param string $oldClass the class to replace
|
|
|
|
* @param string $newClass the class to replace it with
|
|
|
|
* @param bool $strong allows you to enforce a certain class replacement under all circumstances. This is used in
|
|
|
|
* singletons and DB interaction classes
|
|
|
|
*/
|
|
|
|
public static function useCustomClass($oldClass, $newClass, $strong = false) {
|
|
|
|
if($strong) {
|
|
|
|
self::$strong_classes[$oldClass] = $newClass;
|
|
|
|
} else {
|
|
|
|
self::$custom_classes[$oldClass] = $newClass;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* If a class has been overloaded, get the class name it has been overloaded with - otherwise return the class name
|
|
|
|
*
|
|
|
|
* @param string $class the class to check
|
|
|
|
* @return string the class that would be created if you called {@link Object::create()} with the class
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public static function getCustomClass($class) {
|
|
|
|
if(isset(self::$strong_classes[$class]) && ClassInfo::exists(self::$strong_classes[$class])) {
|
|
|
|
return self::$strong_classes[$class];
|
|
|
|
} elseif(isset(self::$custom_classes[$class]) && ClassInfo::exists(self::$custom_classes[$class])) {
|
|
|
|
return self::$custom_classes[$class];
|
2008-04-05 05:43:58 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
return $class;
|
2008-04-05 05:43:58 +02:00
|
|
|
}
|
2011-12-22 03:17:41 +01:00
|
|
|
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Get the value of a static property of a class, even in that property is declared protected (but not private),
|
|
|
|
* without any inheritance, merging or parent lookup if it doesn't exist on the given class.
|
2011-12-22 03:17:41 +01:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $class - The class to get the static from
|
|
|
|
* @param $name - The property to get from the class
|
|
|
|
* @param null $default - The value to return if property doesn't exist on class
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return any - The value of the static property $name on class $class, or $default if that property is not
|
|
|
|
* defined
|
2011-12-22 03:17:41 +01:00
|
|
|
*/
|
|
|
|
public static function static_lookup($class, $name, $default = null) {
|
2012-08-21 09:05:21 +02:00
|
|
|
if (is_subclass_of($class, 'Object')) {
|
2011-12-22 03:17:41 +01:00
|
|
|
if (isset($class::$$name)) {
|
|
|
|
$parent = get_parent_class($class);
|
|
|
|
if (!$parent || !isset($parent::$$name) || $parent::$$name !== $class::$$name) return $class::$$name;
|
|
|
|
}
|
|
|
|
return $default;
|
2012-05-04 00:26:09 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
// TODO: This gets set once, then not updated, so any changes to statics after this is called the first
|
|
|
|
// time for any class won't be exposed
|
2011-12-22 03:17:41 +01:00
|
|
|
static $static_properties = array();
|
|
|
|
|
|
|
|
if (!isset($static_properties[$class])) {
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
$static_properties[$class] = $reflection->getStaticProperties();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($static_properties[$class][$name])) {
|
|
|
|
$value = $static_properties[$class][$name];
|
|
|
|
|
|
|
|
$parent = get_parent_class($class);
|
|
|
|
if (!$parent) return $value;
|
|
|
|
|
|
|
|
if (!isset($static_properties[$parent])) {
|
|
|
|
$reflection = new ReflectionClass($parent);
|
|
|
|
$static_properties[$parent] = $reflection->getStaticProperties();
|
|
|
|
}
|
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
if (!isset($static_properties[$parent][$name]) || $static_properties[$parent][$name] !== $value) {
|
|
|
|
return $value;
|
|
|
|
}
|
2011-12-22 03:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2015-08-21 04:01:10 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function get_static($class, $name, $uncached = false) {
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get');
|
|
|
|
return Config::inst()->get($class, $name, Config::FIRST_SET);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function set_static($class, $name, $value) {
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#update');
|
|
|
|
Config::inst()->update($class, $name, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function uninherited_static($class, $name, $uncached = false) {
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get');
|
|
|
|
return Config::inst()->get($class, $name, Config::UNINHERITED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function combined_static($class, $name, $ceiling = false) {
|
|
|
|
if ($ceiling) throw new Exception('Ceiling argument to combined_static is no longer supported');
|
|
|
|
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#get');
|
|
|
|
return Config::inst()->get($class, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function addStaticVars($class, $properties, $replace = false) {
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#update');
|
|
|
|
foreach($properties as $prop => $value) self::add_static_var($class, $prop, $value, $replace);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
public static function add_static_var($class, $name, $value, $replace = false) {
|
|
|
|
Deprecation::notice('4.0', 'Replaced by Config#remove and Config#update');
|
|
|
|
|
|
|
|
if ($replace) Config::inst()->remove($class, $name);
|
|
|
|
Config::inst()->update($class, $name, $value);
|
|
|
|
}
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
2013-10-11 16:18:00 +02:00
|
|
|
* Return TRUE if a class has a specified extension.
|
2014-03-30 08:37:54 +02:00
|
|
|
* This supports backwards-compatible format (static Object::has_extension($requiredExtension))
|
|
|
|
* and new format ($object->has_extension($class, $requiredExtension))
|
|
|
|
* @param string $classOrExtension if 1 argument supplied, the class name of the extension to
|
|
|
|
* check for; if 2 supplied, the class name to test
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $requiredExtension used only if 2 arguments supplied
|
2014-10-24 05:58:50 +02:00
|
|
|
* @param boolean $strict if the extension has to match the required extension and not be a subclass
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2014-10-24 05:58:50 +02:00
|
|
|
public static function has_extension($classOrExtension, $requiredExtension = null, $strict = false) {
|
2013-10-11 16:18:00 +02:00
|
|
|
//BC support
|
|
|
|
if(func_num_args() > 1){
|
|
|
|
$class = $classOrExtension;
|
|
|
|
$requiredExtension = $requiredExtension;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class = get_called_class();
|
|
|
|
$requiredExtension = $classOrExtension;
|
|
|
|
}
|
2012-10-09 23:59:23 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
$requiredExtension = strtolower($requiredExtension);
|
2011-12-22 03:29:16 +01:00
|
|
|
$extensions = Config::inst()->get($class, 'extensions');
|
|
|
|
|
|
|
|
if($extensions) foreach($extensions as $extension) {
|
2009-04-01 00:27:53 +02:00
|
|
|
$left = strtolower(Extension::get_classname_without_arguments($extension));
|
|
|
|
$right = strtolower(Extension::get_classname_without_arguments($requiredExtension));
|
|
|
|
if($left == $right) return true;
|
2014-10-24 05:58:50 +02:00
|
|
|
if (!$strict && is_subclass_of($left, $right)) return true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* Add an extension to a specific class.
|
2013-04-11 11:32:31 +02:00
|
|
|
*
|
|
|
|
* The preferred method for adding extensions is through YAML config,
|
|
|
|
* since it avoids autoloading the class, and is easier to override in
|
|
|
|
* more specific configurations.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* As an alternative, extensions can be added to a specific class
|
|
|
|
* directly in the {@link Object::$extensions} array.
|
|
|
|
* See {@link SiteTree::$extensions} for examples.
|
|
|
|
* Keep in mind that the extension will only be applied to new
|
|
|
|
* instances, not existing ones (including all instances created through {@link singleton()}).
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
2013-04-11 11:32:31 +02:00
|
|
|
* @see http://doc.silverstripe.org/framework/en/trunk/reference/dataextension
|
2015-08-21 04:01:10 +02:00
|
|
|
* @param string $classOrExtension Class that should be extended - has to be a subclass of {@link Object}
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $extension Subclass of {@link Extension} with optional parameters
|
2009-04-01 00:27:53 +02:00
|
|
|
* as a string, e.g. "Versioned" or "Translatable('Param')"
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2013-04-09 14:56:26 +02:00
|
|
|
public static function add_extension($classOrExtension, $extension = null) {
|
|
|
|
if(func_num_args() > 1) {
|
|
|
|
$class = $classOrExtension;
|
|
|
|
} else {
|
|
|
|
$class = get_called_class();
|
|
|
|
$extension = $classOrExtension;
|
|
|
|
}
|
2012-10-09 05:55:37 +02:00
|
|
|
|
2009-07-07 03:28:47 +02:00
|
|
|
if(!preg_match('/^([^(]*)/', $extension, $matches)) {
|
2009-04-01 00:27:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$extensionClass = $matches[1];
|
|
|
|
if(!class_exists($extensionClass)) {
|
2015-09-20 10:18:48 +02:00
|
|
|
user_error(
|
|
|
|
sprintf('Object::add_extension() - Can\'t find extension class for "%s"', $extensionClass),
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2009-04-01 00:27:53 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-02-21 06:20:14 +01:00
|
|
|
if(!is_subclass_of($extensionClass, 'Extension')) {
|
2015-09-20 10:18:48 +02:00
|
|
|
user_error(
|
|
|
|
sprintf('Object::add_extension() - Extension "%s" is not a subclass of Extension', $extensionClass),
|
|
|
|
E_USER_ERROR
|
|
|
|
);
|
2009-04-01 00:27:53 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
// unset some caches
|
|
|
|
$subclasses = ClassInfo::subclassesFor($class);
|
|
|
|
$subclasses[] = $class;
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
if($subclasses) foreach($subclasses as $subclass) {
|
|
|
|
unset(self::$classes_constructed[$subclass]);
|
2010-04-12 03:50:38 +02:00
|
|
|
unset(self::$extra_methods[$subclass]);
|
2009-04-01 00:27:53 +02:00
|
|
|
}
|
2011-12-22 03:29:16 +01:00
|
|
|
|
|
|
|
Config::inst()->update($class, 'extensions', array($extension));
|
2013-02-26 04:39:40 +01:00
|
|
|
Config::inst()->extraConfigSourcesChanged($class);
|
|
|
|
|
2013-12-01 12:25:19 +01:00
|
|
|
Injector::inst()->unregisterNamedObject($class);
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2009-06-17 10:37:05 +02:00
|
|
|
// load statics now for DataObject classes
|
2011-02-21 06:20:14 +01:00
|
|
|
if(is_subclass_of($class, 'DataObject')) {
|
2011-12-22 03:29:16 +01:00
|
|
|
if(!is_subclass_of($extensionClass, 'DataExtension')) {
|
2011-04-15 11:35:30 +02:00
|
|
|
user_error("$extensionClass cannot be applied to $class without being a DataExtension", E_USER_ERROR);
|
2010-12-20 00:04:44 +01:00
|
|
|
}
|
2009-06-17 10:37:05 +02:00
|
|
|
}
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2009-07-13 05:56:26 +02:00
|
|
|
|
2011-12-22 03:21:44 +01:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* Remove an extension from a class.
|
2013-04-06 08:01:18 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* Keep in mind that this won't revert any datamodel additions
|
|
|
|
* of the extension at runtime, unless its used before the
|
|
|
|
* schema building kicks in (in your _config.php).
|
|
|
|
* Doesn't remove the extension from any {@link Object}
|
|
|
|
* instances which are already created, but will have an
|
|
|
|
* effect on new extensions.
|
|
|
|
* Clears any previously created singletons through {@link singleton()}
|
|
|
|
* to avoid side-effects from stale extension information.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @todo Add support for removing extensions with parameters
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @param string $extension Classname of an {@link Extension} subclass, without parameters
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
2012-10-09 23:30:40 +02:00
|
|
|
public static function remove_extension($extension) {
|
|
|
|
$class = get_called_class();
|
|
|
|
|
2011-12-22 03:29:16 +01:00
|
|
|
Config::inst()->remove($class, 'extensions', Config::anything(), $extension);
|
2013-04-06 08:01:18 +02:00
|
|
|
|
|
|
|
// remove any instances of the extension with parameters
|
|
|
|
$config = Config::inst()->get($class, 'extensions');
|
|
|
|
|
|
|
|
if($config) {
|
|
|
|
foreach($config as $k => $v) {
|
|
|
|
// extensions with parameters will be stored in config as
|
|
|
|
// ExtensionName("Param").
|
|
|
|
if(preg_match(sprintf("/^(%s)\(/", preg_quote($extension, '/')), $v)) {
|
|
|
|
Config::inst()->remove($class, 'extensions', Config::anything(), $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 04:39:40 +01:00
|
|
|
Config::inst()->extraConfigSourcesChanged($class);
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
// unset singletons to avoid side-effects
|
2012-06-11 12:40:47 +02:00
|
|
|
Injector::inst()->unregisterAllObjects();
|
2010-04-12 03:50:38 +02:00
|
|
|
|
|
|
|
// unset some caches
|
|
|
|
$subclasses = ClassInfo::subclassesFor($class);
|
|
|
|
$subclasses[] = $class;
|
|
|
|
if($subclasses) foreach($subclasses as $subclass) {
|
|
|
|
unset(self::$classes_constructed[$subclass]);
|
|
|
|
unset(self::$extra_methods[$subclass]);
|
|
|
|
}
|
2009-04-01 00:27:53 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @param bool $includeArgumentString Include the argument string in the return array,
|
|
|
|
* FALSE would return array("Versioned"), TRUE returns array("Versioned('Stage','Live')").
|
2011-04-15 11:35:30 +02:00
|
|
|
* @return array Numeric array of either {@link DataExtension} classnames,
|
2009-04-01 00:27:53 +02:00
|
|
|
* or eval'ed classname strings with constructor arguments.
|
|
|
|
*/
|
2012-04-12 02:02:13 +02:00
|
|
|
public static function get_extensions($class, $includeArgumentString = false) {
|
2011-12-22 03:29:16 +01:00
|
|
|
$extensions = Config::inst()->get($class, 'extensions');
|
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
if($includeArgumentString) {
|
|
|
|
return $extensions;
|
|
|
|
} else {
|
|
|
|
$extensionClassnames = array();
|
|
|
|
if($extensions) foreach($extensions as $extension) {
|
|
|
|
$extensionClassnames[] = Extension::get_classname_without_arguments($extension);
|
|
|
|
}
|
|
|
|
return $extensionClassnames;
|
|
|
|
}
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2012-08-22 23:29:13 +02:00
|
|
|
private static $unextendable_classes = array('Object', 'ViewableData', 'RequestHandler');
|
|
|
|
|
|
|
|
static public function get_extra_config_sources($class = null) {
|
|
|
|
if($class === null) $class = get_called_class();
|
|
|
|
|
|
|
|
// If this class is unextendable, NOP
|
|
|
|
if(in_array($class, self::$unextendable_classes)) return;
|
|
|
|
|
|
|
|
// Variable to hold sources in
|
|
|
|
$sources = null;
|
|
|
|
|
|
|
|
// Get a list of extensions
|
|
|
|
$extensions = Config::inst()->get($class, 'extensions', Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES);
|
|
|
|
|
|
|
|
if($extensions) {
|
|
|
|
// Build a list of all sources;
|
|
|
|
$sources = array();
|
|
|
|
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
list($extensionClass, $extensionArgs) = self::parse_class_spec($extension);
|
|
|
|
$sources[] = $extensionClass;
|
|
|
|
|
|
|
|
if(!ClassInfo::has_method_from($extensionClass, 'add_to_class', 'Extension')) {
|
2015-06-19 01:59:27 +02:00
|
|
|
Deprecation::notice('4.0',
|
2012-09-26 23:34:00 +02:00
|
|
|
"add_to_class deprecated on $extensionClass. Use get_extra_config instead");
|
2012-08-22 23:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
call_user_func(array($extensionClass, 'add_to_class'), $class, $extensionClass, $extensionArgs);
|
|
|
|
|
|
|
|
foreach(array_reverse(ClassInfo::ancestry($extensionClass)) as $extensionClassParent) {
|
|
|
|
if (ClassInfo::has_method_from($extensionClassParent, 'get_extra_config', $extensionClassParent)) {
|
|
|
|
$extras = $extensionClassParent::get_extra_config($class, $extensionClass, $extensionArgs);
|
|
|
|
if ($extras) $sources[] = $extras;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 04:39:40 +01:00
|
|
|
return $sources;
|
2012-08-22 23:29:13 +02:00
|
|
|
}
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->class = get_class($this);
|
2011-12-22 03:24:05 +01:00
|
|
|
|
2012-08-22 23:29:13 +02:00
|
|
|
foreach(ClassInfo::ancestry(get_called_class()) as $class) {
|
|
|
|
if(in_array($class, self::$unextendable_classes)) continue;
|
2012-09-26 23:34:00 +02:00
|
|
|
$extensions = Config::inst()->get($class, 'extensions',
|
|
|
|
Config::UNINHERITED | Config::EXCLUDE_EXTRA_SOURCES);
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2012-08-22 23:29:13 +02:00
|
|
|
if($extensions) foreach($extensions as $extension) {
|
|
|
|
$instance = self::create_from_string($extension);
|
|
|
|
$instance->setOwner(null, $class);
|
|
|
|
$this->extension_instances[$instance->class] = $instance;
|
2007-10-02 06:52:31 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-08-22 23:29:13 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(!isset(self::$classes_constructed[$this->class])) {
|
|
|
|
$this->defineMethods();
|
|
|
|
self::$classes_constructed[$this->class] = true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located
|
|
|
|
*
|
|
|
|
* You can add extra methods to a class using {@link Extensions}, {@link Object::createMethod()} or
|
|
|
|
* {@link Object::addWrapperMethod()}
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @param array $arguments
|
|
|
|
* @return mixed
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function __call($method, $arguments) {
|
2010-04-12 03:50:38 +02:00
|
|
|
// If the method cache was cleared by an an Object::add_extension() / Object::remove_extension()
|
|
|
|
// call, then we should rebuild it.
|
2011-04-25 13:11:09 +02:00
|
|
|
if(empty(self::$extra_methods[get_class($this)])) {
|
2010-04-12 03:50:38 +02:00
|
|
|
$this->defineMethods();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
$method = strtolower($method);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(isset(self::$extra_methods[$this->class][$method])) {
|
|
|
|
$config = self::$extra_methods[$this->class][$method];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
switch(true) {
|
|
|
|
case isset($config['property']) :
|
|
|
|
$obj = $config['index'] !== null ?
|
|
|
|
$this->{$config['property']}[$config['index']] :
|
|
|
|
$this->{$config['property']};
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-04 08:48:44 +02:00
|
|
|
if($obj) {
|
|
|
|
if(!empty($config['callSetOwnerFirst'])) $obj->setOwner($this);
|
|
|
|
$retVal = call_user_func_array(array($obj, $method), $arguments);
|
|
|
|
if(!empty($config['callSetOwnerFirst'])) $obj->clearOwner();
|
|
|
|
return $retVal;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if($this->destroyed) {
|
|
|
|
throw new Exception (
|
|
|
|
"Object->__call(): attempt to call $method on a destroyed $this->class object"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
throw new Exception (
|
2012-09-26 23:34:00 +02:00
|
|
|
"Object->__call(): $this->class cannot pass control to $config[property]($config[index])."
|
|
|
|
. ' Perhaps this object was mistakenly destroyed?'
|
2009-03-14 01:16:32 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
case isset($config['wrap']) :
|
|
|
|
array_unshift($arguments, $config['method']);
|
|
|
|
return call_user_func_array(array($this, $config['wrap']), $arguments);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
case isset($config['function']) :
|
|
|
|
return $config['function']($this, $arguments);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
default :
|
|
|
|
throw new Exception (
|
2012-09-26 23:34:00 +02:00
|
|
|
"Object->__call(): extra method $method is invalid on $this->class:"
|
|
|
|
. var_export($config, true)
|
2009-03-14 01:16:32 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-30 01:21:59 +02:00
|
|
|
// Please do not change the exception code number below.
|
2013-05-24 21:35:51 +02:00
|
|
|
$class = get_class($this);
|
|
|
|
throw new Exception("Object->__call(): the method '$method' does not exist on '$class'", 2175);
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Return TRUE if a method exists on this object
|
|
|
|
*
|
|
|
|
* This should be used rather than PHP's inbuild method_exists() as it takes into account methods added via
|
|
|
|
* extensions
|
|
|
|
*
|
|
|
|
* @param string $method
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function hasMethod($method) {
|
|
|
|
return method_exists($this, $method) || isset(self::$extra_methods[$this->class][strtolower($method)]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Return the names of all the methods available on this object
|
|
|
|
*
|
|
|
|
* @param bool $custom include methods added dynamically at runtime
|
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function allMethodNames($custom = false) {
|
2009-08-11 08:13:33 +02:00
|
|
|
if(!isset(self::$built_in_methods[$this->class])) {
|
|
|
|
self::$built_in_methods[$this->class] = array_map('strtolower', get_class_methods($this));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if($custom && isset(self::$extra_methods[$this->class])) {
|
|
|
|
return array_merge(self::$built_in_methods[$this->class], array_keys(self::$extra_methods[$this->class]));
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-03-14 01:16:32 +01:00
|
|
|
return self::$built_in_methods[$this->class];
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 00:27:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds any methods from {@link Extension} instances attached to this object.
|
|
|
|
* All these methods can then be called directly on the instance (transparently
|
|
|
|
* mapped through {@link __call()}), or called explicitly through {@link extend()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @uses addMethodsFrom()
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected function defineMethods() {
|
2009-03-14 01:16:32 +01:00
|
|
|
if($this->extension_instances) foreach(array_keys($this->extension_instances) as $key) {
|
|
|
|
$this->addMethodsFrom('extension_instances', $key);
|
2007-08-16 08:37:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(isset($_REQUEST['debugmethods']) && isset(self::$built_in_methods[$this->class])) {
|
2007-11-08 00:46:00 +01:00
|
|
|
Debug::require_developer_login();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
echo '<h2>Methods defined on ' . $this->class . '</h2><ul>';
|
|
|
|
foreach(self::$built_in_methods[$this->class] as $method) {
|
|
|
|
echo "<li>$method</li>";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-03-14 01:16:32 +01:00
|
|
|
echo '</ul>';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* Add all the methods from an object property (which is an {@link Extension}) to this object.
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
|
|
|
* @param string $property the property name
|
|
|
|
* @param string|int $index an index to use if the property is an array
|
|
|
|
*/
|
|
|
|
protected function addMethodsFrom($property, $index = null) {
|
|
|
|
$extension = ($index !== null) ? $this->{$property}[$index] : $this->$property;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(!$extension) {
|
|
|
|
throw new InvalidArgumentException (
|
|
|
|
"Object->addMethodsFrom(): could not add methods from {$this->class}->{$property}[$index]"
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(method_exists($extension, 'allMethodNames')) {
|
2009-08-11 10:35:14 +02:00
|
|
|
$methods = $extension->allMethodNames(true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(!isset(self::$built_in_methods[$extension->class])) {
|
|
|
|
self::$built_in_methods[$extension->class] = array_map('strtolower', get_class_methods($extension));
|
|
|
|
}
|
|
|
|
$methods = self::$built_in_methods[$extension->class];
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 10:35:14 +02:00
|
|
|
if($methods) {
|
2009-08-11 08:13:33 +02:00
|
|
|
$methodInfo = array(
|
|
|
|
'property' => $property,
|
|
|
|
'index' => $index,
|
|
|
|
'callSetOwnerFirst' => $extension instanceof Extension,
|
|
|
|
);
|
|
|
|
|
2009-08-11 10:35:14 +02:00
|
|
|
$newMethods = array_fill_keys($methods, $methodInfo);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 08:13:33 +02:00
|
|
|
if(isset(self::$extra_methods[$this->class])) {
|
|
|
|
self::$extra_methods[$this->class] =
|
|
|
|
array_merge(self::$extra_methods[$this->class], $newMethods);
|
|
|
|
} else {
|
|
|
|
self::$extra_methods[$this->class] = $newMethods;
|
2009-03-14 01:16:32 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* Add a wrapper method - a method which points to another method with a different name. For example, Thumbnail(x)
|
|
|
|
* can be wrapped to generateThumbnail(x)
|
|
|
|
*
|
|
|
|
* @param string $method the method name to wrap
|
|
|
|
* @param string $wrap the method name to wrap to
|
|
|
|
*/
|
|
|
|
protected function addWrapperMethod($method, $wrap) {
|
|
|
|
self::$extra_methods[$this->class][strtolower($method)] = array (
|
|
|
|
'wrap' => $wrap,
|
|
|
|
'method' => $method
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Add an extra method using raw PHP code passed as a string
|
|
|
|
*
|
|
|
|
* @param string $method the method name
|
|
|
|
* @param string $code the PHP code - arguments will be in an array called $args, while you can access this object
|
2012-09-26 23:34:00 +02:00
|
|
|
* by using $obj. Note that you cannot call protected methods, as the method is actually an external
|
|
|
|
* function
|
2009-03-14 01:16:32 +01:00
|
|
|
*/
|
|
|
|
protected function createMethod($method, $code) {
|
|
|
|
self::$extra_methods[$this->class][strtolower($method)] = array (
|
|
|
|
'function' => create_function('$obj, $args', $code)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* @see Object::get_static()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function stat($name, $uncached = false) {
|
2012-04-18 13:10:57 +02:00
|
|
|
return Config::inst()->get(($this->class ? $this->class : get_class($this)), $name, Config::FIRST_SET);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* @see Object::set_static()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function set_stat($name, $value) {
|
2012-04-18 13:10:57 +02:00
|
|
|
Config::inst()->update(($this->class ? $this->class : get_class($this)), $name, $value);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* @see Object::uninherited_static()
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function uninherited($name) {
|
2012-04-18 13:10:57 +02:00
|
|
|
return Config::inst()->get(($this->class ? $this->class : get_class($this)), $name, Config::UNINHERITED);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-11-16 02:33:37 +01:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Return true if this object "exists" i.e. has a sensible value
|
2008-11-02 01:12:23 +01:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* This method should be overriden in subclasses to provide more context about the classes state. For example, a
|
|
|
|
* {@link DataObject} class could return false when it is deleted from the database
|
|
|
|
*
|
|
|
|
* @return bool
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
|
|
|
public function exists() {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* @return string this classes parent class
|
|
|
|
*/
|
|
|
|
public function parentClass() {
|
2008-11-02 01:12:23 +01:00
|
|
|
return get_parent_class($this);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 01:12:23 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Check if this class is an instance of a specific class, or has that class as one of its parents
|
|
|
|
*
|
2008-11-02 01:12:23 +01:00
|
|
|
* @param string $class
|
2009-03-14 01:16:32 +01:00
|
|
|
* @return bool
|
2008-11-02 01:12:23 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function is_a($class) {
|
|
|
|
return $this instanceof $class;
|
2008-11-02 01:12:23 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
/**
|
|
|
|
* @return string the class name
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __toString() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-06 05:43:48 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Calls a method if available on both this object and all applied {@link Extensions}, and then attempts to merge
|
|
|
|
* all results into an array
|
2008-08-06 05:43:48 +02:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $method the method name to call
|
|
|
|
* @param mixed $argument a single argument to pass
|
|
|
|
* @return mixed
|
|
|
|
* @todo integrate inheritance rules
|
2008-08-06 05:43:48 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function invokeWithExtensions($method, $argument = null) {
|
|
|
|
$result = method_exists($this, $method) ? array($this->$method($argument)) : array();
|
|
|
|
$extras = $this->extend($method, $argument);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
return $extras ? array_merge($result, $extras) : $result;
|
2008-08-06 05:43:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-16 08:37:49 +02:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Run the given function on all of this object's extensions. Note that this method originally returned void, so if
|
|
|
|
* you wanted to return results, you're hosed
|
|
|
|
*
|
|
|
|
* Currently returns an array, with an index resulting every time the function is called. Only adds returns if
|
2011-04-15 11:35:30 +02:00
|
|
|
* they're not NULL, to avoid bogus results from methods just defined on the parent extension. This is important for
|
2009-03-14 01:16:32 +01:00
|
|
|
* permission-checks through extend, as they use min() to determine if any of the returns is FALSE. As min() doesn't
|
2009-04-01 00:27:53 +02:00
|
|
|
* do type checking, an included NULL return would fail the permission checks.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* The extension methods are defined during {@link __construct()} in {@link defineMethods()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $method the name of the method to call on each extension
|
|
|
|
* @param mixed $a1,... up to 7 arguments to be passed to the method
|
|
|
|
* @return array
|
2007-08-16 08:37:49 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function extend($method, &$a1=null, &$a2=null, &$a3=null, &$a4=null, &$a5=null, &$a6=null, &$a7=null) {
|
|
|
|
$values = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
if(!empty($this->beforeExtendCallbacks[$method])) {
|
|
|
|
foreach(array_reverse($this->beforeExtendCallbacks[$method]) as $callback) {
|
2015-12-13 22:10:45 +01:00
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7));
|
2013-05-16 00:34:45 +02:00
|
|
|
if($value !== null) $values[] = $value;
|
|
|
|
}
|
|
|
|
$this->beforeExtendCallbacks[$method] = array();
|
|
|
|
}
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if($this->extension_instances) foreach($this->extension_instances as $instance) {
|
2009-08-11 10:35:14 +02:00
|
|
|
if(method_exists($instance, $method)) {
|
2009-06-04 08:48:44 +02:00
|
|
|
$instance->setOwner($this);
|
2009-03-14 01:16:32 +01:00
|
|
|
$value = $instance->$method($a1, $a2, $a3, $a4, $a5, $a6, $a7);
|
|
|
|
if($value !== null) $values[] = $value;
|
2009-06-04 08:48:44 +02:00
|
|
|
$instance->clearOwner();
|
2007-08-16 08:37:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-16 00:34:45 +02:00
|
|
|
if(!empty($this->afterExtendCallbacks[$method])) {
|
|
|
|
foreach(array_reverse($this->afterExtendCallbacks[$method]) as $callback) {
|
2015-12-13 22:10:45 +01:00
|
|
|
$value = call_user_func_array($callback, array(&$a1, &$a2, &$a3, &$a4, &$a5, &$a6, &$a7));
|
2013-05-16 00:34:45 +02:00
|
|
|
if($value !== null) $values[] = $value;
|
|
|
|
}
|
|
|
|
$this->afterExtendCallbacks[$method] = array();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
return $values;
|
2007-08-16 08:37:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-16 08:37:49 +02:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* Get an extension instance attached to this object by name.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @uses hasExtension()
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
|
|
|
* @param string $extension
|
|
|
|
* @return Extension
|
2007-08-16 08:37:49 +02:00
|
|
|
*/
|
2009-04-01 00:27:53 +02:00
|
|
|
public function getExtensionInstance($extension) {
|
2009-03-14 01:16:32 +01:00
|
|
|
if($this->hasExtension($extension)) return $this->extension_instances[$extension];
|
2007-08-16 08:37:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-08-16 08:37:49 +02:00
|
|
|
/**
|
2009-04-01 00:27:53 +02:00
|
|
|
* Returns TRUE if this object instance has a specific extension applied
|
|
|
|
* in {@link $extension_instances}. Extension instances are initialized
|
|
|
|
* at constructor time, meaning if you use {@link add_extension()}
|
|
|
|
* afterwards, the added extension will just be added to new instances
|
2011-04-15 11:35:30 +02:00
|
|
|
* of the extended class. Use the static method {@link has_extension()}
|
2009-04-01 00:27:53 +02:00
|
|
|
* to check if a class (not an instance) has a specific extension.
|
|
|
|
* Caution: Don't use singleton(<class>)->hasExtension() as it will
|
|
|
|
* give you inconsistent results based on when the singleton was first
|
|
|
|
* accessed.
|
2009-03-14 01:16:32 +01:00
|
|
|
*
|
2009-04-01 00:27:53 +02:00
|
|
|
* @param string $extension Classname of an {@link Extension} subclass without parameters
|
2009-03-14 01:16:32 +01:00
|
|
|
* @return bool
|
2007-08-16 08:37:49 +02:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function hasExtension($extension) {
|
|
|
|
return isset($this->extension_instances[$extension]);
|
2008-11-21 02:06:21 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-01 00:27:53 +02:00
|
|
|
/**
|
|
|
|
* Get all extension instances for this specific object instance.
|
|
|
|
* See {@link get_extensions()} to get all applied extension classes
|
|
|
|
* for this class (not the instance).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-04-15 11:35:30 +02:00
|
|
|
* @return array Map of {@link DataExtension} instances, keyed by classname.
|
2009-04-01 00:27:53 +02:00
|
|
|
*/
|
|
|
|
public function getExtensionInstances() {
|
|
|
|
return $this->extension_instances;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
// --------------------------------------------------------------------------------------------------------------
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Cache the results of an instance method in this object to a file, or if it is already cache return the cached
|
|
|
|
* results
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $method the method name to cache
|
|
|
|
* @param int $lifetime the cache lifetime in seconds
|
|
|
|
* @param string $ID custom cache ID to use
|
|
|
|
* @param array $arguments an optional array of arguments
|
|
|
|
* @return mixed the cached data
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
public function cacheToFile($method, $lifetime = 3600, $ID = false, $arguments = array()) {
|
2015-07-27 13:12:26 +02:00
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.');
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(!$this->hasMethod($method)) {
|
|
|
|
throw new InvalidArgumentException("Object->cacheToFile(): the method $method does not exist to cache");
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
$cacheName = $this->class . '_' . $method;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(!is_array($arguments)) $arguments = array($arguments);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if($ID) $cacheName .= '_' . $ID;
|
2013-10-17 18:36:07 +02:00
|
|
|
if(count($arguments)) $cacheName .= '_' . md5(serialize($arguments));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-24 19:01:20 +02:00
|
|
|
$data = $this->loadCache($cacheName, $lifetime);
|
|
|
|
|
|
|
|
if($data !== false) {
|
2009-03-14 01:16:32 +01:00
|
|
|
return $data;
|
|
|
|
}
|
2013-10-24 19:01:20 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
$data = call_user_func_array(array($this, $method), $arguments);
|
|
|
|
$this->saveCache($cacheName, $data);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
return $data;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-15 02:09:59 +01:00
|
|
|
/**
|
|
|
|
* Clears the cache for the given cacheToFile call
|
|
|
|
*/
|
|
|
|
public function clearCache($method, $ID = false, $arguments = array()) {
|
2015-07-27 13:12:26 +02:00
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.');
|
|
|
|
|
2009-03-15 02:09:59 +01:00
|
|
|
$cacheName = $this->class . '_' . $method;
|
|
|
|
if(!is_array($arguments)) $arguments = array($arguments);
|
|
|
|
if($ID) $cacheName .= '_' . $ID;
|
2013-10-17 18:36:07 +02:00
|
|
|
if(count($arguments)) $cacheName .= '_' . md5(serialize($arguments));
|
2009-03-15 02:09:59 +01:00
|
|
|
|
2009-03-16 05:31:53 +01:00
|
|
|
$file = TEMP_FOLDER . '/' . $this->sanitiseCachename($cacheName);
|
|
|
|
if(file_exists($file)) unlink($file);
|
2009-03-15 02:09:59 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Loads a cache from the filesystem if a valid on is present and within the specified lifetime
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $cache the cache name
|
|
|
|
* @param int $lifetime the lifetime (in seconds) of the cache before it is invalid
|
|
|
|
* @return mixed
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
protected function loadCache($cache, $lifetime = 3600) {
|
2015-07-27 13:12:26 +02:00
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.');
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
$path = TEMP_FOLDER . '/' . $this->sanitiseCachename($cache);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
if(!isset($_REQUEST['flush']) && file_exists($path) && (filemtime($path) + $lifetime) > time()) {
|
|
|
|
return unserialize(file_get_contents($path));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
return false;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Save a piece of cached data to the file system
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $cache the cache name
|
|
|
|
* @param mixed $data data to save (must be serializable)
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
protected function saveCache($cache, $data) {
|
2015-07-27 13:12:26 +02:00
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.');
|
2009-03-14 01:16:32 +01:00
|
|
|
file_put_contents(TEMP_FOLDER . '/' . $this->sanitiseCachename($cache), serialize($data));
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2009-03-14 01:16:32 +01:00
|
|
|
* Strip a file name of special characters so it is suitable for use as a cache file name
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2009-03-14 01:16:32 +01:00
|
|
|
* @param string $name
|
|
|
|
* @return string the name with all special cahracters replaced with underscores
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2009-03-14 01:16:32 +01:00
|
|
|
protected function sanitiseCachename($name) {
|
2015-07-27 13:12:26 +02:00
|
|
|
Deprecation::notice('4.0', 'Caching methods on Object have been deprecated. Use the SS_Cache API instead.');
|
2009-03-14 01:16:32 +01:00
|
|
|
return str_replace(array('~', '.', '/', '!', ' ', "\n", "\r", "\t", '\\', ':', '"', '\'', ';'), '_', $name);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|