diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index 91260321deb..c7999a89c1f 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -543,10 +543,9 @@ public static function import($type = null, $name = null, $parent = true, $searc if (!$specialPackage && isset(self::$legacy[$type . 's'])) { $type = self::$legacy[$type . 's']; } + list($plugin, $name) = pluginSplit($name); if (!$specialPackage) { - list($plugin, $name) = pluginSplit($name, true); - if ($type == 'Console/Command' && $name == 'Shell') { $type = 'Console'; } else if (isset(self::$types[$originalType]['suffix'])) { @@ -554,19 +553,47 @@ public static function import($type = null, $name = null, $parent = true, $searc $name .= ($suffix == $name) ? '' : $suffix; } - if (isset(self::$types[$originalType]['extends'])) { + if ($parent && isset(self::$types[$originalType]['extends'])) { $extends = self::$types[$originalType]['extends']; App::uses($extends, $type); if ($plugin && in_array($originalType, array('controller', 'model'))) { - $pluginName = substr($plugin, 0 , -1); - App::uses($pluginName . $extends, $plugin . $type); + App::uses($plugin . $extends, $plugin . '.' .$type); } } - + if ($plugin) { + $plugin .= '.'; + } App::uses(Inflector::camelize($name), $plugin . $type); return (bool) self::load($name); } + if ($type == 'file' && !empty($file)) { + $mapped = self::__mapped($name, $plugin); + if ($mapped) { + $file = $mapped; + } else if (!empty($search)) { + foreach ($search as $path) { + $found = false; + if (file_exists($path . $file)) { + $file = $path . $file; + $found = true; + break; + } + if (empty($found)) { + $file = false; + } + } + } + if (!empty($file) && file_exists($file)) { + self::__map($file, $name, $plugin); + $returnValue = include $file; + if ($return) { + return $returnValue; + } + return (bool) $returnValue; + } + } + return false; if ($name != null && strpos($name, '.') !== false) { diff --git a/lib/Cake/tests/cases/libs/app.test.php b/lib/Cake/tests/cases/libs/app.test.php index cece9d3c9e4..d2793cf1652 100644 --- a/lib/Cake/tests/cases/libs/app.test.php +++ b/lib/Cake/tests/cases/libs/app.test.php @@ -547,7 +547,7 @@ function testFileLoadingReturnValue () { * @return void */ function testLoadingWithSearch () { - $file = App::import('File', 'NewName', false, array(LIBS ), 'config.php'); + $file = App::import('File', 'NewName', false, array(LIBS . 'config' . DS), 'config.php'); $this->assertTrue($file); $file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php'); @@ -560,12 +560,24 @@ function testLoadingWithSearch () { * @access public * @return void */ - function testLoadingWithSearchArray () { - $type = array('type' => 'File', 'name' => 'RandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS )); + function testLoadingWithSearchArray() { + $type = array( + 'type' => 'File', + 'name' => 'RandomName', + 'parent' => false, + 'file' => 'config.php', + 'search' => array(LIBS . 'config' . DS) + ); $file = App::import($type); $this->assertTrue($file); - $type = array('type' => 'File', 'name' => 'AnotherRandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS)); + $type = array( + 'type' => 'File', + 'name' => 'AnotherRandomName', + 'parent' => false, + 'file' => 'config.php', + 'search' => array(LIBS) + ); $file = App::import($type); $this->assertFalse($file); } @@ -577,28 +589,24 @@ function testLoadingWithSearchArray () { * @return void */ function testMultipleLoading() { - if (class_exists('I18n', false) || class_exists('CakeSocket', false)) { + if (class_exists('PersisterOne', false) || class_exists('PersisterTwo', false)) { $this->markTestSkipped('Cannot test loading of classes that exist.'); } - $toLoad = array('I18n', 'CakeSocket'); - - $classes = array_flip(get_declared_classes()); - $this->assertFalse(isset($classes['i18n'])); - $this->assertFalse(isset($classes['CakeSocket'])); - - $load = App::import($toLoad); + App::build(array( + 'Model' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS) + )); + $toLoad = array('PersisterOne', 'PersisterTwo'); + $load = App::import('Model', $toLoad); $this->assertTrue($load); $classes = array_flip(get_declared_classes()); - $this->assertTrue(isset($classes['I18n'])); + $this->assertTrue(isset($classes['PersisterOne'])); + $this->assertTrue(isset($classes['PersisterTwo'])); - $load = App::import(array('I18n', 'SomeNotFoundClass', 'CakeSocket')); + $load = App::import('Model', array('PersisterOne', 'SomeNotFoundClass', 'PersisterTwo')); $this->assertFalse($load); - - $load = App::import($toLoad); - $this->assertTrue($load); } /**