Skip to content

Commit

Permalink
Starting to change the class loading for app classes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 5, 2010
1 parent 0249518 commit fcd23b0
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 42 deletions.
2 changes: 2 additions & 0 deletions lib/Cake/Controller/PagesController.php
Expand Up @@ -20,6 +20,8 @@
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

App::uses('AppController', 'Controller');

/**
* Static content controller
*
Expand Down
62 changes: 32 additions & 30 deletions lib/Cake/Core/App.php
Expand Up @@ -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
*
Expand All @@ -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];
}

/**
Expand All @@ -234,14 +240,14 @@ public static function path($type) {
*/
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,
Expand All @@ -254,7 +260,7 @@ public static function build($paths = array(), $reset = false) {

if ($reset == true) {
foreach ($paths as $type => $new) {
self::${$type} = (array)$new;
self::$__packages[$type] = (array)$new;
}
return $paths;
}
Expand All @@ -263,27 +269,19 @@ public static function build($paths = array(), $reset = false) {
$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);
}
}
}
Expand Down Expand Up @@ -442,9 +440,15 @@ public static function uses($className, $location) {

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;
Expand Down Expand Up @@ -900,6 +904,4 @@ public static function shutdown() {
Cache::write('object_map', self::$__objects, '_cake_core_');
}
}
}

spl_autoload_register(array('App', 'load'));
}
File renamed without changes.
12 changes: 6 additions & 6 deletions lib/Cake/Routing/Dispatcher.php
Expand Up @@ -223,10 +223,7 @@ protected function _getController($request) {
if (!$ctrlClass) {
return false;
}
$ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) {
return new $ctrlClass($request);
}
return new $ctrlClass($request);
}

/**
Expand All @@ -245,8 +242,11 @@ protected function _loadController($request) {
$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;
Expand Down
13 changes: 8 additions & 5 deletions lib/Cake/Utility/ClassRegistry.php
Expand Up @@ -134,14 +134,17 @@ public static function &init($class, $type = null) {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/View/View.php
Expand Up @@ -799,7 +799,7 @@ protected function _paths($plugin = null, $cached = true) {
return $this->__paths;
}
$paths = array();
$viewPaths = App::path('views');
$viewPaths = App::path('View');
$corePaths = array_flip(App::core('views'));

if (!empty($plugin)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Cake/bootstrap.php
Expand Up @@ -222,6 +222,8 @@
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');
Expand Down

0 comments on commit fcd23b0

Please sign in to comment.