class RulesExtendable

Base class for RulesExtendables.

The RulesExtendable uses the rules cache to setup the defined extenders and overrides automatically. As soon faces is used the faces information is autoloaded using setUp().

Hierarchy

Expanded class hierarchy of RulesExtendable

File

includes/rules.core.inc, line 323

View source
abstract class RulesExtendable extends FacesExtendable {
    
    /**
     * The name of the info definitions associated with info about this class.
     *
     * This would be defined abstract, if possible. Common rules hooks with class
     * info are e.g. plugin_info and data_info.
     */
    protected $hook;
    
    /**
     * The name of the item this class represents in the info hook.
     *
     * @var string
     */
    protected $itemName;
    protected $cache;
    
    /**
     * @var array
     */
    protected $itemInfo = array();
    public function __construct() {
        $this->setUp();
    }
    protected function setUp() {
        // Keep a reference on the cache, so elements created during cache
        // rebuilding end up with a complete cache in the end too.
        $this->cache =& rules_get_cache();
        if (isset($this->cache[$this->hook][$this->itemName])) {
            $this->itemInfo =& $this->cache[$this->hook][$this->itemName];
        }
        // Set up the Faces Extenders.
        if (!empty($this->itemInfo['faces_cache'])) {
            list($this->facesMethods, $this->facesIncludes, $this->faces) = $this->itemInfo['faces_cache'];
        }
    }
    
    /**
     * Forces the object to be setUp, this executes setUp() if not done yet.
     */
    public function forceSetUp() {
        if (!isset($this->cache) || !empty($this->itemInfo['faces_cache']) && !$this->faces) {
            $this->setUp();
        }
    }
    
    /**
     * Magic method: Invoke the dynamically implemented methods.
     */
    public function __call($name, $arguments = array()) {
        $this->forceSetUp();
        return parent::__call($name, $arguments);
    }
    public function facesAs($interface = NULL) {
        $this->forceSetUp();
        return parent::facesAs($interface);
    }
    
    /**
     * Allows items to add something to the rules cache.
     */
    public function rebuildCache(&$itemInfo, &$cache) {
        // Speed up setting up items by caching the faces methods.
        if (!empty($itemInfo['extenders'])) {
            // Apply extenders and overrides.
            $itemInfo += array(
                'overrides' => array(),
            );
            foreach ($itemInfo['extenders'] as $face => $data) {
                $data += array(
                    'file' => array(),
                );
                if (isset($data['class'])) {
                    $this->extendByClass($face, $data['class'], $data['file']);
                }
                elseif (isset($data['methods'])) {
                    $this->extend($face, $data['methods'], $data['file']);
                }
            }
            foreach ($itemInfo['overrides'] as $data) {
                $data += array(
                    'file' => array(),
                );
                $this->override($data['methods'], $data['file']);
            }
            $itemInfo['faces_cache'] = array(
                $this->facesMethods,
                $this->facesIncludes,
                $this->faces,
            );
            // We don't need that any more.
            unset($itemInfo['extenders'], $itemInfo['overrides']);
        }
    }
    
    /**
     * Returns whether the a RuleExtendable supports the given interface.
     *
     * @param $itemInfo
     *   The info about the item as specified in the hook.
     * @param $interface
     *   The interface to check for.
     *
     * @return bool
     *   Whether it supports the given interface.
     */
    public static function itemFacesAs(&$itemInfo, $interface) {
        return in_array($interface, class_implements($itemInfo['class'])) || isset($itemInfo['faces_cache'][2][$interface]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
RulesExtendable::$cache protected property 1
RulesExtendable::$hook protected property The name of the info definitions associated with info about this class. 1
RulesExtendable::$itemInfo protected property
RulesExtendable::$itemName protected property The name of the item this class represents in the info hook. 9
RulesExtendable::facesAs public function
RulesExtendable::forceSetUp public function Forces the object to be setUp, this executes setUp() if not done yet. 1
RulesExtendable::itemFacesAs public static function Returns whether the a RuleExtendable supports the given interface.
RulesExtendable::rebuildCache public function Allows items to add something to the rules cache. 1
RulesExtendable::setUp protected function 1
RulesExtendable::__call public function Magic method: Invoke the dynamically implemented methods.
RulesExtendable::__construct public function 2