Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
13
 */
14
 
15
/**
16
 * Deals with Collections of objects. Keeping registries of those objects,
17
 * loading and constructing new objects and triggering callbacks. Each subclass needs
18
 * to implement its own load() functionality.
19
 *
20
 * All core subclasses of ObjectCollection by convention loaded objects are stored
21
 * in `$this->_loaded`. Enabled objects are stored in `$this->_enabled`. In addition,
22
 * they all support an `enabled` option that controls the enabled/disabled state of the object
23
 * when loaded.
24
 *
25
 * @package       Cake.Utility
26
 * @since CakePHP(tm) v 2.0
27
 */
28
abstract class ObjectCollection {
29
 
30
/**
31
 * List of the currently-enabled objects
32
 *
33
 * @var array
34
 */
35
	protected $_enabled = array();
36
 
37
/**
38
 * A hash of loaded objects, indexed by name
39
 *
40
 * @var array
41
 */
42
	protected $_loaded = array();
43
 
44
/**
45
 * Default object priority. A non zero integer.
46
 *
47
 * @var int
48
 */
49
	public $defaultPriority = 10;
50
 
51
/**
52
 * Loads a new object onto the collection. Can throw a variety of exceptions
53
 *
54
 * Implementations of this class support a `$options['enabled']` flag which enables/disables
55
 * a loaded object.
56
 *
57
 * @param string $name Name of object to load.
58
 * @param array $options Array of configuration options for the object to be constructed.
59
 * @return object the constructed object
60
 */
61
	abstract public function load($name, $options = array());
62
 
63
/**
64
 * Trigger a callback method on every object in the collection.
65
 * Used to trigger methods on objects in the collection. Will fire the methods in the
66
 * order they were attached.
67
 *
68
 * ### Options
69
 *
70
 * - `breakOn` Set to the value or values you want the callback propagation to stop on.
71
 *    Can either be a scalar value, or an array of values to break on. Defaults to `false`.
72
 *
73
 * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
74
 *    will be returned. If used in combination with `collectReturn` the collected results will be returned.
75
 *    Defaults to `false`.
76
 *
77
 * - `collectReturn` Set to true to collect the return of each object into an array.
78
 *    This array of return values will be returned from the trigger() call. Defaults to `false`.
79
 *
80
 * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
81
 *    Setting modParams to an integer value will allow you to modify the parameter with that index.
82
 *    Any non-null value will modify the parameter index indicated.
83
 *    Defaults to false.
84
 *
85
 * @param string|CakeEvent $callback Method to fire on all the objects. Its assumed all the objects implement
86
 *   the method you are calling. If an instance of CakeEvent is provided, then then Event name will parsed to
87
 *   get the callback name. This is done by getting the last word after any dot in the event name
88
 *   (eg. `Model.afterSave` event will trigger the `afterSave` callback)
89
 * @param array $params Array of parameters for the triggered callback.
90
 * @param array $options Array of options.
91
 * @return mixed Either the last result or all results if collectReturn is on.
92
 * @throws CakeException when modParams is used with an index that does not exist.
93
 */
94
	public function trigger($callback, $params = array(), $options = array()) {
95
		if (empty($this->_enabled)) {
96
			return true;
97
		}
98
		if ($callback instanceof CakeEvent) {
99
			$event = $callback;
100
			if (is_array($event->data)) {
101
				$params =& $event->data;
102
			}
103
			if (empty($event->omitSubject)) {
104
				$subject = $event->subject();
105
			}
106
 
107
			foreach (array('break', 'breakOn', 'collectReturn', 'modParams') as $opt) {
108
				if (isset($event->{$opt})) {
109
					$options[$opt] = $event->{$opt};
110
				}
111
			}
112
			$parts = explode('.', $event->name());
113
			$callback = array_pop($parts);
114
		}
115
		$options += array(
116
			'break' => false,
117
			'breakOn' => false,
118
			'collectReturn' => false,
119
			'modParams' => false
120
		);
121
		$collected = array();
122
		$list = array_keys($this->_enabled);
123
		if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
124
			throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
125
		}
126
		$result = null;
127
		foreach ($list as $name) {
128
			$result = call_user_func_array(array($this->_loaded[$name], $callback), compact('subject') + $params);
129
			if ($options['collectReturn'] === true) {
130
				$collected[] = $result;
131
			}
132
			if (
133
				$options['break'] && ($result === $options['breakOn'] ||
134
				(is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
135
			) {
136
				return $result;
137
			} elseif ($options['modParams'] !== false && !in_array($result, array(true, false, null), true)) {
138
				$params[$options['modParams']] = $result;
139
			}
140
		}
141
		if ($options['modParams'] !== false) {
142
			return $params[$options['modParams']];
143
		}
144
		return $options['collectReturn'] ? $collected : $result;
145
	}
146
 
147
/**
148
 * Provide public read access to the loaded objects
149
 *
150
 * @param string $name Name of property to read
151
 * @return mixed
152
 */
153
	public function __get($name) {
154
		if (isset($this->_loaded[$name])) {
155
			return $this->_loaded[$name];
156
		}
157
		return null;
158
	}
159
 
160
/**
161
 * Provide isset access to _loaded
162
 *
163
 * @param string $name Name of object being checked.
164
 * @return bool
165
 */
166
	public function __isset($name) {
167
		return isset($this->_loaded[$name]);
168
	}
169
 
170
/**
171
 * Enables callbacks on an object or array of objects
172
 *
173
 * @param string|array $name CamelCased name of the object(s) to enable (string or array)
174
 * @param bool $prioritize Prioritize enabled list after enabling object(s)
175
 * @return void
176
 */
177
	public function enable($name, $prioritize = true) {
178
		$enabled = false;
179
		foreach ((array)$name as $object) {
180
			if (isset($this->_loaded[$object]) && !isset($this->_enabled[$object])) {
181
				$priority = $this->defaultPriority;
182
				if (isset($this->_loaded[$object]->settings['priority'])) {
183
					$priority = $this->_loaded[$object]->settings['priority'];
184
				}
185
				$this->_enabled[$object] = array($priority);
186
				$enabled = true;
187
			}
188
		}
189
		if ($prioritize && $enabled) {
190
			$this->prioritize();
191
		}
192
	}
193
 
194
/**
195
 * Prioritize list of enabled object
196
 *
197
 * @return array Prioritized list of object
198
 */
199
	public function prioritize() {
200
		$i = 1;
201
		foreach ($this->_enabled as $name => $priority) {
202
			$priority[1] = $i++;
203
			$this->_enabled[$name] = $priority;
204
		}
205
		asort($this->_enabled);
206
		return $this->_enabled;
207
	}
208
 
209
/**
210
 * Set priority for an object or array of objects
211
 *
212
 * @param string|array $name CamelCased name of the object(s) to enable (string or array)
213
 * 	If string the second param $priority is used else it should be an associative array
214
 * 	with keys as object names and values as priorities to set.
215
 * @param int|null $priority Integer priority to set or null for default
216
 * @return void
217
 */
218
	public function setPriority($name, $priority = null) {
219
		if (is_string($name)) {
220
			$name = array($name => $priority);
221
		}
222
		foreach ($name as $object => $objectPriority) {
223
			if (isset($this->_loaded[$object])) {
224
				if ($objectPriority === null) {
225
					$objectPriority = $this->defaultPriority;
226
				}
227
				$this->_loaded[$object]->settings['priority'] = $objectPriority;
228
				if (isset($this->_enabled[$object])) {
229
					$this->_enabled[$object] = array($objectPriority);
230
				}
231
			}
232
		}
233
		$this->prioritize();
234
	}
235
 
236
/**
237
 * Disables callbacks on a object or array of objects. Public object methods are still
238
 * callable as normal.
239
 *
240
 * @param string|array $name CamelCased name of the objects(s) to disable (string or array)
241
 * @return void
242
 */
243
	public function disable($name) {
244
		foreach ((array)$name as $object) {
245
			unset($this->_enabled[$object]);
246
		}
247
	}
248
 
249
/**
250
 * Gets the list of currently-enabled objects, or, the current status of a single objects
251
 *
252
 * @param string $name Optional. The name of the object to check the status of. If omitted,
253
 *   returns an array of currently-enabled object
254
 * @return mixed If $name is specified, returns the boolean status of the corresponding object.
255
 *   Otherwise, returns an array of all enabled objects.
256
 */
257
	public function enabled($name = null) {
258
		if (!empty($name)) {
259
			return isset($this->_enabled[$name]);
260
		}
261
		return array_keys($this->_enabled);
262
	}
263
 
264
/**
265
 * Gets the list of attached objects, or, whether the given object is attached
266
 *
267
 * @param string $name Optional. The name of the object to check the status of. If omitted,
268
 *   returns an array of currently-attached objects
269
 * @return mixed If $name is specified, returns the boolean status of the corresponding object.
270
 *    Otherwise, returns an array of all attached objects.
271
 * @deprecated Will be removed in 3.0. Use loaded instead.
272
 */
273
	public function attached($name = null) {
274
		return $this->loaded($name);
275
	}
276
 
277
/**
278
 * Gets the list of loaded objects, or, whether the given object is loaded
279
 *
280
 * @param string $name Optional. The name of the object to check the status of. If omitted,
281
 *   returns an array of currently-loaded objects
282
 * @return mixed If $name is specified, returns the boolean status of the corresponding object.
283
 *    Otherwise, returns an array of all loaded objects.
284
 */
285
	public function loaded($name = null) {
286
		if (!empty($name)) {
287
			return isset($this->_loaded[$name]);
288
		}
289
		return array_keys($this->_loaded);
290
	}
291
 
292
/**
293
 * Name of the object to remove from the collection
294
 *
295
 * @param string $name Name of the object to delete.
296
 * @return void
297
 */
298
	public function unload($name) {
299
		list(, $name) = pluginSplit($name);
300
		unset($this->_loaded[$name], $this->_enabled[$name]);
301
	}
302
 
303
/**
304
 * Adds or overwrites an instantiated object to the collection
305
 *
306
 * @param string $name Name of the object
307
 * @param Object $object The object to use
308
 * @return array Loaded objects
309
 */
310
	public function set($name = null, $object = null) {
311
		if (!empty($name) && !empty($object)) {
312
			list(, $name) = pluginSplit($name);
313
			$this->_loaded[$name] = $object;
314
		}
315
		return $this->_loaded;
316
	}
317
 
318
/**
319
 * Normalizes an object array, creates an array that makes lazy loading
320
 * easier
321
 *
322
 * @param array $objects Array of child objects to normalize.
323
 * @return array Array of normalized objects.
324
 */
325
	public static function normalizeObjectArray($objects) {
326
		$normal = array();
327
		foreach ($objects as $i => $objectName) {
328
			$options = array();
329
			if (!is_int($i)) {
330
				$options = (array)$objectName;
331
				$objectName = $i;
332
			}
333
			list(, $name) = pluginSplit($objectName);
334
			$normal[$name] = array('class' => $objectName, 'settings' => $options);
335
		}
336
		return $normal;
337
	}
338
 
339
}