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 class ClassRegistry {
00038
00039
00040
00041
00042
00043
00044 var $__objects = array();
00045
00046
00047
00048
00049
00050
00051 var $__map = array();
00052
00053
00054
00055
00056
00057
00058 var $__config = array();
00059
00060
00061
00062
00063
00064
00065 function &getInstance() {
00066 static $instance = array();
00067 if (!$instance) {
00068 $instance[0] =& new ClassRegistry();
00069 }
00070 return $instance[0];
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 function &init($class, $type = null) {
00099 $_this =& ClassRegistry::getInstance();
00100 $id = $false = false;
00101 $true = true;
00102
00103 if (!$type) {
00104 $type = 'Model';
00105 }
00106
00107 if (is_array($class)) {
00108 $objects = $class;
00109 if (!isset($class[0])) {
00110 $objects = array($class);
00111 }
00112 } else {
00113 $objects = array(array('class' => $class));
00114 }
00115 $defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array();
00116 $count = count($objects);
00117
00118 foreach ($objects as $key => $settings) {
00119 if (is_array($settings)) {
00120 $plugin = $pluginPath = null;
00121 $settings = array_merge($defaults, $settings);
00122 $class = $settings['class'];
00123
00124 if (strpos($class, '.') !== false) {
00125 list($plugin, $class) = explode('.', $class);
00126 $pluginPath = $plugin . '.';
00127 }
00128
00129 if (empty($settings['alias'])) {
00130 $settings['alias'] = $class;
00131 }
00132 $alias = $settings['alias'];
00133
00134 if ($model =& $_this->__duplicate($alias, $class)) {
00135 $_this->map($alias, $class);
00136 return $model;
00137 }
00138
00139 if (class_exists($class) || App::import($type, $pluginPath . $class)) {
00140 ${$class} =& new $class($settings);
00141 } elseif ($type === 'Model') {
00142 if ($plugin && class_exists($plugin . 'AppModel')) {
00143 $appModel = $plugin . 'AppModel';
00144 } else {
00145 $appModel = 'AppModel';
00146 }
00147 $settings['name'] = $class;
00148 ${$class} =& new $appModel($settings);
00149 }
00150
00151 if (!isset(${$class})) {
00152 trigger_error(sprintf(__('(ClassRegistry::init() could not create instance of %1$s class %2$s ', true), $class, $type), E_USER_WARNING);
00153 return $false;
00154 }
00155
00156 if ($type !== 'Model') {
00157 $_this->addObject($alias, ${$class});
00158 } else {
00159 $_this->map($alias, $class);
00160 }
00161 } elseif (is_numeric($settings)) {
00162 trigger_error(__('(ClassRegistry::init() Attempted to create instance of a class with a numeric name', true), E_USER_WARNING);
00163 return $false;
00164 }
00165 }
00166
00167 if ($count > 1) {
00168 return $true;
00169 }
00170 return ${$class};
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 function addObject($key, &$object) {
00182 $_this =& ClassRegistry::getInstance();
00183 $key = Inflector::underscore($key);
00184 if (!isset($_this->__objects[$key])) {
00185 $_this->__objects[$key] =& $object;
00186 return true;
00187 }
00188 return false;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198 function removeObject($key) {
00199 $_this =& ClassRegistry::getInstance();
00200 $key = Inflector::underscore($key);
00201 if (isset($_this->__objects[$key])) {
00202 unset($_this->__objects[$key]);
00203 }
00204 }
00205
00206
00207
00208
00209
00210
00211
00212
00213 function isKeySet($key) {
00214 $_this =& ClassRegistry::getInstance();
00215 $key = Inflector::underscore($key);
00216 if (isset($_this->__objects[$key])) {
00217 return true;
00218 } elseif (isset($_this->__map[$key])) {
00219 return true;
00220 }
00221 return false;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230 function keys() {
00231 $_this =& ClassRegistry::getInstance();
00232 return array_keys($_this->__objects);
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242 function &getObject($key) {
00243 $_this =& ClassRegistry::getInstance();
00244 $key = Inflector::underscore($key);
00245 $return = false;
00246 if (isset($_this->__objects[$key])) {
00247 $return =& $_this->__objects[$key];
00248 } else {
00249 $key = $_this->__getMap($key);
00250 if (isset($_this->__objects[$key])) {
00251 $return =& $_this->__objects[$key];
00252 }
00253 }
00254 return $return;
00255 }
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 function config($type, $param = array()) {
00268 $_this =& ClassRegistry::getInstance();
00269
00270 if (empty($param) && is_array($type)) {
00271 $param = $type;
00272 $type = 'Model';
00273 } elseif (is_null($param)) {
00274 unset($_this->__config[$type]);
00275 } elseif (empty($param) && is_string($type)) {
00276 return isset($_this->__config[$type]) ? $_this->__config[$type] : null;
00277 }
00278 $_this->__config[$type] = $param;
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 function &__duplicate($alias, $class) {
00290 $duplicate = false;
00291 if ($this->isKeySet($alias)) {
00292 $model =& $this->getObject($alias);
00293 if (is_object($model) && (is_a($model, $class) || $model->alias === $class)) {
00294 $duplicate =& $model;
00295 }
00296 unset($model);
00297 }
00298 return $duplicate;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307
00308 function map($key, $name) {
00309 $_this =& ClassRegistry::getInstance();
00310 $key = Inflector::underscore($key);
00311 $name = Inflector::underscore($name);
00312 if (!isset($_this->__map[$key])) {
00313 $_this->__map[$key] = $name;
00314 }
00315 }
00316
00317
00318
00319
00320
00321
00322
00323 function mapKeys() {
00324 $_this =& ClassRegistry::getInstance();
00325 return array_keys($_this->__map);
00326 }
00327
00328
00329
00330
00331
00332
00333
00334
00335 function __getMap($key) {
00336 if (isset($this->__map[$key])) {
00337 return $this->__map[$key];
00338 }
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 function flush() {
00348 $_this =& ClassRegistry::getInstance();
00349 $_this->__objects = array();
00350 $_this->__map = array();
00351 }
00352 }
00353 ?>