2010-07-02 02:47:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-02 02:47:20 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-07-02 02:47:20 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-07-02 02:47:20 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2010-07-02 02:47:20 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2010-12-06 05:09:23 +00:00
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Deals with Collections of objects. Keeping registries of those objects,
|
2010-12-06 05:09:23 +00:00
|
|
|
* 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
|
2013-03-29 00:14:41 +00:00
|
|
|
* in `$this->_loaded`. Enabled objects are stored in `$this->_enabled`. In addition,
|
|
|
|
* they all support an `enabled` option that controls the enabled/disabled state of the object
|
2010-12-06 05:09:23 +00:00
|
|
|
* when loaded.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2010-12-06 05:09:23 +00:00
|
|
|
* @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
|
|
|
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();
|
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
/**
|
|
|
|
* Default object priority. A non zero integer.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-11-06 22:00:17 +00:00
|
|
|
*/
|
|
|
|
public $defaultPriority = 10;
|
|
|
|
|
2010-07-02 03:58:43 +00:00
|
|
|
/**
|
|
|
|
* Loads a new object onto the collection. Can throw a variety of exceptions
|
|
|
|
*
|
2011-11-06 14:59:46 +00:00
|
|
|
* Implementations of this class support a `$options['enabled']` flag which enables/disables
|
2010-11-07 03:48:27 +00:00
|
|
|
* 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.
|
2016-04-08 13:12:48 +00:00
|
|
|
* @return CakeObject the constructed object
|
2010-07-02 03:58:43 +00:00
|
|
|
*/
|
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.
|
2012-12-22 22:48:15 +00:00
|
|
|
* Used to trigger methods on objects in the collection. Will fire the methods in the
|
2010-07-02 03:58:43 +00:00
|
|
|
* 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
|
2012-12-22 22:48:15 +00:00
|
|
|
* will be returned. If used in combination with `collectReturn` the collected results will be returned.
|
2010-12-12 19:10:22 +00:00
|
|
|
* 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
|
|
|
*
|
|
|
|
* - `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.
|
2011-08-16 03:55:08 +00:00
|
|
|
*
|
2014-05-28 16:39:54 +00:00
|
|
|
* @param string|CakeEvent $callback Method to fire on all the objects. Its assumed all the objects implement
|
2011-12-25 01:30:39 +00:00
|
|
|
* the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
|
|
|
|
* get the callback name. This is done by getting the last word after any dot in the event name
|
|
|
|
* (eg. `Model.afterSave` event will trigger the `afterSave` callback)
|
2010-07-02 03:58:43 +00:00
|
|
|
* @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;
|
|
|
|
}
|
2011-12-25 01:30:39 +00:00
|
|
|
if ($callback instanceof CakeEvent) {
|
2011-12-25 23:30:19 +00:00
|
|
|
$event = $callback;
|
|
|
|
if (is_array($event->data)) {
|
|
|
|
$params =& $event->data;
|
2011-12-26 03:47:08 +00:00
|
|
|
}
|
|
|
|
if (empty($event->omitSubject)) {
|
|
|
|
$subject = $event->subject();
|
2011-12-25 01:30:39 +00:00
|
|
|
}
|
2012-11-14 12:16:08 +00:00
|
|
|
|
2011-12-26 17:36:48 +00:00
|
|
|
foreach (array('break', 'breakOn', 'collectReturn', 'modParams') as $opt) {
|
2011-12-26 03:18:27 +00:00
|
|
|
if (isset($event->{$opt})) {
|
|
|
|
$options[$opt] = $event->{$opt};
|
|
|
|
}
|
2011-12-25 23:30:19 +00:00
|
|
|
}
|
2011-12-29 16:10:25 +00:00
|
|
|
$parts = explode('.', $event->name());
|
|
|
|
$callback = array_pop($parts);
|
2011-12-25 01:30:39 +00:00
|
|
|
}
|
2014-04-07 23:25:14 +00:00
|
|
|
$options += array(
|
|
|
|
'break' => false,
|
|
|
|
'breakOn' => false,
|
|
|
|
'collectReturn' => false,
|
|
|
|
'modParams' => false
|
2010-07-02 03:58:43 +00:00
|
|
|
);
|
2010-07-04 20:57:13 +00:00
|
|
|
$collected = array();
|
2011-11-06 22:00:17 +00:00
|
|
|
$list = array_keys($this->_enabled);
|
2010-12-12 19:10:22 +00:00
|
|
|
if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
|
2011-03-20 15:35:43 +00:00
|
|
|
throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
|
2010-12-12 19:10:22 +00:00
|
|
|
}
|
2012-09-12 20:55:54 +00:00
|
|
|
$result = null;
|
2010-09-15 02:40:46 +00:00
|
|
|
foreach ($list as $name) {
|
2011-12-26 03:47:08 +00:00
|
|
|
$result = call_user_func_array(array($this->_loaded[$name], $callback), compact('subject') + $params);
|
2010-07-04 20:57:13 +00:00
|
|
|
if ($options['collectReturn'] === true) {
|
|
|
|
$collected[] = $result;
|
|
|
|
}
|
2014-12-09 02:17:35 +00:00
|
|
|
if ($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;
|
2011-10-09 01:34:18 +00:00
|
|
|
} elseif ($options['modParams'] !== false && !in_array($result, array(true, false, null), true)) {
|
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
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param string $name Name of object being checked.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool
|
2010-07-04 04:29:28 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
*
|
2011-11-06 22:00:17 +00:00
|
|
|
* @param string|array $name CamelCased name of the object(s) to enable (string or array)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $prioritize Prioritize enabled list after enabling object(s)
|
2010-07-02 03:00:48 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-11-06 22:00:17 +00:00
|
|
|
public function enable($name, $prioritize = true) {
|
|
|
|
$enabled = false;
|
2010-07-04 05:09:39 +00:00
|
|
|
foreach ((array)$name as $object) {
|
2015-07-24 01:42:26 +00:00
|
|
|
list(, $object) = pluginSplit($object);
|
2011-11-06 22:00:17 +00:00
|
|
|
if (isset($this->_loaded[$object]) && !isset($this->_enabled[$object])) {
|
2012-09-12 20:55:54 +00:00
|
|
|
$priority = $this->defaultPriority;
|
|
|
|
if (isset($this->_loaded[$object]->settings['priority'])) {
|
|
|
|
$priority = $this->_loaded[$object]->settings['priority'];
|
|
|
|
}
|
2011-11-06 22:00:17 +00:00
|
|
|
$this->_enabled[$object] = array($priority);
|
|
|
|
$enabled = true;
|
2010-07-04 05:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-06 22:00:17 +00:00
|
|
|
if ($prioritize && $enabled) {
|
|
|
|
$this->prioritize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prioritize list of enabled object
|
|
|
|
*
|
|
|
|
* @return array Prioritized list of object
|
|
|
|
*/
|
|
|
|
public function prioritize() {
|
|
|
|
$i = 1;
|
|
|
|
foreach ($this->_enabled as $name => $priority) {
|
|
|
|
$priority[1] = $i++;
|
|
|
|
$this->_enabled[$name] = $priority;
|
|
|
|
}
|
|
|
|
asort($this->_enabled);
|
|
|
|
return $this->_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set priority for an object or array of objects
|
|
|
|
*
|
|
|
|
* @param string|array $name CamelCased name of the object(s) to enable (string or array)
|
|
|
|
* If string the second param $priority is used else it should be an associative array
|
|
|
|
* with keys as object names and values as priorities to set.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int|null $priority Integer priority to set or null for default
|
2011-11-06 22:00:17 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setPriority($name, $priority = null) {
|
|
|
|
if (is_string($name)) {
|
|
|
|
$name = array($name => $priority);
|
|
|
|
}
|
2012-09-12 20:55:54 +00:00
|
|
|
foreach ($name as $object => $objectPriority) {
|
2015-07-24 01:42:26 +00:00
|
|
|
list(, $object) = pluginSplit($object);
|
2012-09-12 20:55:54 +00:00
|
|
|
if (isset($this->_loaded[$object])) {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($objectPriority === null) {
|
2012-09-12 20:55:54 +00:00
|
|
|
$objectPriority = $this->defaultPriority;
|
2011-11-06 22:00:17 +00:00
|
|
|
}
|
2012-09-12 20:55:54 +00:00
|
|
|
$this->_loaded[$object]->settings['priority'] = $objectPriority;
|
|
|
|
if (isset($this->_enabled[$object])) {
|
|
|
|
$this->_enabled[$object] = array($objectPriority);
|
2011-11-06 22:00:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->prioritize();
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Disables callbacks on an object or array of objects. Public object methods are still
|
2010-07-02 03:00:48 +00:00
|
|
|
* callable as normal.
|
|
|
|
*
|
2011-11-06 22:00:17 +00:00
|
|
|
* @param string|array $name CamelCased name of the objects(s) to disable (string or array)
|
2010-07-02 03:00:48 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function disable($name) {
|
|
|
|
foreach ((array)$name as $object) {
|
2015-07-24 01:42:26 +00:00
|
|
|
list(, $object) = pluginSplit($object);
|
2011-11-06 22:00:17 +00:00
|
|
|
unset($this->_enabled[$object]);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of currently-enabled objects, or, the current status of a single objects
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string $name Optional. The name of the object to check the status of. If omitted,
|
2010-07-02 03:00:48 +00:00
|
|
|
* 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)) {
|
2015-07-24 01:42:26 +00:00
|
|
|
list(, $name) = pluginSplit($name);
|
2011-11-06 22:00:17 +00:00
|
|
|
return isset($this->_enabled[$name]);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
2011-11-06 22:00:17 +00:00
|
|
|
return array_keys($this->_enabled);
|
2010-07-02 03:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-17 13:25:37 +00:00
|
|
|
* Gets the list of attached objects, or, whether the given object is attached
|
2010-07-02 03:00:48 +00:00
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string $name Optional. The name of the object to check the status of. If omitted,
|
2012-11-16 22:32:56 +00:00
|
|
|
* returns an array of currently-attached objects
|
|
|
|
* @return mixed If $name is specified, returns the boolean status of the corresponding object.
|
|
|
|
* Otherwise, returns an array of all attached objects.
|
2014-09-02 15:03:22 +00:00
|
|
|
* @deprecated 3.0.0 Will be removed in 3.0. Use loaded instead.
|
2010-07-02 03:00:48 +00:00
|
|
|
*/
|
|
|
|
public function attached($name = null) {
|
2012-11-16 11:19:23 +00:00
|
|
|
return $this->loaded($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of loaded objects, or, whether the given object is loaded
|
|
|
|
*
|
2012-12-22 22:48:15 +00:00
|
|
|
* @param string $name Optional. The name of the object to check the status of. If omitted,
|
2012-11-16 22:32:56 +00:00
|
|
|
* returns an array of currently-loaded objects
|
|
|
|
* @return mixed If $name is specified, returns the boolean status of the corresponding object.
|
|
|
|
* Otherwise, returns an array of all loaded objects.
|
2010-07-02 03:00:48 +00:00
|
|
|
*/
|
2012-11-16 11:14:28 +00:00
|
|
|
public function loaded($name = null) {
|
2010-07-02 03:00:48 +00:00
|
|
|
if (!empty($name)) {
|
2015-07-24 01:42:26 +00:00
|
|
|
list(, $name) = pluginSplit($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) {
|
2012-09-14 00:59:17 +00:00
|
|
|
list(, $name) = pluginSplit($name);
|
2012-09-12 20:55:54 +00:00
|
|
|
unset($this->_loaded[$name], $this->_enabled[$name]);
|
2010-07-07 03:29:42 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
2011-01-10 01:16:44 +00:00
|
|
|
* Adds or overwrites an instantiated object to the collection
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
|
|
|
* @param string $name Name of the object
|
2016-04-08 12:33:26 +00:00
|
|
|
* @param CakeObject $object The object to use
|
2011-07-29 02:03:44 +00:00
|
|
|
* @return array Loaded objects
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
public function set($name = null, $object = null) {
|
|
|
|
if (!empty($name) && !empty($object)) {
|
2012-09-13 20:12:04 +00:00
|
|
|
list(, $name) = pluginSplit($name);
|
|
|
|
$this->_loaded[$name] = $object;
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
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)) {
|
2011-10-15 18:52:41 +00:00
|
|
|
$options = (array)$objectName;
|
|
|
|
$objectName = $i;
|
2010-07-03 15:36:53 +00:00
|
|
|
}
|
2012-09-13 20:12:04 +00:00
|
|
|
list(, $name) = pluginSplit($objectName);
|
|
|
|
$normal[$name] = array('class' => $objectName, 'settings' => $options);
|
2010-07-03 15:36:53 +00:00
|
|
|
}
|
|
|
|
return $normal;
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2011-11-06 22:00:17 +00:00
|
|
|
}
|