00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class ModelBehavior extends Object {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 var $settings = array();
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 var $mapMethods = array();
00060
00061
00062
00063
00064
00065
00066
00067 function setup(&$model, $config = array()) { }
00068
00069
00070
00071
00072
00073
00074
00075
00076 function cleanup(&$model) {
00077 if (isset($this->settings[$model->alias])) {
00078 unset($this->settings[$model->alias]);
00079 }
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089 function beforeFind(&$model, $query) { }
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 function afterFind(&$model, $results, $primary) { }
00100
00101
00102
00103
00104
00105
00106
00107 function beforeValidate(&$model) { }
00108
00109
00110
00111
00112
00113
00114
00115 function beforeSave(&$model) { }
00116
00117
00118
00119
00120
00121
00122
00123 function afterSave(&$model, $created) { }
00124
00125
00126
00127
00128
00129
00130
00131
00132 function beforeDelete(&$model, $cascade = true) { }
00133
00134
00135
00136
00137
00138
00139 function afterDelete(&$model) { }
00140
00141
00142
00143
00144
00145
00146
00147 function onError(&$model, $error) { }
00148
00149
00150
00151
00152
00153
00154 function dispatchMethod(&$model, $method, $params = array()) {
00155 if (empty($params)) {
00156 return $this->{$method}($model);
00157 }
00158 $params = array_values($params);
00159
00160 switch (count($params)) {
00161 case 1:
00162 return $this->{$method}($model, $params[0]);
00163 case 2:
00164 return $this->{$method}($model, $params[0], $params[1]);
00165 case 3:
00166 return $this->{$method}($model, $params[0], $params[1], $params[2]);
00167 case 4:
00168 return $this->{$method}($model, $params[0], $params[1], $params[2], $params[3]);
00169 case 5:
00170 return $this->{$method}($model, $params[0], $params[1], $params[2], $params[3], $params[4]);
00171 default:
00172 array_unshift($params, $model);
00173 return call_user_func_array(array(&$this, $method), $params);
00174 break;
00175 }
00176 }
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 function _addToWhitelist(&$model, $field) {
00189 if (is_array($field)) {
00190 foreach ($field as $f) {
00191 $this->_addToWhitelist($model, $f);
00192 }
00193 return;
00194 }
00195 if (!empty($model->whitelist) && !in_array($field, $model->whitelist)) {
00196 $model->whitelist[] = $field;
00197 }
00198 }
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 class BehaviorCollection extends Object {
00210
00211
00212
00213
00214
00215
00216 var $modelName = null;
00217
00218
00219
00220
00221
00222
00223 var $_attached = array();
00224
00225
00226
00227
00228
00229
00230 var $_disabled = array();
00231
00232
00233
00234
00235
00236 var $__methods = array();
00237
00238
00239
00240
00241
00242 var $__mappedMethods = array();
00243
00244
00245
00246
00247
00248 function init($modelName, $behaviors = array()) {
00249 $this->modelName = $modelName;
00250
00251 if (!empty($behaviors)) {
00252 foreach (Set::normalize($behaviors) as $behavior => $config) {
00253 $this->attach($behavior, $config);
00254 }
00255 }
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265 function attach($behavior, $config = array()) {
00266 $name = $behavior;
00267 if (strpos($behavior, '.')) {
00268 list($plugin, $name) = explode('.', $behavior, 2);
00269 }
00270 $class = $name . 'Behavior';
00271
00272 if (!App::import('Behavior', $behavior)) {
00273 return false;
00274 }
00275
00276 if (!isset($this->{$name})) {
00277 if (PHP5) {
00278 $this->{$name} = new $class;
00279 } else {
00280 $this->{$name} =& new $class;
00281 }
00282 } elseif (isset($this->{$name}->settings) && isset($this->{$name}->settings[$this->modelName])) {
00283 if ($config !== null && $config !== false) {
00284 $config = array_merge($this->{$name}->settings[$this->modelName], $config);
00285 } else {
00286 $config = array();
00287 }
00288 }
00289 $this->{$name}->setup(ClassRegistry::getObject($this->modelName), $config);
00290
00291 foreach ($this->{$name}->mapMethods as $method => $alias) {
00292 $this->__mappedMethods[$method] = array($alias, $name);
00293 }
00294 $methods = get_class_methods($this->{$name});
00295 $parentMethods = get_class_methods('ModelBehavior');
00296 $callbacks = array('setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave', 'beforeDelete', 'afterDelete', 'afterError');
00297
00298 foreach ($methods as $m) {
00299 if (!in_array($m, $parentMethods)) {
00300 if ($m[0] != '_' && !array_key_exists($m, $this->__methods) && !in_array($m, $callbacks)) {
00301 $this->__methods[$m] = array($m, $name);
00302 }
00303 }
00304 }
00305
00306 if (!in_array($name, $this->_attached)) {
00307 $this->_attached[] = $name;
00308 }
00309 if (in_array($name, $this->_disabled) && !(isset($config['enabled']) && $config['enabled'] === false)) {
00310 $this->enable($name);
00311 } elseif (isset($config['enabled']) && $config['enabled'] === false) {
00312 $this->disable($name);
00313 }
00314 return true;
00315 }
00316
00317
00318
00319
00320
00321
00322 function detach($name) {
00323 if (isset($this->{$name})) {
00324 $this->{$name}->cleanup(ClassRegistry::getObject($this->modelName));
00325 unset($this->{$name});
00326 }
00327 foreach ($this->__methods as $m => $callback) {
00328 if (is_array($callback) && $callback[1] == $name) {
00329 unset($this->__methods[$m]);
00330 }
00331 }
00332 $keys = array_combine(array_values($this->_attached), array_keys($this->_attached));
00333 unset($this->_attached[$keys[$name]]);
00334 $this->_attached = array_values($this->_attached);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 function enable($name) {
00344 $keys = array_combine(array_values($this->_disabled), array_keys($this->_disabled));
00345 foreach ((array)$name as $behavior) {
00346 unset($this->_disabled[$keys[$behavior]]);
00347 }
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357 function disable($name) {
00358 foreach ((array)$name as $behavior) {
00359 if (in_array($behavior, $this->_attached) && !in_array($behavior, $this->_disabled)) {
00360 $this->_disabled[] = $behavior;
00361 }
00362 }
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 function enabled($name = null) {
00374 if (!empty($name)) {
00375 return (in_array($name, $this->_attached) && !in_array($name, $this->_disabled));
00376 }
00377 return array_diff($this->_attached, $this->_disabled);
00378 }
00379
00380
00381
00382
00383
00384
00385 function dispatchMethod(&$model, $method, $params = array(), $strict = false) {
00386 $methods = array_map('strtolower', array_keys($this->__methods));
00387 $found = (in_array(strtolower($method), $methods));
00388 $call = null;
00389
00390 if ($strict && !$found) {
00391 trigger_error("BehaviorCollection::dispatchMethod() - Method {$method} not found in any attached behavior", E_USER_WARNING);
00392 return null;
00393 } elseif ($found) {
00394 $methods = array_combine($methods, array_values($this->__methods));
00395 $call = $methods[strtolower($method)];
00396 } else {
00397 $count = count($this->__mappedMethods);
00398 $mapped = array_keys($this->__mappedMethods);
00399
00400 for ($i = 0; $i < $count; $i++) {
00401 if (preg_match($mapped[$i] . 'i', $method)) {
00402 $call = $this->__mappedMethods[$mapped[$i]];
00403 array_unshift($params, $method);
00404 break;
00405 }
00406 }
00407 }
00408 if (!empty($call)) {
00409 return $this->{$call[1]}->dispatchMethod($model, $call[0], $params);
00410 }
00411 return array('unhandled');
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 function trigger(&$model, $callback, $params = array(), $options = array()) {
00424 if (empty($this->_attached)) {
00425 return true;
00426 }
00427 $_params = $params;
00428 $options = array_merge(array('break' => false, 'breakOn' => array(null, false), 'modParams' => false), $options);
00429 $count = count($this->_attached);
00430
00431 for ($i = 0; $i < $count; $i++) {
00432 $name = $this->_attached[$i];
00433 if (in_array($name, $this->_disabled)) {
00434 continue;
00435 }
00436 $result = $this->{$name}->dispatchMethod($model, $callback, $params);
00437
00438 if ($options['break'] && ($result === $options['breakOn'] || (is_array($options['breakOn']) && in_array($result, $options['breakOn'], true)))) {
00439 return $result;
00440 } elseif ($options['modParams'] && is_array($result)) {
00441 $params[0] = $result;
00442 }
00443 }
00444 if ($options['modParams'] && isset($params[0])) {
00445 return $params[0];
00446 }
00447 return true;
00448 }
00449
00450
00451
00452
00453
00454
00455 function methods() {
00456 return $this->__methods;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 function attached($name = null) {
00468 if (!empty($name)) {
00469 return (in_array($name, $this->_attached));
00470 }
00471 return $this->_attached;
00472 }
00473 }
00474 ?>