Starting to change the class loading for app classes

This commit is contained in:
José Lorenzo Rodríguez 2010-12-05 10:54:42 -04:30
parent 02495188ef
commit fcd23b0978
7 changed files with 51 additions and 42 deletions

View file

@ -20,6 +20,8 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('AppController', 'Controller');
/**
* Static content controller
*

View file

@ -207,6 +207,12 @@ class App {
*/
private static $__classMap = array();
/**
* Holds the possible paths for each package name
*
*/
private static $__packages = array();
/**
* Used to read information stored path
*
@ -218,10 +224,10 @@ class App {
* @return string array
*/
public static function path($type) {
if (!isset(self::${$type})) {
if (!isset(self::$__packages[$type])) {
return array();
}
return self::${$type};
return self::$__packages[$type];
}
/**
@ -234,14 +240,14 @@ class App {
*/
public static function build($paths = array(), $reset = false) {
$defaults = array(
'models' => array(MODELS),
'behaviors' => array(BEHAVIORS),
'datasources' => array(MODELS . 'datasources'),
'controllers' => array(CONTROLLERS),
'components' => array(COMPONENTS),
'Model' => array(MODELS),
'Model/Behavior' => array(BEHAVIORS),
'Datasource' => array(MODELS . 'datasources'),
'Controller' => array(CONTROLLERS),
'Controller/Component' => array(COMPONENTS),
'libs' => array(APPLIBS),
'views' => array(VIEWS),
'helpers' => array(HELPERS),
'View' => array(VIEWS),
'View/Helper' => array(HELPERS),
'locales' => array(APP . 'locale' . DS),
'shells' => array(
APP . 'console' . DS . 'shells' . DS,
@ -254,7 +260,7 @@ class App {
if ($reset == true) {
foreach ($paths as $type => $new) {
self::${$type} = (array)$new;
self::$__packages[$type] = (array)$new;
}
return $paths;
}
@ -263,27 +269,19 @@ class App {
$app = array('models' => true, 'controllers' => true, 'helpers' => true);
foreach ($defaults as $type => $default) {
$merge = array();
if (isset($app[$type])) {
$merge = array(APP);
}
if (isset($core[$type])) {
$merge = array_merge($merge, (array)$core[$type]);
}
if (empty(self::${$type}) || empty($paths)) {
self::${$type} = $default;
if (empty(self::$__packages[$type]) || empty($paths)) {
self::$__packages[$type] = $default;
}
if (!empty($paths[$type])) {
$path = array_flip(array_flip(array_merge(
(array)$paths[$type], self::${$type}, $merge
(array)$paths[$type], self::$__packages[$type], $merge
)));
self::${$type} = array_values($path);
self::$__packages[$type] = array_values($path);
} else {
$path = array_flip(array_flip(array_merge(self::${$type}, $merge)));
self::${$type} = array_values($path);
$path = array_flip(array_flip(self::$__packages[$type]));
self::$__packages[$type] = array_values($path);
}
}
}
@ -442,9 +440,15 @@ class App {
public static function load($className) {
if (isset(self::$__classMap[$className])) {
$file = LIBS . self::$__classMap[$className] . DS . $className . '.php';
if (file_exists($file)) {
return include $file;
$package = self::$__classMap[$className];
$paths = self::path($package);
$paths[] = LIBS . self::$__classMap[$className] . DS;
foreach ($paths as $path) {
$file = $path . $className . '.php';
if (file_exists($file)) {
return include $file;
}
}
}
return false;
@ -901,5 +905,3 @@ class App {
}
}
}
spl_autoload_register(array('App', 'load'));

View file

@ -223,10 +223,7 @@ class Dispatcher {
if (!$ctrlClass) {
return false;
}
$ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) {
return new $ctrlClass($request);
}
return new $ctrlClass($request);
}
/**
@ -245,8 +242,11 @@ class Dispatcher {
$controller = Inflector::camelize($request->params['controller']);
}
if ($pluginPath . $controller) {
if (App::import('Controller', $pluginPath . $controller)) {
return $controller;
$class = $controller . 'Controller';
App::uses('AppController', 'Controller');
App::uses($class, $pluginPath . 'Controller');
if (class_exists($class)) {
return $class;
}
}
return false;

View file

@ -134,14 +134,17 @@ class ClassRegistry {
return $model;
}
if (class_exists($class) || App::import($type, $pluginPath . $class)) {
App::uses('Model', 'Model');
App::uses('AppModel', 'Model');
App::uses($class, $pluginPath . $type);
if (class_exists($class)) {
${$class} = new $class($settings);
} elseif ($type === 'Model') {
if ($plugin && class_exists($plugin . 'AppModel')) {
$appModel = $plugin . 'AppModel';
} else {
//if ($plugin && class_exists($plugin . 'AppModel')) {
// $appModel = $plugin . 'AppModel';
//} else {
$appModel = 'AppModel';
}
//}
$settings['name'] = $class;
${$class} = new $appModel($settings);
}

View file

@ -799,7 +799,7 @@ class View extends Object {
return $this->__paths;
}
$paths = array();
$viewPaths = App::path('views');
$viewPaths = App::path('View');
$corePaths = array_flip(App::core('views'));
if (!empty($plugin)) {

View file

@ -222,6 +222,8 @@ require LIBS . 'basics.php';
require LIBS . 'Core' . DS .'App.php';
require LIBS . 'Error' . DS . 'exceptions.php';
spl_autoload_register(array('App', 'load'));
App::uses('ErrorHandler', 'Error');
App::uses('Configure', 'Core');
App::uses('Cache', 'Cache');