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