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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclNode
  • Aco
  • AcoAction
  • Aro
  • BehaviorCollection
  • CakeSchema
  • ConnectionManager
  • I18nModel
  • Model
  • ModelBehavior
  • ModelValidator
  • Permission
  1: <?php
  2: /**
  3:  * BehaviorCollection
  4:  *
  5:  * Provides management and interface for interacting with collections of behaviors.
  6:  *
  7:  * PHP 5
  8:  *
  9:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 10:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 11:  *
 12:  * Licensed under The MIT License
 13:  * For full copyright and license information, please see the LICENSE.txt
 14:  * Redistributions of files must retain the above copyright notice.
 15:  *
 16:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 17:  * @link          http://cakephp.org CakePHP(tm) Project
 18:  * @package       Cake.Model
 19:  * @since         CakePHP(tm) v 1.2.0.0
 20:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 21:  */
 22: 
 23: App::uses('ObjectCollection', 'Utility');
 24: App::uses('CakeEventListener', 'Event');
 25: 
 26: /**
 27:  * Model behavior collection class.
 28:  *
 29:  * Defines the Behavior interface, and contains common model interaction functionality.
 30:  *
 31:  * @package       Cake.Model
 32:  */
 33: class BehaviorCollection extends ObjectCollection implements CakeEventListener {
 34: 
 35: /**
 36:  * Stores a reference to the attached name
 37:  *
 38:  * @var string
 39:  */
 40:     public $modelName = null;
 41: 
 42: /**
 43:  * Keeps a list of all methods of attached behaviors
 44:  *
 45:  * @var array
 46:  */
 47:     protected $_methods = array();
 48: 
 49: /**
 50:  * Keeps a list of all methods which have been mapped with regular expressions
 51:  *
 52:  * @var array
 53:  */
 54:     protected $_mappedMethods = array();
 55: 
 56: /**
 57:  * Attaches a model object and loads a list of behaviors
 58:  *
 59:  * @param string $modelName
 60:  * @param array $behaviors
 61:  * @return void
 62:  */
 63:     public function init($modelName, $behaviors = array()) {
 64:         $this->modelName = $modelName;
 65: 
 66:         if (!empty($behaviors)) {
 67:             foreach (BehaviorCollection::normalizeObjectArray($behaviors) as $config) {
 68:                 $this->load($config['class'], $config['settings']);
 69:             }
 70:         }
 71:     }
 72: 
 73: /**
 74:  * Backwards compatible alias for load()
 75:  *
 76:  * @param string $behavior
 77:  * @param array $config
 78:  * @return void
 79:  * @deprecated Replaced with load()
 80:  */
 81:     public function attach($behavior, $config = array()) {
 82:         return $this->load($behavior, $config);
 83:     }
 84: 
 85: /**
 86:  * Loads a behavior into the collection. You can use use `$config['enabled'] = false`
 87:  * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors
 88:  * can still be used as normal.
 89:  *
 90:  * You can alias your behavior as an existing behavior by setting the 'className' key, i.e.,
 91:  * {{{
 92:  * public $actsAs = array(
 93:  *   'Tree' => array(
 94:  *     'className' => 'AliasedTree'
 95:  *   );
 96:  * );
 97:  * }}}
 98:  * All calls to the `Tree` behavior would use `AliasedTree` instead.
 99:  *
100:  * @param string $behavior CamelCased name of the behavior to load
101:  * @param array $config Behavior configuration parameters
102:  * @return boolean True on success, false on failure
103:  * @throws MissingBehaviorException when a behavior could not be found.
104:  */
105:     public function load($behavior, $config = array()) {
106:         if (is_array($config) && isset($config['className'])) {
107:             $alias = $behavior;
108:             $behavior = $config['className'];
109:         }
110:         $configDisabled = isset($config['enabled']) && $config['enabled'] === false;
111:         $priority = isset($config['priority']) ? $config['priority'] : $this->defaultPriority;
112:         unset($config['enabled'], $config['className'], $config['priority']);
113: 
114:         list($plugin, $name) = pluginSplit($behavior, true);
115:         if (!isset($alias)) {
116:             $alias = $name;
117:         }
118: 
119:         $class = $name . 'Behavior';
120: 
121:         App::uses($class, $plugin . 'Model/Behavior');
122:         if (!class_exists($class)) {
123:             throw new MissingBehaviorException(array(
124:                 'class' => $class,
125:                 'plugin' => substr($plugin, 0, -1)
126:             ));
127:         }
128: 
129:         if (!isset($this->{$alias})) {
130:             if (ClassRegistry::isKeySet($class)) {
131:                 $this->_loaded[$alias] = ClassRegistry::getObject($class);
132:             } else {
133:                 $this->_loaded[$alias] = new $class();
134:                 ClassRegistry::addObject($class, $this->_loaded[$alias]);
135:                 if (!empty($plugin)) {
136:                     ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]);
137:                 }
138:             }
139:         } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) {
140:             if ($config !== null && $config !== false) {
141:                 $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config);
142:             } else {
143:                 $config = array();
144:             }
145:         }
146:         if (empty($config)) {
147:             $config = array();
148:         }
149:         $this->_loaded[$alias]->settings['priority'] = $priority;
150:         $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config);
151: 
152:         foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) {
153:             $this->_mappedMethods[$method] = array($alias, $methodAlias);
154:         }
155:         $methods = get_class_methods($this->_loaded[$alias]);
156:         $parentMethods = array_flip(get_class_methods('ModelBehavior'));
157:         $callbacks = array(
158:             'setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave',
159:             'beforeDelete', 'afterDelete', 'onError'
160:         );
161: 
162:         foreach ($methods as $m) {
163:             if (!isset($parentMethods[$m])) {
164:                 $methodAllowed = (
165:                     $m[0] !== '_' && !array_key_exists($m, $this->_methods) &&
166:                     !in_array($m, $callbacks)
167:                 );
168:                 if ($methodAllowed) {
169:                     $this->_methods[$m] = array($alias, $m);
170:                 }
171:             }
172:         }
173: 
174:         if ($configDisabled) {
175:             $this->disable($alias);
176:         } elseif (!$this->enabled($alias)) {
177:             $this->enable($alias);
178:         } else {
179:             $this->setPriority($alias, $priority);
180:         }
181: 
182:         return true;
183:     }
184: 
185: /**
186:  * Detaches a behavior from a model
187:  *
188:  * @param string $name CamelCased name of the behavior to unload
189:  * @return void
190:  */
191:     public function unload($name) {
192:         list(, $name) = pluginSplit($name);
193:         if (isset($this->_loaded[$name])) {
194:             $this->_loaded[$name]->cleanup(ClassRegistry::getObject($this->modelName));
195:             parent::unload($name);
196:         }
197:         foreach ($this->_methods as $m => $callback) {
198:             if (is_array($callback) && $callback[0] == $name) {
199:                 unset($this->_methods[$m]);
200:             }
201:         }
202:     }
203: 
204: /**
205:  * Backwards compatible alias for unload()
206:  *
207:  * @param string $name Name of behavior
208:  * @return void
209:  * @deprecated Use unload instead.
210:  */
211:     public function detach($name) {
212:         return $this->unload($name);
213:     }
214: 
215: /**
216:  * Dispatches a behavior method. Will call either normal methods or mapped methods.
217:  *
218:  * If a method is not handled by the BehaviorCollection, and $strict is false, a
219:  * special return of `array('unhandled')` will be returned to signal the method was not found.
220:  *
221:  * @param Model $model The model the method was originally called on.
222:  * @param string $method The method called.
223:  * @param array $params Parameters for the called method.
224:  * @param boolean $strict If methods are not found, trigger an error.
225:  * @return array All methods for all behaviors attached to this object
226:  */
227:     public function dispatchMethod($model, $method, $params = array(), $strict = false) {
228:         $method = $this->hasMethod($method, true);
229: 
230:         if ($strict && empty($method)) {
231:             trigger_error(__d('cake_dev', "BehaviorCollection::dispatchMethod() - Method %s not found in any attached behavior", $method), E_USER_WARNING);
232:             return null;
233:         }
234:         if (empty($method)) {
235:             return array('unhandled');
236:         }
237:         if (count($method) === 3) {
238:             array_unshift($params, $method[2]);
239:             unset($method[2]);
240:         }
241:         return call_user_func_array(
242:             array($this->_loaded[$method[0]], $method[1]),
243:             array_merge(array(&$model), $params)
244:         );
245:     }
246: 
247: /**
248:  * Gets the method list for attached behaviors, i.e. all public, non-callback methods.
249:  * This does not include mappedMethods.
250:  *
251:  * @return array All public methods for all behaviors attached to this collection
252:  */
253:     public function methods() {
254:         return $this->_methods;
255:     }
256: 
257: /**
258:  * Check to see if a behavior in this collection implements the provided method. Will
259:  * also check mappedMethods.
260:  *
261:  * @param string $method The method to find.
262:  * @param boolean $callback Return the callback for the method.
263:  * @return mixed If $callback is false, a boolean will be returned, if its true, an array
264:  *   containing callback information will be returned. For mapped methods the array will have 3 elements.
265:  */
266:     public function hasMethod($method, $callback = false) {
267:         if (isset($this->_methods[$method])) {
268:             return $callback ? $this->_methods[$method] : true;
269:         }
270:         foreach ($this->_mappedMethods as $pattern => $target) {
271:             if (preg_match($pattern . 'i', $method)) {
272:                 if ($callback) {
273:                     $target[] = $method;
274:                     return $target;
275:                 }
276:                 return true;
277:             }
278:         }
279:         return false;
280:     }
281: 
282: /**
283:  * Returns the implemented events that will get routed to the trigger function
284:  * in order to dispatch them separately on each behavior
285:  *
286:  * @return array
287:  */
288:     public function implementedEvents() {
289:         return array(
290:             'Model.beforeFind' => 'trigger',
291:             'Model.afterFind' => 'trigger',
292:             'Model.beforeValidate' => 'trigger',
293:             'Model.afterValidate' => 'trigger',
294:             'Model.beforeSave' => 'trigger',
295:             'Model.afterSave' => 'trigger',
296:             'Model.beforeDelete' => 'trigger',
297:             'Model.afterDelete' => 'trigger'
298:         );
299:     }
300: 
301: }
302: 
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