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 {
|
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}.
|
|
|
|
*/
|
|
|
|
public static $allowed_actions = null;
|
|
|
|
|
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;
|
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;
|
2009-06-04 08:48:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference counter to ensure that the owner isn't cleared until clearOwner() has
|
|
|
|
* been called as many times as setOwner()
|
|
|
|
*/
|
|
|
|
private $ownerRefs = 0;
|
2009-08-11 10:35:14 +02:00
|
|
|
|
|
|
|
public $class;
|
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* TODO: This is likely to be replaced by event sytem before 3.0 final, so be aware
|
|
|
|
* this API is fairly unstable.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param $class
|
|
|
|
*/
|
2012-03-12 04:14:47 +01:00
|
|
|
static function add_to_class($class, $extensionClass, $args) {
|
2011-12-22 03:29:16 +01:00
|
|
|
Config::add_static_source($class, $extensionClass);
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
/**
|
2011-04-15 11:35:30 +02:00
|
|
|
* Set the owner of this extension.
|
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
|
|
|
*/
|
2009-06-04 08:48:44 +02:00
|
|
|
function setOwner($owner, $ownerBaseClass = null) {
|
|
|
|
if($owner) $this->ownerRefs++;
|
2007-08-16 08:32:49 +02:00
|
|
|
$this->owner = $owner;
|
2009-06-04 08:48:44 +02:00
|
|
|
|
|
|
|
if($ownerBaseClass) $this->ownerBaseClass = $ownerBaseClass;
|
|
|
|
else if(!$this->ownerBaseClass && $owner) $this->ownerBaseClass = $owner->class;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearOwner() {
|
|
|
|
if($this->ownerRefs <= 0) user_error("clearOwner() called more than setOwner()", E_USER_WARNING);
|
|
|
|
$this->ownerRefs--;
|
|
|
|
if($this->ownerRefs == 0) $this->owner = null;
|
2007-08-16 08:32:49 +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;
|
|
|
|
}
|
2009-03-31 21:32:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to strip eval'ed arguments from a string
|
|
|
|
* thats passed to {@link DataObject::$extensions} or
|
|
|
|
* {@link Object::add_extension()}.
|
|
|
|
*
|
|
|
|
* @param string $extensionStr E.g. "Versioned('Stage','Live')"
|
|
|
|
* @return string Extension classname, e.g. "Versioned"
|
|
|
|
*/
|
|
|
|
public static function get_classname_without_arguments($extensionStr) {
|
|
|
|
return (($p = strpos($extensionStr, '(')) !== false) ? substr($extensionStr, 0, $p) : $extensionStr;
|
|
|
|
}
|
2011-12-22 03:29:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-08-16 08:32:49 +02:00
|
|
|
}
|
|
|
|
|