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}.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Reference counter to ensure that the owner isn't cleared until clearOwner() has
|
|
|
|
* been called as many times as setOwner()
|
|
|
|
*/
|
|
|
|
private $ownerRefs = 0;
|
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.
|
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) {
|
2009-06-04 08:48:44 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clearOwner() {
|
2009-06-04 08:48:44 +02:00
|
|
|
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
|
|
|
}
|
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
|
2014-08-15 08:53:05 +02:00
|
|
|
* thats 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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|