CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.0 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.0
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Auth
    • Core
    • Error
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml
  1: <?php
  2: /**
  3:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4:  * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5:  *
  6:  * Licensed under The MIT License
  7:  * Redistributions of files must retain the above copyright notice.
  8:  *
  9:  * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  * @link          http://cakephp.org CakePHP(tm) Project
 11:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 12:  */
 13: 
 14: /**
 15:  * Deals with Collections of objects.  Keeping registries of those objects,
 16:  * loading and constructing new objects and triggering callbacks. Each subclass needs
 17:  * to implement its own load() functionality.
 18:  *
 19:  * All core subclasses of ObjectCollection by convention loaded objects are stored
 20:  * in `$this->_loaded`. Enabled objects are stored in `$this->_enabled`.  In addition
 21:  * the all support an `enabled` option that controls the enabled/disabled state of the object
 22:  * when loaded.
 23:  *
 24:  * @package       Cake.Utility
 25:  * @since CakePHP(tm) v 2.0
 26:  */
 27: abstract class ObjectCollection {
 28: 
 29: /**
 30:  * List of the currently-enabled objects
 31:  *
 32:  * @var array
 33:  */
 34:     protected $_enabled = array();
 35: 
 36: /**
 37:  * A hash of loaded objects, indexed by name
 38:  *
 39:  * @var array
 40:  */
 41:     protected $_loaded = array();
 42: 
 43: /**
 44:  * Loads a new object onto the collection. Can throw a variety of exceptions
 45:  *
 46:  * Implementations of this class support a `$options['enabled']` flag which enables/disables
 47:  * a loaded object.
 48:  *
 49:  * @param string $name Name of object to load.
 50:  * @param array $options Array of configuration options for the object to be constructed.
 51:  * @return object the constructed object
 52:  */
 53:     abstract public function load($name, $options = array());
 54: 
 55: /**
 56:  * Trigger a callback method on every object in the collection.
 57:  * Used to trigger methods on objects in the collection.  Will fire the methods in the
 58:  * order they were attached.
 59:  *
 60:  * ### Options
 61:  *
 62:  * - `breakOn` Set to the value or values you want the callback propagation to stop on.
 63:  *    Can either be a scalar value, or an array of values to break on. Defaults to `false`.
 64:  *
 65:  * - `break` Set to true to enabled breaking. When a trigger is broken, the last returned value
 66:  *    will be returned.  If used in combination with `collectReturn` the collected results will be returned.
 67:  *    Defaults to `false`.
 68:  *
 69:  * - `collectReturn` Set to true to collect the return of each object into an array.
 70:  *    This array of return values will be returned from the trigger() call. Defaults to `false`.
 71:  *
 72:  * - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
 73:  *    Setting modParams to an integer value will allow you to modify the parameter with that index.
 74:  *    Any non-null value will modify the parameter index indicated.
 75:  *    Defaults to false.
 76:  *
 77:  *
 78:  * @param string $callback Method to fire on all the objects. Its assumed all the objects implement
 79:  *   the method you are calling.
 80:  * @param array $params Array of parameters for the triggered callback.
 81:  * @param array $options Array of options.
 82:  * @return mixed Either the last result or all results if collectReturn is on.
 83:  * @throws CakeException when modParams is used with an index that does not exist.
 84:  */
 85:     public function trigger($callback, $params = array(), $options = array()) {
 86:         if (empty($this->_enabled)) {
 87:             return true;
 88:         }
 89:         $options = array_merge(
 90:             array(
 91:                 'break' => false,
 92:                 'breakOn' => false,
 93:                 'collectReturn' => false,
 94:                 'modParams' => false
 95:             ),
 96:             $options
 97:         );
 98:         $collected = array();
 99:         $list = $this->_enabled;
100:         if ($options['modParams'] !== false && !isset($params[$options['modParams']])) {
101:             throw new CakeException(__d('cake_dev', 'Cannot use modParams with indexes that do not exist.'));
102:         }
103:         foreach ($list as $name) {
104:             $result = call_user_func_array(array($this->_loaded[$name], $callback), $params);
105:             if ($options['collectReturn'] === true) {
106:                 $collected[] = $result;
107:             }
108:             if (
109:                 $options['break'] && ($result === $options['breakOn'] ||
110:                 (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))
111:             ) {
112:                 return $result;
113:             } elseif ($options['modParams'] !== false && is_array($result)) {
114:                 $params[$options['modParams']] = $result;
115:             }
116:         }
117:         if ($options['modParams'] !== false) {
118:             return $params[$options['modParams']];
119:         }
120:         return $options['collectReturn'] ? $collected : $result;
121:     }
122: 
123: /**
124:  * Provide public read access to the loaded objects
125:  *
126:  * @param string $name Name of property to read
127:  * @return mixed
128:  */
129:     public function __get($name) {
130:         if (isset($this->_loaded[$name])) {
131:             return $this->_loaded[$name];
132:         }
133:         return null;
134:     }
135: 
136: /**
137:  * Provide isset access to _loaded
138:  *
139:  * @param string $name Name of object being checked.
140:  * @return boolean
141:  */
142:     public function __isset($name) {
143:         return isset($this->_loaded[$name]);
144:     }
145: 
146: /**
147:  * Enables callbacks on an object or array of objects
148:  *
149:  * @param mixed $name CamelCased name of the object(s) to enable (string or array)
150:  * @return void
151:  */
152:     public function enable($name) {
153:         foreach ((array)$name as $object) {
154:             if (isset($this->_loaded[$object]) && array_search($object, $this->_enabled) === false) {
155:                 $this->_enabled[] = $object;
156:             }
157:         }
158:     }
159: 
160: /**
161:  * Disables callbacks on a object or array of objects.  Public object methods are still
162:  * callable as normal.
163:  *
164:  * @param mixed $name CamelCased name of the objects(s) to disable (string or array)
165:  * @return void
166:  */
167:     public function disable($name) {
168:         foreach ((array)$name as $object) {
169:             $index = array_search($object, $this->_enabled);
170:             unset($this->_enabled[$index]);
171:         }
172:         $this->_enabled = array_values($this->_enabled);
173:     }
174: 
175: /**
176:  * Gets the list of currently-enabled objects, or, the current status of a single objects
177:  *
178:  * @param string $name Optional.  The name of the object to check the status of.  If omitted,
179:  *   returns an array of currently-enabled object
180:  * @return mixed If $name is specified, returns the boolean status of the corresponding object.
181:  *   Otherwise, returns an array of all enabled objects.
182:  */
183:     public function enabled($name = null) {
184:         if (!empty($name)) {
185:             return in_array($name, $this->_enabled);
186:         }
187:         return $this->_enabled;
188:     }
189: 
190: /**
191:  * Gets the list of attached behaviors, or, whether the given behavior is attached
192:  *
193:  * @param string $name Optional.  The name of the behavior to check the status of.  If omitted,
194:  *   returns an array of currently-attached behaviors
195:  * @return mixed If $name is specified, returns the boolean status of the corresponding behavior.
196:  *    Otherwise, returns an array of all attached behaviors.
197:  */
198:     public function attached($name = null) {
199:         if (!empty($name)) {
200:             return isset($this->_loaded[$name]);
201:         }
202:         return array_keys($this->_loaded);
203:     }
204: 
205: /**
206:  * Name of the object to remove from the collection
207:  *
208:  * @param string $name Name of the object to delete.
209:  * @return void
210:  */
211:     public function unload($name) {
212:         list($plugin, $name) = pluginSplit($name);
213:         unset($this->_loaded[$name]);
214:         $this->_enabled = array_values(array_diff($this->_enabled, (array)$name));
215:     }
216: 
217: /**
218:  * Adds or overwrites an instantiated object to the collection
219:  *
220:  * @param string $name Name of the object
221:  * @param Object $object The object to use
222:  * @return array Loaded objects
223:  */
224:     public function set($name = null, $object = null) {
225:         if (!empty($name) && !empty($object)) {
226:             list($plugin, $name) = pluginSplit($name);
227:             $this->_loaded[$name] = $object;
228:         }
229:         return $this->_loaded;
230:     }
231: 
232: /**
233:  * Normalizes an object array, creates an array that makes lazy loading
234:  * easier
235:  *
236:  * @param array $objects Array of child objects to normalize.
237:  * @return array Array of normalized objects.
238:  */
239:     public static function normalizeObjectArray($objects) {
240:         $normal = array();
241:         foreach ($objects as $i => $objectName) {
242:             $options = array();
243:             if (!is_int($i)) {
244:                 $options = (array)$objectName;
245:                 $objectName = $i;
246:             }
247:             list($plugin, $name) = pluginSplit($objectName);
248:             $normal[$name] = array('class' => $objectName, 'settings' => $options);
249:         }
250:         return $normal;
251:     }
252: }
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs