2010-07-02 02:47:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-12-06 05:09:23 +00:00
|
|
|
/**
|
|
|
|
* Deals with Collections of objects. Keeping registries of those objects,
|
|
|
|
* loading and constructing new objects and triggering callbacks. Each subclass needs
|
|
|
|
* to implement its own load() functionality.
|
|
|
|
*
|
|
|
|
* All core subclasses of ObjectCollection by convention loaded objects are stored
|
|
|
|
* in `$this->_loaded`. Enabled objects are stored in `$this->_enabled`. In addition
|
|
|
|
* the all support an `enabled` option that controls the enabled/disabled state of the object
|
|
|
|
* when loaded.
|
|
|
|
*
|
|
|
|
* @package cake.libs
|
|
|
|
* @since CakePHP(tm) v 2.0
|
|
|
|
*/
|
2010-07-02 02:47:20 +00:00
|
|
|
abstract class ObjectCollection {
|
|
|
|
|
|
|
|
/**
|
2010-07-04 05:09:39 +00:00
|
|
|
* List of the currently-enabled objects
|
2010-07-02 02:47:20 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2010-07-04 05:09:39 +00:00
|
|
|
* @access protected
|
2010-07-02 02:47:20 +00:00
|
|
|
*/
|
2010-07-04 05:09:39 +00:00
|
|
|
protected $_enabled = array();
|
2010-07-02 02:47:20 +00:00
|
|
|
|
2010-07-04 04:29:28 +00:00
|
|
|
/**
|
2010-08-12 02:47:20 +00:00
|
|
|
* A hash of loaded objects, indexed by name
|
2010-07-04 04:29:28 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $_loaded = array();
|
|
|
|
|
2010-07-02 03:58:43 +00:00
|
|
|
/**
|
|
|
|
* Loads a new object onto the collection. Can throw a variety of exceptions
|
|
|
|
*
|
2010-11-07 03:48:27 +00:00
|
|
|
* Implementations of this class support a `$options['callbacks']` flag which enables/disables
|
|
|
|
* a loaded object.
|
|
|
|
*
|
2010-07-02 03:58:43 +00:00
|
|
|
* @param string $name Name of object to load.
|
|
|
|
* @param array $options Array of configuration options for the object to be constructed.
|
|
|
|
* @return object the constructed object
|
|
|
|
*/
|
2010-11-07 03:48:27 +00:00
|
|
|
abstract public function load($name, $options = array());
|
2010-07-02 03:58:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a callback method on every object in the collection.
|
|
|
|
* Used to trigger methods on objects in the collection. Will fire the methods in the
|
|
|
|
* order they were attached.
|
|
|
|
*
|
2010-07-02 04:01:42 +00:00
|
|
|
* ### Options
|
|
|
|
*
|
|
|
|
* - `breakOn` Set to the value or values you want the callback propagation to stop on.
|
2010-12-12 19:10:22 +00:00
|
|
|
* Can either be a scalar value, or an array of values to break on. Defaults to `false`.
|
|
|
|
*
|
|
|
|
* - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
|
|
|
|
* will be returned. If used in combination with `collectReturn` the collected results will be returned.
|
|
|
|
* Defaults to `false`.
|
|
|
|
*
|
2010-08-11 03:45:28 +00:00
|
|
|
* - `collectReturn` Set to true to collect the return of each object into an array.
|
|
|
|
* This array of return values will be returned from the trigger() call. Defaults to `false`.
|
2010-12-12 19:10:22 +00:00
|
|
|
*
|
2010-09-15 01:57:40 +00:00
|
|
|
* - `triggerDisabled` Will trigger the callback on all objects in the collection even the non-enabled
|
2010-12-12 19:10:22 +00:00
|
|
|
* objects. Defaults to false.
|
|
|
|
*
|
|
|
|
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
|
2010-12-12 20:33:24 +00:00
|
|
|
* Setting modParams to an integer value will allow you to modify the parameter with that index.
|
|
|
|
* Any non-null value will modify the parameter index indicated.
|
|
|
|
* Defaults to false.
|
|
|
|
*
|
2010-07-02 03:58:43 +00:00
|
|
|
*
|
|
|
|
* @param string $callback Method to fire on all the objects. Its assumed all the objects implement
|
|
|
|
* the method you are calling.
|
|
|
|
* @param array $params Array of parameters for the triggered callback.
|
|
|
|
* @param array $options Array of options.
|
2010-12-12 19:12:55 +00:00
|
|
|
* @return mixed Either the last result or all results if collectReturn is on.
|
|
|
|
* @throws CakeException when modParams is used with an index that does not exist.
|
2010-07-02 03:58:43 +00:00
|
|
|
*/
|
|
|
|
public function trigger($callback, $params = array(), $options = array()) {
|
2010-07-04 05:09:39 +00:00
|
|
|
if (empty($this->_enabled)) {
|
2010-07-02 03:58:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$options = array_merge(
|
2010-12-12 19:10:22 +00:00
|
|
|
array(
|
|
|
|
'break' => false,
|
|
|
|
'breakOn' => false,
|
|
|
|
'collectReturn' => false,
|
|
|
|
'triggerDisabled' => false,
|
|
|
|
'modParams' => false
|
|
|
|
),
|
2010-07-02 03:58:43 +00:00
|
|
|
$options
|
|
|
|
);
|
2010-07-04 20:57:13 +00:00
|
|
|
$collected = array();
|
2010-09-15 01:57:40 +00:00
|
|
|
$list = $this->_enabled;
|
|
|
|
if ($options['triggerDisabled'] === true) {
|
|
|
|
$list = array_keys($this->_loaded);
|
|
|
|
}
|
2010-12-12 19:10:22 +00:00
|
|
|
if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
|
|
|
|
throw new CakeException(__('Cannot use modParams with indexes that do not exist.'));
|
|
|
|
}
|
2010-09-15 02:40:46 +00:00
|
|
|
foreach ($list as $name) {
|
2010-12-12 19:10:22 +00:00
|
|
|
$result = call_user_func_array(array($this->_loaded[$name], $callback), $params);
|
2010-07-04 20:57:13 +00:00
|
|
|
if ($options['collectReturn'] === true) {
|
|
|
|
$collected[] = $result;
|
|
|
|
}
|
2010-07-02 03:58:43 +00:00
|
|
|
if (
|
2010-12-12 22:41:57 +00:00
|
|
|
$options['break'] && ($result === $options['breakOn'] ||
|
2010-07-02 03:58:43 +00:00
|
|
|
(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
|
|
|
|
) {
|
2010-12-12 22:41:57 +00:00
|
|
|
return $result;
|
|
|
|
} elseif ($options['modParams'] !== false && is_array($result)) {
|
2010-12-12 19:10:22 +00:00
|
|
|
$params[$options['modParams']] = $result;
|
2010-07-02 03:58:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-12 19:10:22 +00:00
|
|
|
if ($options['modParams'] !== false) {
|
|
|
|
return $params[$options['modParams']];
|
|
|
|
}
|
|
|
|
return $options['collectReturn'] ? $collected : $result;
|
2010-07-02 03:58:43 +00:00
|
|
|
}
|
|
|
|
|
2010-07-04 04:29:28 +00:00
|
|
|
/**
|
|
|
|
* Provide public read access to the loaded objects
|
|
|
|
*
|
|
|
|
* @param string $name Name of property to read
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($name) {
|
|
|
|
if (isset($this->_loaded[$name])) {
|
|
|
|
return $this->_loaded[$name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide isset access to _loaded
|
|
|
|
*
|
|
|
|
* @param sting $name Name of object being checked.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function __isset($name) {
|
|
|
|
return isset($this->_loaded[$name]);
|
|
|
|
}
|
|
|
|
|
2010-07-02 03:00:48 +00:00
|
|
|
/**
|
2010-07-04 05:09:39 +00:00
|
|
|
* Enables callbacks on an object or array of objects
|
2010-07-02 03:00:48 +00:00
|
|
|
*
|
2010-07-04 05:09:39 +00:00
|
|
|
* @param mixed $name CamelCased name of the object(s) to enable (string or array)
|
2010-07-02 03:00:48 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function enable($name) {
|
2010-07-04 05:09:39 +00:00
|
|
|
foreach ((array)$name as $object) {
|
|
|
|
if (isset($this->_loaded[$object]) && array_search($object, $this->_enabled) === false) {
|
|
|
|
$this->_enabled[] = $object;
|
|
|
|
}
|
|
|
|
}
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables callbacks on a object or array of objects. Public object methods are still
|
|
|
|
* callable as normal.
|
|
|
|
*
|
|
|
|
* @param mixed $name CamelCased name of the objects(s) to disable (string or array)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function disable($name) {
|
|
|
|
foreach ((array)$name as $object) {
|
2010-07-04 05:09:39 +00:00
|
|
|
$index = array_search($object, $this->_enabled);
|
|
|
|
unset($this->_enabled[$index]);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
2010-07-04 05:09:39 +00:00
|
|
|
$this->_enabled = array_values($this->_enabled);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of currently-enabled objects, or, the current status of a single objects
|
|
|
|
*
|
|
|
|
* @param string $name Optional. The name of the object to check the status of. If omitted,
|
|
|
|
* returns an array of currently-enabled object
|
|
|
|
* @return mixed If $name is specified, returns the boolean status of the corresponding object.
|
|
|
|
* Otherwise, returns an array of all enabled objects.
|
|
|
|
*/
|
|
|
|
public function enabled($name = null) {
|
|
|
|
if (!empty($name)) {
|
2010-07-04 05:09:39 +00:00
|
|
|
return in_array($name, $this->_enabled);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
2010-07-04 05:09:39 +00:00
|
|
|
return $this->_enabled;
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of attached behaviors, or, whether the given behavior is attached
|
|
|
|
*
|
|
|
|
* @param string $name Optional. The name of the behavior to check the status of. If omitted,
|
|
|
|
* returns an array of currently-attached behaviors
|
|
|
|
* @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
|
|
|
|
* Otherwise, returns an array of all attached behaviors.
|
|
|
|
*/
|
|
|
|
public function attached($name = null) {
|
|
|
|
if (!empty($name)) {
|
2010-07-04 05:09:39 +00:00
|
|
|
return isset($this->_loaded[$name]);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
2010-07-04 05:09:39 +00:00
|
|
|
return array_keys($this->_loaded);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
2010-07-07 03:29:42 +00:00
|
|
|
/**
|
|
|
|
* Name of the object to remove from the collection
|
|
|
|
*
|
|
|
|
* @param string $name Name of the object to delete.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function unload($name) {
|
|
|
|
list($plugin, $name) = pluginSplit($name);
|
|
|
|
unset($this->_loaded[$name]);
|
|
|
|
$this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Adds or overwrites an instatiated object to the collection
|
|
|
|
*
|
|
|
|
* @param string $name Name of the object
|
|
|
|
* @param Object $object The object to use
|
|
|
|
*/
|
|
|
|
public function set($name = null, $object = null) {
|
|
|
|
if (!empty($name) && !empty($object)) {
|
|
|
|
list($plugin, $name) = pluginSplit($name);
|
|
|
|
$this->_loaded[$name] = $object;
|
|
|
|
}
|
|
|
|
return $this->_loaded;
|
|
|
|
}
|
|
|
|
|
2010-07-03 15:36:53 +00:00
|
|
|
/**
|
|
|
|
* Normalizes an object array, creates an array that makes lazy loading
|
|
|
|
* easier
|
|
|
|
*
|
|
|
|
* @param array $objects Array of child objects to normalize.
|
|
|
|
* @return array Array of normalized objects.
|
|
|
|
*/
|
|
|
|
public static function normalizeObjectArray($objects) {
|
|
|
|
$normal = array();
|
|
|
|
foreach ($objects as $i => $objectName) {
|
|
|
|
$options = array();
|
|
|
|
if (!is_int($i)) {
|
|
|
|
list($options, $objectName) = array($objectName, $i);
|
|
|
|
}
|
|
|
|
list($plugin, $name) = pluginSplit($objectName);
|
|
|
|
$normal[$name] = array('class' => $objectName, 'settings' => $options);
|
|
|
|
}
|
|
|
|
return $normal;
|
|
|
|
}
|
2010-07-02 02:47:20 +00:00
|
|
|
}
|