Skip to content

Commit

Permalink
Re-implmenting file loading in App::import(), search it is not longer…
Browse files Browse the repository at this point in the history
… recursive... if it ever was
  • Loading branch information
lorenzo committed Mar 11, 2011
1 parent 6ac87ee commit 5a57f2c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
39 changes: 33 additions & 6 deletions lib/Cake/Core/App.php
Expand Up @@ -543,30 +543,57 @@ 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'])) {
$suffix = self::$types[$originalType]['suffix'];
$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) {
Expand Down
42 changes: 25 additions & 17 deletions lib/Cake/tests/cases/libs/app.test.php
Expand Up @@ -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');
Expand All @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 5a57f2c

Please sign in to comment.