Permalink
Browse files

Cleaning up the ClassRegistry::init() method, removing the use of the…

… second parameter as it always accepted but one value

The second parameter is now $strict, meaning that if the model class is not found it will not try to create an AppModel instance
  • Loading branch information...
1 parent d54e8cd commit 60b44e074a9b64d526e6dba56c3e94c361e4390b @lorenzo lorenzo committed Jul 14, 2011
Showing with 24 additions and 21 deletions.
  1. +8 −0 lib/Cake/Test/Case/Utility/ClassRegistryTest.php
  2. +16 −21 lib/Cake/Utility/ClassRegistry.php
@@ -280,4 +280,12 @@ public function testPluginAppModel() {
$this->assertSame($PluginUser, $PluginUserCopy);
CakePlugin::unload();
}
+
+/**
+ * Tests that passing the string parameter to init() will return false if the model does not exists
+ *
+ */
+ public function testInitStrict() {
+ $this->assertFalse(ClassRegistry::init('NonExistent', true));
+ }
}
@@ -74,33 +74,30 @@ public static function &getInstance() {
* Examples
* Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
*
- * Exapanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
+ * Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
*
* Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
*
* When $class is a numeric keyed array, multiple class instances will be stored in the registry,
* no instance of the object will be returned
* {{{
* array(
- * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
- * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
- * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model')
+ * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
+ * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
+ * array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry')
* );
* }}}
* @param mixed $class as a string or a single key => value array instance will be created,
* stored in the registry and returned.
- * @param string $type Only model is accepted as a valid value for $type.
+ * @param boolean $strict if set to true it will return false if the class was not found instead
+ * of trying to create an AppModel
* @return object instance of ClassName
*/
- public static function &init($class, $type = null) {
+ public static function init($class, $strict = false) {
$_this = ClassRegistry::getInstance();
$false = false;
$true = true;
- if (!$type) {
- $type = 'Model';
- }
-
if (is_array($class)) {
$objects = $class;
if (!isset($class[0])) {
@@ -109,7 +106,7 @@ public static function &init($class, $type = null) {
} else {
$objects = array(array('class' => $class));
}
- $defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array();
+ $defaults = isset($_this->__config['Model']) ? $_this->__config['Model'] : array();
$count = count($objects);
foreach ($objects as $key => $settings) {
@@ -135,12 +132,15 @@ public static function &init($class, $type = null) {
App::uses('Model', 'Model');
App::uses('AppModel', 'Model');
- App::uses($plugin . 'AppModel', $pluginPath . $type);
- App::uses($class, $pluginPath . $type);
+ App::uses($plugin . 'AppModel', $pluginPath . 'Model');
+ App::uses($class, $pluginPath . 'Model');
+
if (class_exists($class)) {
${$class} = new $class($settings);
- } elseif ($type === 'Model') {
- if ($plugin && class_exists($plugin . 'AppModel')) {
+ } else {
+ if ($strict) {
+ return false;
+ } elseif ($plugin && class_exists($plugin . 'AppModel')) {
$appModel = $plugin . 'AppModel';
} else {
$appModel = 'AppModel';
@@ -153,12 +153,7 @@ public static function &init($class, $type = null) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
return $false;
}
-
- if ($type !== 'Model') {
- $_this->addObject($alias, ${$class});
- } else {
- $_this->map($alias, $class);
- }
+ $_this->map($alias, $class);
} elseif (is_numeric($settings)) {
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
return $false;

0 comments on commit 60b44e0

Please sign in to comment.