Starting to write a new App::import() that keeps where possible the backwards compatibility

This commit is contained in:
Jose Lorenzo Rodriguez 2011-03-11 00:50:14 -04:30
parent 90b01b7ec3
commit e6b253ec0f
3 changed files with 40 additions and 34 deletions

View file

@ -52,18 +52,18 @@ class App {
* @var array * @var array
*/ */
public static $types = array( public static $types = array(
'class' => array('suffix' => '.php', 'extends' => null, 'core' => true), 'class' => array('extends' => null, 'core' => true),
'file' => array('suffix' => '.php', 'extends' => null, 'core' => true), 'file' => array('extends' => null, 'core' => true),
'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false), 'model' => array('extends' => 'AppModel', 'core' => false),
'behavior' => array('suffix' => '.php', 'extends' => 'ModelBehavior', 'core' => true), 'behavior' => array('extends' => 'ModelBehavior', 'core' => true),
'controller' => array('suffix' => '_controller.php', 'extends' => 'AppController', 'core' => true), 'controller' => array('suffix' => 'Controller', 'extends' => 'AppController', 'core' => true),
'component' => array('suffix' => '.php', 'extends' => null, 'core' => true), 'component' => array('suffix' => 'Component', 'extends' => null, 'core' => true),
'lib' => array('suffix' => '.php', 'extends' => null, 'core' => true), 'lib' => array('extends' => null, 'core' => true),
'view' => array('suffix' => '.php', 'extends' => null, 'core' => true), 'view' => array('suffix' => 'View', 'extends' => null, 'core' => true),
'helper' => array('suffix' => '.php', 'extends' => 'AppHelper', 'core' => true), 'helper' => array('suffix' => 'Helper', 'extends' => 'AppHelper', 'core' => true),
'vendor' => array('suffix' => '', 'extends' => null, 'core' => true), 'vendor' => array('extends' => null, 'core' => true),
'shell' => array('suffix' => '.php', 'extends' => 'Shell', 'core' => true), 'shell' => array('suffix' => 'Shell', 'extends' => 'Shell', 'core' => true),
'plugin' => array('suffix' => '', 'extends' => null, 'core' => true) 'plugin' => array('extends' => null, 'core' => true)
); );
/** /**
@ -525,11 +525,28 @@ class App {
extract($parent, EXTR_OVERWRITE); extract($parent, EXTR_OVERWRITE);
} }
if ($name === null && $file === null) { if ($name == null && $file == null) {
$name = $type; return false;
$type = 'Core'; }
} elseif ($name === null) {
$type = 'File'; $originalType = $type = strtolower($type);
$specialPackage = in_array($type, array('core', 'file', 'vendor'));
if (!$specialPackage && isset(self::$legacy[$type . 's'])) {
$type = self::$legacy[$type . 's'];
}
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;
}
App::uses(Inflector::camelize($name), $plugin . $type);
return (bool) self::load($name);
} }
if (is_array($name)) { if (is_array($name)) {

View file

@ -85,7 +85,8 @@ class CakeTestRunner extends PHPUnit_TextUI_TestRunner {
} }
throw new RuntimeException(__('Could not find fixture manager %s.', $arguments['fixtureManager'])); throw new RuntimeException(__('Could not find fixture manager %s.', $arguments['fixtureManager']));
} }
if (App::import('Lib', 'test_suite/AppFixtureManager')) { App::uses('AppFixtureManager', 'TestSuite');
if (class_exists('AppFixtureManager')) {
return new AppFixtureManager(); return new AppFixtureManager();
} }
return new CakeFixtureManager(); return new CakeFixtureManager();

View file

@ -352,9 +352,6 @@ class AppImportTest extends CakeTestCase {
* @return void * @return void
*/ */
function testClassLoading() { function testClassLoading() {
$file = App::import();
$this->assertTrue($file);
$file = App::import('Model', 'Model', false); $file = App::import('Model', 'Model', false);
$this->assertTrue($file); $this->assertTrue($file);
$this->assertTrue(class_exists('Model')); $this->assertTrue(class_exists('Model'));
@ -363,15 +360,15 @@ class AppImportTest extends CakeTestCase {
$this->assertTrue($file); $this->assertTrue($file);
$this->assertTrue(class_exists('Controller')); $this->assertTrue(class_exists('Controller'));
$file = App::import('Component', 'Component', false); $file = App::import('Component', 'Auth', false);
$this->assertTrue($file); $this->assertTrue($file);
$this->assertTrue(class_exists('Component')); $this->assertTrue(class_exists('AuthComponent'));
$file = App::import('Shell', 'Shell', false); $file = App::import('Shell', 'Shell', false);
$this->assertTrue($file); $this->assertTrue($file);
$this->assertTrue(class_exists('Shell')); $this->assertTrue(class_exists('Shell'));
$file = App::import('Lib', 'config/PhpReader'); $file = App::import('Configure', 'PhpReader');
$this->assertTrue($file); $this->assertTrue($file);
$this->assertTrue(class_exists('PhpReader')); $this->assertTrue(class_exists('PhpReader'));
@ -383,23 +380,14 @@ class AppImportTest extends CakeTestCase {
$this->assertTrue(class_exists('AppModel')); $this->assertTrue(class_exists('AppModel'));
$file = App::import('WrongType', null, true, array(), ''); $file = App::import('WrongType', null, true, array(), '');
$this->assertTrue($file); $this->assertFalse($file);
$file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false); $file = App::import('Model', 'NonExistingPlugin.NonExistingModel', false);
$this->assertFalse($file); $this->assertFalse($file);
$file = App::import('Core', 'NonExistingPlugin.NonExistingModel', false);
$this->assertFalse($file);
$file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false); $file = App::import('Model', array('NonExistingPlugin.NonExistingModel'), false);
$this->assertFalse($file); $this->assertFalse($file);
$file = App::import('Core', array('NonExistingPlugin.NonExistingModel'), false);
$this->assertFalse($file);
$file = App::import('Core', array('NonExistingPlugin.NonExistingModel.AnotherChild'), false);
$this->assertFalse($file);
if (!class_exists('AppController')) { if (!class_exists('AppController')) {
$classes = array_flip(get_declared_classes()); $classes = array_flip(get_declared_classes());