Initial port of App to a static class, one test is failing.

This commit is contained in:
mark_story 2010-07-10 00:48:45 -04:00
parent 0e2a313145
commit f202f89974

View file

@ -399,15 +399,14 @@ class Configure {
* @package cake
* @subpackage cake.cake.libs
*/
class App extends Object {
class App {
/**
* List of object types and their properties
*
* @var array
* @access public
*/
public $types = array(
public static $types = array(
'class' => array('suffix' => '.php', 'extends' => null, 'core' => true),
'file' => array('suffix' => '.php', 'extends' => null, 'core' => true),
'model' => array('suffix' => '.php', 'extends' => 'AppModel', 'core' => false),
@ -426,152 +425,134 @@ class App extends Object {
* List of additional path(s) where model files reside.
*
* @var array
* @access public
*/
public $models = array();
public static $models = array();
/**
* List of additional path(s) where behavior files reside.
*
* @var array
* @access public
*/
public $behaviors = array();
public static $behaviors = array();
/**
* List of additional path(s) where controller files reside.
*
* @var array
* @access public
*/
public $controllers = array();
public static $controllers = array();
/**
* List of additional path(s) where component files reside.
*
* @var array
* @access public
*/
public $components = array();
public static $components = array();
/**
* List of additional path(s) where datasource files reside.
*
* @var array
* @access public
*/
public $datasources = array();
public static $datasources = array();
/**
* List of additional path(s) where libs files reside.
*
* @var array
* @access public
*/
public $libs = array();
public static $libs = array();
/**
* List of additional path(s) where view files reside.
*
* @var array
* @access public
*/
public $views = array();
public static $views = array();
/**
* List of additional path(s) where helper files reside.
*
* @var array
* @access public
*/
public $helpers = array();
public static $helpers = array();
/**
* List of additional path(s) where plugins reside.
*
* @var array
* @access public
*/
public $plugins = array();
public static $plugins = array();
/**
* List of additional path(s) where vendor packages reside.
*
* @var array
* @access public
*/
public $vendors = array();
public static $vendors = array();
/**
* List of additional path(s) where locale files reside.
*
* @var array
* @access public
*/
public $locales = array();
public static $locales = array();
/**
* List of additional path(s) where console shell files reside.
*
* @var array
* @access public
*/
public $shells = array();
public static $shells = array();
/**
* Paths to search for files.
*
* @var array
* @access public
*/
public $search = array();
public static $search = array();
/**
* Whether or not to return the file that is loaded.
*
* @var boolean
* @access public
*/
public $return = false;
public static $return = false;
/**
* Determines if $__maps and $__paths cache should be written.
*
* @var boolean
* @access private
*/
private $__cache = false;
private static $__cache = false;
/**
* Holds key/value pairs of $type => file path.
*
* @var array
* @access private
*/
private $__map = array();
private static $__map = array();
/**
* Holds paths for deep searching of files.
*
* @var array
* @access private
*/
private $__paths = array();
private static $__paths = array();
/**
* Holds loaded files.
*
* @var array
* @access private
*/
private $__loaded = array();
private static $__loaded = array();
/**
* Holds and key => value array of object types.
*
* @var array
* @access private
*/
private $__objects = array();
private static $__objects = array();
/**
* Used to read information stored path
@ -584,11 +565,10 @@ class App extends Object {
* @return string array
*/
public static function path($type) {
$_this = App::getInstance();
if (!isset($_this->{$type})) {
if (!isset(self::${$type})) {
return array();
}
return $_this->{$type};
return self::${$type};
}
/**
@ -600,7 +580,6 @@ class App extends Object {
* @return void
*/
public static function build($paths = array(), $reset = false) {
$_this = App::getInstance();
$defaults = array(
'models' => array(MODELS),
'behaviors' => array(BEHAVIORS),
@ -618,12 +597,12 @@ class App extends Object {
if ($reset == true) {
foreach ($paths as $type => $new) {
$_this->{$type} = (array)$new;
self::${$type} = (array)$new;
}
return $paths;
}
$core = $_this->core();
$core = self::core();
$app = array('models' => true, 'controllers' => true, 'helpers' => true);
foreach ($defaults as $type => $default) {
@ -637,17 +616,17 @@ class App extends Object {
}
if (empty($_this->{$type}) || empty($paths)) {
$_this->{$type} = $default;
self::${$type} = $default;
}
if (!empty($paths[$type])) {
$path = array_flip(array_flip(array_merge(
(array)$paths[$type], $_this->{$type}, $merge
(array)$paths[$type], self::${$type}, $merge
)));
$_this->{$type} = array_values($path);
self::${$type} = array_values($path);
} else {
$path = array_flip(array_flip(array_merge($_this->{$type}, $merge)));
$_this->{$type} = array_values($path);
$path = array_flip(array_flip(array_merge(self::${$type}, $merge)));
self::${$type} = array_values($path);
}
}
}
@ -659,14 +638,13 @@ class App extends Object {
* @return string full path to the plugin.
*/
public static function pluginPath($plugin) {
$_this = App::getInstance();
$pluginDir = Inflector::underscore($plugin);
for ($i = 0, $length = count($_this->plugins); $i < $length; $i++) {
if (is_dir($_this->plugins[$i] . $pluginDir)) {
return $_this->plugins[$i] . $pluginDir . DS ;
for ($i = 0, $length = count(self::$plugins); $i < $length; $i++) {
if (is_dir(self::$plugins[$i] . $pluginDir)) {
return self::$plugins[$i] . $pluginDir . DS ;
}
}
return $_this->plugins[0] . $pluginDir . DS;
return self::$plugins[0] . $pluginDir . DS;
}
/**
@ -676,14 +654,13 @@ class App extends Object {
* @return string full path to the theme.
*/
public static function themePath($theme) {
$_this = App::getInstance();
$themeDir = 'themed' . DS . Inflector::underscore($theme);
for ($i = 0, $length = count($_this->views); $i < $length; $i++) {
if (is_dir($_this->views[$i] . $themeDir)) {
return $_this->views[$i] . $themeDir . DS ;
for ($i = 0, $length = count(self::$views); $i < $length; $i++) {
if (is_dir(self::$views[$i] . $themeDir)) {
return self::$views[$i] . $themeDir . DS ;
}
}
return $_this->views[0] . $themeDir . DS;
return self::$views[0] . $themeDir . DS;
}
/**
@ -750,14 +727,13 @@ class App extends Object {
$extension = true;
$name = $type . str_replace(DS, '', $path);
}
$_this = App::getInstance();
if (empty($_this->__objects) && $cache === true) {
$_this->__objects = Cache::read('object_map', '_cake_core_');
if (empty(self::$__objects) && $cache === true) {
self::$__objects = Cache::read('object_map', '_cake_core_');
}
if (!isset($_this->__objects[$name]) || $cache !== true) {
$types = $_this->types;
if (!isset(self::$__objects[$name]) || $cache !== true) {
$types = self::$types;
if (!isset($types[$type])) {
return false;
@ -765,7 +741,7 @@ class App extends Object {
$objects = array();
if (empty($path)) {
$path = $_this->{"{$type}s"};
$path = self::${"{$type}s"};
if (isset($types[$type]['core']) && $types[$type]['core'] === false) {
array_pop($path);
}
@ -774,7 +750,7 @@ class App extends Object {
foreach ((array)$path as $dir) {
if ($dir != APP) {
$items = $_this->__list($dir, $types[$type]['suffix'], $extension);
$items = self::__list($dir, $types[$type]['suffix'], $extension);
$objects = array_merge($items, array_diff($objects, $items));
}
}
@ -786,12 +762,12 @@ class App extends Object {
}
if ($cache === true) {
$_this->__cache = true;
self::$__cache = true;
}
$_this->__objects[$name] = $objects;
self::$__objects[$name] = $objects;
}
return $_this->__objects[$name];
return self::$__objects[$name];
}
/**
@ -803,8 +779,7 @@ class App extends Object {
* @return void
*/
public static function setObjects($type, $values) {
$_this = App::getInstance();
$_this->__objects[$type] = $values;
self::$__objects[$type] = $values;
}
/**
@ -875,33 +850,32 @@ class App extends Object {
list($plugin, $name) = explode('.', $name);
$plugin = Inflector::camelize($plugin);
}
$_this = App::getInstance();
$_this->return = $return;
self::$return = $return;
if (isset($ext)) {
$file = Inflector::underscore($name) . ".{$ext}";
}
$ext = $_this->__settings($type, $plugin, $parent);
$ext = self::__settings($type, $plugin, $parent);
if ($name != null && !class_exists($name . $ext['class'])) {
if ($load = $_this->__mapped($name . $ext['class'], $type, $plugin)) {
if ($_this->__load($load)) {
$_this->__overload($type, $name . $ext['class'], $parent);
if ($load = self::__mapped($name . $ext['class'], $type, $plugin)) {
if (self::__load($load)) {
self::__overload($type, $name . $ext['class'], $parent);
if ($_this->return) {
if (self::$return) {
return include($load);
}
return true;
} else {
$_this->__remove($name . $ext['class'], $type, $plugin);
$_this->__cache = true;
self::__remove($name . $ext['class'], $type, $plugin);
self::$__cache = true;
}
}
if (!empty($search)) {
$_this->search = $search;
self::$search = $search;
} elseif ($plugin) {
$_this->search = $_this->__paths('plugin');
self::$search = self::__paths('plugin');
} else {
$_this->search = $_this->__paths($type);
self::$search = self::__paths($type);
}
$find = $file;
@ -909,26 +883,26 @@ class App extends Object {
$find = Inflector::underscore($name . $ext['suffix']).'.php';
if ($plugin) {
$paths = $_this->search;
$paths = self::$search;
foreach ($paths as $key => $value) {
$_this->search[$key] = $value . $ext['path'];
self::$search[$key] = $value . $ext['path'];
}
}
}
if (strtolower($type) !== 'vendor' && empty($search) && $_this->__load($file)) {
if (strtolower($type) !== 'vendor' && empty($search) && self::__load($file)) {
$directory = false;
} else {
$file = $find;
$directory = $_this->__find($find, true);
$directory = self::__find($find, true);
}
if ($directory !== null) {
$_this->__cache = true;
$_this->__map($directory . $file, $name . $ext['class'], $type, $plugin);
$_this->__overload($type, $name . $ext['class'], $parent);
self::$__cache = true;
self::__map($directory . $file, $name . $ext['class'], $type, $plugin);
self::__overload($type, $name . $ext['class'], $parent);
if ($_this->return) {
if (self::$return) {
return include($directory . $file);
}
return true;
@ -939,17 +913,13 @@ class App extends Object {
}
/**
* Returns a single instance of App.
* Initializes the cache for App, registers a shutdown function.
*
* @return object
* @return void
*/
public static function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new App();
$instance[0]->__map = (array)Cache::read('file_map', '_cake_core_');
}
return $instance[0];
public static function init() {
self::$__map = (array)Cache::read('file_map', '_cake_core_');
register_shutdown_function(array('App', 'shutdown'));
}
/**
@ -958,22 +928,21 @@ class App extends Object {
* @param string $file full file name
* @param boolean $recursive search $__paths recursively
* @return mixed boolean on fail, $file directory path on success
* @access private
*/
private function __find($file, $recursive = true) {
private static function __find($file, $recursive = true) {
static $appPath = false;
if (empty($this->search)) {
if (empty(self::$search)) {
return null;
} elseif (is_string($this->search)) {
$this->search = array($this->search);
} elseif (is_string(self::$search)) {
$this->search = array(self::$search);
}
if (empty($this->__paths)) {
$this->__paths = Cache::read('dir_map', '_cake_core_');
if (empty(self::$__paths)) {
self::$__paths = Cache::read('dir_map', '_cake_core_');
}
foreach ($this->search as $path) {
foreach (self::$search as $path) {
if ($appPath === false) {
$appPath = rtrim(APP, DS);
}
@ -983,24 +952,24 @@ class App extends Object {
$recursive = false;
}
if ($recursive === false) {
if ($this->__load($path . DS . $file)) {
if (self::__load($path . DS . $file)) {
return $path . DS;
}
continue;
}
if (!isset($this->__paths[$path])) {
if (!isset(self::$__paths[$path])) {
if (!class_exists('Folder')) {
require LIBS . 'folder.php';
}
$Folder =& new Folder();
$directories = $Folder->tree($path, array('.svn', '.git', 'CVS', 'tests', 'templates'), 'dir');
sort($directories);
$this->__paths[$path] = $directories;
self::$__paths[$path] = $directories;
}
foreach ($this->__paths[$path] as $directory) {
if ($this->__load($directory . DS . $file)) {
foreach (self::$__paths[$path] as $directory) {
if (self::__load($directory . DS . $file)) {
return $directory . DS;
}
}
@ -1015,17 +984,17 @@ class App extends Object {
* @return boolean
* @access private
*/
private function __load($file) {
private static function __load($file) {
if (empty($file)) {
return false;
}
if (!$this->return && isset($this->__loaded[$file])) {
if (!self::$return && isset(self::$__loaded[$file])) {
return true;
}
if (file_exists($file)) {
if (!$this->return) {
if (!self::$return) {
require($file);
$this->__loaded[$file] = true;
self::$__loaded[$file] = true;
}
return true;
}
@ -1042,11 +1011,11 @@ class App extends Object {
* @return void
* @access private
*/
private function __map($file, $name, $type, $plugin) {
private static function __map($file, $name, $type, $plugin) {
if ($plugin) {
$this->__map['Plugin'][$plugin][$type][$name] = $file;
self::$__map['Plugin'][$plugin][$type][$name] = $file;
} else {
$this->__map[$type][$name] = $file;
self::$__map[$type][$name] = $file;
}
}
@ -1059,16 +1028,16 @@ class App extends Object {
* @return mixed, file path if found, false otherwise
* @access private
*/
private function __mapped($name, $type, $plugin) {
private static function __mapped($name, $type, $plugin) {
if ($plugin) {
if (isset($this->__map['Plugin'][$plugin][$type]) && isset($this->__map['Plugin'][$plugin][$type][$name])) {
return $this->__map['Plugin'][$plugin][$type][$name];
if (isset(self::$__map['Plugin'][$plugin][$type]) && isset(self::$__map['Plugin'][$plugin][$type][$name])) {
return self::$__map['Plugin'][$plugin][$type][$name];
}
return false;
}
if (isset($this->__map[$type]) && isset($this->__map[$type][$name])) {
return $this->__map[$type][$name];
if (isset(self::$__map[$type]) && isset(self::$__map[$type][$name])) {
return self::$__map[$type][$name];
}
return false;
}
@ -1094,7 +1063,7 @@ class App extends Object {
* @return array
* @access private
*/
private function __settings($type, $plugin, $parent) {
private static function __settings($type, $plugin, $parent) {
if (!$parent) {
return array('class' => null, 'suffix' => null, 'path' => null);
}
@ -1185,17 +1154,16 @@ class App extends Object {
*
* @param string $type type of object to be searched
* @return array list of paths
* @access private
*/
private function __paths($type) {
private static function __paths($type) {
$type = strtolower($type);
$paths = array();
if ($type === 'core') {
return App::core('libs');
}
if (isset($this->{$type . 's'})) {
return $this->{$type . 's'};
if (isset(self::${$type . 's'})) {
return self::${$type . 's'};
}
return $paths;
}
@ -1207,13 +1175,12 @@ class App extends Object {
* @param string $type type of object
* @param string $plugin camelized name of plugin
* @return void
* @access private
*/
private function __remove($name, $type, $plugin) {
private static function __remove($name, $type, $plugin) {
if ($plugin) {
unset($this->__map['Plugin'][$plugin][$type][$name]);
unset(self::$__map['Plugin'][$plugin][$type][$name]);
} else {
unset($this->__map[$type][$name]);
unset(self::$__map[$type][$name]);
}
}
@ -1223,9 +1190,8 @@ class App extends Object {
* @param string $path Path to scan for files
* @param string $suffix if false, return only directories. if string, match and return files
* @return array List of directories or files in directory
* @access private
*/
private function __list($path, $suffix = false, $extension = false) {
private static function __list($path, $suffix = false, $extension = false) {
if (!class_exists('Folder')) {
require LIBS . 'folder.php';
}
@ -1257,15 +1223,14 @@ class App extends Object {
* Writes cache file if changes have been made to the $__map or $__paths
*
* @return void
* @access private
*/
function __destruct() {
if ($this->__cache) {
public static function shutdown() {
if (self::$__cache) {
$core = App::core('cake');
unset($this->__paths[rtrim($core[0], DS)]);
Cache::write('dir_map', array_filter($this->__paths), '_cake_core_');
Cache::write('file_map', array_filter($this->__map), '_cake_core_');
Cache::write('object_map', $this->__objects, '_cake_core_');
unset(self::$__paths[rtrim($core[0], DS)]);
Cache::write('dir_map', array_filter(self::$__paths), '_cake_core_');
Cache::write('file_map', array_filter(self::$__map), '_cake_core_');
Cache::write('object_map', self::$__objects, '_cake_core_');
}
}
}