2007-08-16 08:32:49 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-31 21:32:51 +02:00
|
|
|
* Add extension that can be added to an object with {@link Object::add_extension()}.
|
2011-04-15 11:35:30 +02:00
|
|
|
* For {@link DataObject} extensions, use {@link DataExtension}.
|
2009-03-31 21:32:51 +02:00
|
|
|
* Each extension instance has an "owner" instance, accessible through
|
|
|
|
* {@link getOwner()}.
|
|
|
|
* Every object instance gets its own set of extension instances,
|
|
|
|
* meaning you can set parameters specific to the "owner instance"
|
|
|
|
* in new Extension instances.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage core
|
2007-08-16 08:32:49 +02:00
|
|
|
*/
|
2009-08-11 10:35:14 +02:00
|
|
|
abstract class Extension {
|
2016-03-22 02:39:25 +01:00
|
|
|
|
2008-03-11 02:30:19 +01:00
|
|
|
/**
|
|
|
|
* This is used by extensions designed to be applied to controllers.
|
|
|
|
* It works the same way as {@link Controller::$allowed_actions}.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = null;
|
2008-03-11 02:30:19 +01:00
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* The object this extension is applied to.
|
|
|
|
*
|
|
|
|
* @var Object
|
2007-08-16 08:32:49 +02:00
|
|
|
*/
|
|
|
|
protected $owner;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-26 05:46:04 +02:00
|
|
|
/**
|
|
|
|
* The base class that this extension was applied to; $this->owner must be one of these
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
|
|
|
protected $ownerBaseClass;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-04 08:48:44 +02:00
|
|
|
/**
|
2016-03-22 02:39:25 +01:00
|
|
|
* Ownership stack for recursive methods.
|
|
|
|
* Last item is current owner.
|
|
|
|
*
|
|
|
|
* @var array
|
2009-06-04 08:48:44 +02:00
|
|
|
*/
|
2016-03-22 02:39:25 +01:00
|
|
|
private $ownerStack = [];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-11 10:35:14 +02:00
|
|
|
public $class;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct() {
|
2009-08-11 10:35:14 +02:00
|
|
|
$this->class = get_class($this);
|
|
|
|
}
|
2007-08-16 08:32:49 +02:00
|
|
|
|
2011-12-22 03:29:16 +01:00
|
|
|
/**
|
|
|
|
* Called when this extension is added to a particular class
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $class
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function add_to_class($class, $extensionClass, $args = null) {
|
2012-08-22 23:29:13 +02:00
|
|
|
// NOP
|
2011-12-22 03:29:16 +01:00
|
|
|
}
|
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Set the owner of this extension.
|
2016-03-22 02:39:25 +01:00
|
|
|
*
|
2009-05-26 05:46:04 +02:00
|
|
|
* @param Object $owner The owner object,
|
|
|
|
* @param string $ownerBaseClass The base class that the extension is applied to; this may be
|
|
|
|
* the class of owner, or it may be a parent. For example, if Versioned was applied to SiteTree,
|
|
|
|
* and then a Page object was instantiated, $owner would be a Page object, but $ownerBaseClass
|
|
|
|
* would be 'SiteTree'.
|
2007-08-16 08:32:49 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setOwner($owner, $ownerBaseClass = null) {
|
2016-03-22 02:39:25 +01:00
|
|
|
if($owner) {
|
|
|
|
$this->ownerStack[] = $owner;
|
|
|
|
}
|
2007-08-16 08:32:49 +02:00
|
|
|
$this->owner = $owner;
|
2009-06-04 08:48:44 +02:00
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
// Set ownerBaseClass
|
|
|
|
if($ownerBaseClass) {
|
|
|
|
$this->ownerBaseClass = $ownerBaseClass;
|
|
|
|
} elseif(!$this->ownerBaseClass && $owner) {
|
|
|
|
$this->ownerBaseClass = get_class($owner);
|
|
|
|
}
|
2009-06-04 08:48:44 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-22 02:39:25 +01:00
|
|
|
/**
|
|
|
|
* Clear the current owner, and restore extension to the state prior to the last setOwner()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clearOwner() {
|
2016-03-22 02:39:25 +01:00
|
|
|
if(empty($this->ownerStack)) {
|
|
|
|
throw new BadMethodCallException("clearOwner() called more than setOwner()");
|
|
|
|
}
|
|
|
|
array_pop($this->ownerStack);
|
|
|
|
if($this->ownerStack) {
|
|
|
|
$this->owner = end($this->ownerStack);
|
|
|
|
} else {
|
|
|
|
$this->owner = null;
|
|
|
|
}
|
2007-08-16 08:32:49 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Returns the owner of this extension.
|
2009-01-10 12:35:50 +01:00
|
|
|
*
|
|
|
|
* @return Object
|
|
|
|
*/
|
|
|
|
public function getOwner() {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-03-31 21:32:51 +02:00
|
|
|
/**
|
|
|
|
* Helper method to strip eval'ed arguments from a string
|
2015-12-09 22:19:23 +01:00
|
|
|
* that's passed to {@link DataObject::$extensions} or
|
2009-03-31 21:32:51 +02:00
|
|
|
* {@link Object::add_extension()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-03-31 21:32:51 +02:00
|
|
|
* @param string $extensionStr E.g. "Versioned('Stage','Live')"
|
|
|
|
* @return string Extension classname, e.g. "Versioned"
|
|
|
|
*/
|
|
|
|
public static function get_classname_without_arguments($extensionStr) {
|
2015-12-09 22:19:23 +01:00
|
|
|
$parts = explode('(', $extensionStr);
|
|
|
|
return $parts[0];
|
2009-03-31 21:32:51 +02:00
|
|
|
}
|
2011-12-22 03:29:16 +01:00
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
}
|