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