2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-01-27 01:46:43 +01:00
|
|
|
|
|
|
|
use SilverStripe\Framework\Core\Configurable;
|
|
|
|
use SilverStripe\Framework\Core\Extensible;
|
|
|
|
use SilverStripe\Framework\Core\Injectable;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
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 {
|
2016-01-27 01:46:43 +01:00
|
|
|
use Extensible;
|
|
|
|
use Injectable;
|
|
|
|
use Configurable;
|
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
|
|
|
|
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
|
|
|
*
|
2016-01-27 01:46:43 +01:00
|
|
|
* It is designed for simple, cloneable objects. The first time this method is called for a given
|
2010-10-04 06:46:41 +02:00
|
|
|
* 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
|
2016-01-27 01:46:43 +01:00
|
|
|
* `Varchar::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.
|
2016-01-27 01:46:43 +01:00
|
|
|
*
|
|
|
|
* @param string $classSpec
|
|
|
|
* @param mixed $firstArg
|
|
|
|
* @return object
|
2010-10-04 06:46:41 +02:00
|
|
|
*/
|
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')"
|
2016-01-27 01:46:43 +01:00
|
|
|
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
|
|
|
|
2016-01-27 01:46:43 +01:00
|
|
|
if($firstArg !== null) {
|
|
|
|
array_unshift($args, $firstArg);
|
|
|
|
}
|
2010-10-04 06:46:41 +02:00
|
|
|
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().
|
2016-01-27 01:46:43 +01:00
|
|
|
* Returns a 2-element array, with classname and arguments
|
|
|
|
*
|
|
|
|
* @param string $classSpec
|
|
|
|
* @return array
|
|
|
|
* @throws Exception
|
2010-10-04 06:46:41 +02:00
|
|
|
*/
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-14 01:16:32 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->class = get_class($this);
|
2016-01-27 01:46:43 +01:00
|
|
|
$this->constructExtensions();
|
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
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|