Re-implmenting file loading in App::import(), search it is not longer recursive... if it ever was

This commit is contained in:
Jose Lorenzo Rodriguez 2011-03-11 18:56:32 -04:30
parent 6ac87eef68
commit 5a57f2c3c3
2 changed files with 58 additions and 23 deletions

View file

@ -543,10 +543,9 @@ class App {
if (!$specialPackage && isset(self::$legacy[$type . 's'])) { if (!$specialPackage && isset(self::$legacy[$type . 's'])) {
$type = self::$legacy[$type . 's']; $type = self::$legacy[$type . 's'];
} }
list($plugin, $name) = pluginSplit($name);
if (!$specialPackage) { if (!$specialPackage) {
list($plugin, $name) = pluginSplit($name, true);
if ($type == 'Console/Command' && $name == 'Shell') { if ($type == 'Console/Command' && $name == 'Shell') {
$type = 'Console'; $type = 'Console';
} else if (isset(self::$types[$originalType]['suffix'])) { } else if (isset(self::$types[$originalType]['suffix'])) {
@ -554,19 +553,47 @@ class App {
$name .= ($suffix == $name) ? '' : $suffix; $name .= ($suffix == $name) ? '' : $suffix;
} }
if (isset(self::$types[$originalType]['extends'])) { if ($parent && isset(self::$types[$originalType]['extends'])) {
$extends = self::$types[$originalType]['extends']; $extends = self::$types[$originalType]['extends'];
App::uses($extends, $type); App::uses($extends, $type);
if ($plugin && in_array($originalType, array('controller', 'model'))) { if ($plugin && in_array($originalType, array('controller', 'model'))) {
$pluginName = substr($plugin, 0 , -1); App::uses($plugin . $extends, $plugin . '.' .$type);
App::uses($pluginName . $extends, $plugin . $type);
} }
} }
if ($plugin) {
$plugin .= '.';
}
App::uses(Inflector::camelize($name), $plugin . $type); App::uses(Inflector::camelize($name), $plugin . $type);
return (bool) self::load($name); 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; return false;
if ($name != null && strpos($name, '.') !== false) { if ($name != null && strpos($name, '.') !== false) {

View file

@ -547,7 +547,7 @@ class AppImportTest extends CakeTestCase {
* @return void * @return void
*/ */
function testLoadingWithSearch () { 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); $this->assertTrue($file);
$file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php'); $file = App::import('File', 'AnotherNewName', false, array(LIBS), 'config.php');
@ -560,12 +560,24 @@ class AppImportTest extends CakeTestCase {
* @access public * @access public
* @return void * @return void
*/ */
function testLoadingWithSearchArray () { function testLoadingWithSearchArray() {
$type = array('type' => 'File', 'name' => 'RandomName', 'parent' => false, 'file' => 'config.php', 'search' => array(LIBS )); $type = array(
'type' => 'File',
'name' => 'RandomName',
'parent' => false,
'file' => 'config.php',
'search' => array(LIBS . 'config' . DS)
);
$file = App::import($type); $file = App::import($type);
$this->assertTrue($file); $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); $file = App::import($type);
$this->assertFalse($file); $this->assertFalse($file);
} }
@ -577,28 +589,24 @@ class AppImportTest extends CakeTestCase {
* @return void * @return void
*/ */
function testMultipleLoading() { 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.'); $this->markTestSkipped('Cannot test loading of classes that exist.');
} }
$toLoad = array('I18n', 'CakeSocket'); App::build(array(
'Model' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'models' . DS)
$classes = array_flip(get_declared_classes()); ));
$this->assertFalse(isset($classes['i18n'])); $toLoad = array('PersisterOne', 'PersisterTwo');
$this->assertFalse(isset($classes['CakeSocket'])); $load = App::import('Model', $toLoad);
$load = App::import($toLoad);
$this->assertTrue($load); $this->assertTrue($load);
$classes = array_flip(get_declared_classes()); $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); $this->assertFalse($load);
$load = App::import($toLoad);
$this->assertTrue($load);
} }
/** /**