2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-01-27 01:46:43 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Core;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use Exception;
|
|
|
|
use ReflectionClass;
|
2016-01-27 01:46:43 +01:00
|
|
|
|
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.
|
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')"
|
|
|
|
if(strpos($classSpec,'(') === false) {
|
2016-01-27 01:46:43 +01:00
|
|
|
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
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
self::$_cache_inst_args[$classSpec.$firstArg] = call_user_func_array(array('SilverStripe\\Core\\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();
|
2015-04-21 16:00:18 +02:00
|
|
|
$hadNamespace = false;
|
|
|
|
$currentKey = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:46:41 +02:00
|
|
|
foreach($tokens as $token) {
|
2015-04-21 16:00:18 +02:00
|
|
|
// $forceResult used to allow null result to be detected
|
|
|
|
$result = $forceResult = null;
|
|
|
|
$tokenName = is_array($token) ? $token[0] : $token;
|
|
|
|
|
|
|
|
// Get the class name
|
|
|
|
if($class === null && is_array($token) && $token[0] === T_STRING) {
|
2010-10-04 06:46:41 +02:00
|
|
|
$class = $token[1];
|
2015-04-21 16:00:18 +02:00
|
|
|
} elseif(is_array($token) && $token[0] === T_NS_SEPARATOR) {
|
2012-12-18 03:00:45 +01:00
|
|
|
$class .= $token[1];
|
2015-04-21 16:00:18 +02:00
|
|
|
$hadNamespace = true;
|
|
|
|
} elseif($hadNamespace && is_array($token) && $token[0] === T_STRING) {
|
2012-12-18 03:00:45 +01:00
|
|
|
$class .= $token[1];
|
2015-04-21 16:00:18 +02:00
|
|
|
$hadNamespace = 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 '"':
|
2015-04-21 16:00:18 +02:00
|
|
|
$result = stripcslashes(substr($argString,1,-1));
|
2012-09-26 23:34:00 +02:00
|
|
|
break;
|
|
|
|
case "'":
|
2015-04-21 16:00:18 +02:00
|
|
|
$result = str_replace(array("\\\\", "\\'"),array("\\", "'"), substr($argString,1,-1));
|
2012-09-26 23:34:00 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString");
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
2016-06-29 12:40:27 +02:00
|
|
|
|
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:
|
2015-04-21 16:00:18 +02:00
|
|
|
$result = (double)$token[1];
|
2010-10-04 06:46:41 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LNUMBER:
|
2015-04-21 16:00:18 +02:00
|
|
|
$result = (int)$token[1];
|
|
|
|
break;
|
2013-03-22 04:34:17 +01:00
|
|
|
|
2015-04-21 16:00:18 +02:00
|
|
|
case T_DOUBLE_ARROW:
|
|
|
|
// We've encountered an associative array (the array itself has already been
|
|
|
|
// added to the bucket), so the previous item added to the bucket is the key
|
|
|
|
end($bucket);
|
|
|
|
$currentKey = current($bucket);
|
|
|
|
array_pop($bucket);
|
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]) {
|
2015-04-21 16:00:18 +02:00
|
|
|
case 'true': $result = true; break;
|
|
|
|
case 'false': $result = false; break;
|
|
|
|
case 'null': $result = null; $forceResult = true; break;
|
2010-10-04 06:46:41 +02:00
|
|
|
default: throw new Exception("Bad T_STRING arg '{$token[1]}'");
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2013-03-22 04:34:17 +01:00
|
|
|
break;
|
|
|
|
|
2010-10-04 06:47:08 +02:00
|
|
|
case T_ARRAY:
|
2015-04-21 16:00:18 +02:00
|
|
|
$result = array();
|
|
|
|
break;
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
2010-10-04 06:47:08 +02:00
|
|
|
} else {
|
2015-04-21 16:00:18 +02:00
|
|
|
if($tokenName === '[') {
|
|
|
|
$result = array();
|
|
|
|
} elseif(($tokenName === ')' || $tokenName === ']') && ! empty($bucketStack)) {
|
|
|
|
// Store the bucket we're currently working on
|
|
|
|
$oldBucket = $bucket;
|
|
|
|
// Fetch the key for the bucket at the top of the stack
|
|
|
|
end($bucketStack);
|
|
|
|
$key = key($bucketStack);
|
|
|
|
reset($bucketStack);
|
|
|
|
// Re-instate the bucket from the top of the stack
|
|
|
|
$bucket = &$bucketStack[$key];
|
|
|
|
// Add our saved, "nested" bucket to the bucket we just popped off the stack
|
|
|
|
$bucket[$key] = $oldBucket;
|
|
|
|
// Remove the bucket we just popped off the stack
|
2010-10-04 06:47:08 +02:00
|
|
|
array_pop($bucketStack);
|
|
|
|
}
|
2010-10-04 06:46:41 +02:00
|
|
|
}
|
2015-04-21 16:00:18 +02:00
|
|
|
|
|
|
|
// If we've got something to add to the bucket, add it
|
|
|
|
if($result !== null || $forceResult) {
|
|
|
|
if($currentKey) {
|
|
|
|
$bucket[$currentKey] = $result;
|
|
|
|
$currentKey = null;
|
|
|
|
} else {
|
|
|
|
$bucket[] = $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've just pushed an array, that becomes our new bucket
|
|
|
|
if($result === array()) {
|
|
|
|
// Fetch the key that the array was pushed to
|
|
|
|
end($bucket);
|
|
|
|
$key = key($bucket);
|
|
|
|
reset($bucket);
|
|
|
|
// Store reference to "old" bucket in the stack
|
|
|
|
$bucketStack[$key] = &$bucket;
|
|
|
|
// Set the active bucket to be our newly-pushed, empty array
|
|
|
|
$bucket = &$bucket[$key];
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $class The class to get the static from
|
|
|
|
* @param string $name The property to get from the class
|
|
|
|
* @param mixed $default The value to return if property doesn't exist on class
|
|
|
|
* @return mixed 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) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if (is_subclass_of($class, 'SilverStripe\\Core\\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
|
|
|
}
|