Beging of an experiment: a class utoloader for cakephp through emulating the keyword "use" of php 5.3

This commit is contained in:
José Lorenzo Rodríguez 2010-12-03 16:54:50 -04:30
parent aa0bad9247
commit da7c53b374
2 changed files with 25 additions and 7 deletions

View file

@ -201,6 +201,12 @@ class App {
*/ */
private static $__objects = array(); private static $__objects = array();
/**
* Holds the location of each class
*
*/
private static $__classMap = array();
/** /**
* Used to read information stored path * Used to read information stored path
* *
@ -430,6 +436,17 @@ class App {
self::$__objects[$type] = $values; self::$__objects[$type] = $values;
} }
public static function uses($className, $location) {
self::$__classMap[$className] = $location;
}
public static function load($className) {
if (isset(self::$__classMap[$className])) {
return App::import(self::$__classMap[$className], $className, false);
}
return false;
}
/** /**
* Finds classes based on $name or specific file(s) to search. Calling App::import() will * Finds classes based on $name or specific file(s) to search. Calling App::import() will
* not construct any classes contained in the files. It will only find and require() the file. * not construct any classes contained in the files. It will only find and require() the file.
@ -887,3 +904,5 @@ class App {
} }
} }
} }
spl_autoload_register(array('App', 'load'));

View file

@ -24,10 +24,12 @@
/** /**
* List of helpers to include * List of helpers to include
*/ */
App::import('Core', 'Router', false); App::uses('Router', 'Core');
App::import('Core', 'CakeRequest', false); App::uses('CakeRequest', 'Core');
App::import('Core', 'CakeResponse', false); App::uses('CakeResponse', 'Core');
App::import('Controller', 'Controller', false); App::uses('Controller', 'Controller');
App::uses('View', 'View');
App::uses('Debugger', 'Core');
/** /**
* Dispatcher converts Requests into controller actions. It uses the dispatched Request * Dispatcher converts Requests into controller actions. It uses the dispatched Request
@ -270,9 +272,6 @@ class Dispatcher {
} }
if (file_exists($filename)) { if (file_exists($filename)) {
if (!class_exists('View')) {
App::import('View', 'View', false);
}
$controller = null; $controller = null;
$view = new View($controller); $view = new View($controller);
return $view->renderCache($filename, microtime(true)); return $view->renderCache($filename, microtime(true));