Skip to content

Commit

Permalink
Refactoring App::build() so it looks more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Yosuke Basuke Suzuki authored and lorenzo committed Oct 14, 2011
1 parent 0a70963 commit 0b6c93c
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 87 deletions.
176 changes: 91 additions & 85 deletions lib/Cake/Core/App.php
Expand Up @@ -214,8 +214,9 @@ public static function path($type, $plugin = null) {
if (!empty($plugin)) {
$path = array();
$pluginPath = self::pluginPath($plugin);
if (!empty(self::$_packageFormat[$type])) {
foreach (self::$_packageFormat[$type] as $f) {
$packageFormat= self::_packageFormat();
if (!empty($packageFormat[$type])) {
foreach ($packageFormat[$type] as $f) {
$path[] = sprintf($f, $pluginPath);
}
}
Expand Down Expand Up @@ -260,100 +261,28 @@ public static function paths() {
* @return void
*/
public static function build($paths = array(), $mode = App::PREPEND) {
if (empty(self::$_packageFormat)) {
self::$_packageFormat = array(
'Model' => array(
'%s' . 'Model' . DS,
'%s' . 'models' . DS
),
'Model/Behavior' => array(
'%s' . 'Model' . DS . 'Behavior' . DS,
'%s' . 'models' . DS . 'behaviors' . DS
),
'Model/Datasource' => array(
'%s' . 'Model' . DS . 'Datasource' . DS,
'%s' . 'models' . DS . 'datasources' . DS
),
'Model/Datasource/Database' => array(
'%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
'%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
),
'Model/Datasource/Session' => array(
'%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
'%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
),
'Controller' => array(
'%s' . 'Controller' . DS,
'%s' . 'controllers' . DS
),
'Controller/Component' => array(
'%s' . 'Controller' . DS . 'Component' . DS,
'%s' . 'controllers' . DS . 'components' . DS
),
'Controller/Component/Auth' => array(
'%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
'%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
),
'View' => array(
'%s' . 'View' . DS,
'%s' . 'views' . DS
),
'View/Helper' => array(
'%s' . 'View' . DS . 'Helper' . DS,
'%s' . 'views' . DS . 'helpers' . DS
),
'Console' => array(
'%s' . 'Console' . DS,
'%s' . 'console' . DS
),
'Console/Command' => array(
'%s' . 'Console' . DS . 'Command' . DS,
'%s' . 'console' . DS . 'shells' . DS,
),
'Console/Command/Task' => array(
'%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
'%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
),
'Lib' => array(
'%s' . 'Lib' . DS,
'%s' . 'libs' . DS
),
'locales' => array(
'%s' . 'Locale' . DS,
'%s' . 'locale' . DS
),
'Vendor' => array('%s' . 'Vendor' . DS, VENDORS),
'Plugin' => array(
APP . 'Plugin' . DS,
APP . 'plugins' . DS,
dirname(dirname(CAKE)) . DS . 'plugins' . DS,
)
);
//Provides Backwards compatibility for old-style package names
$legacyPaths = array();
foreach ($paths as $type => $path) {
if (!empty(self::$legacy[$type])) {
$type = self::$legacy[$type];
}
$legacyPaths[$type] = $path;
}
$paths = $legacyPaths;

if ($mode === App::RESET) {
foreach ($paths as $type => $new) {
if (!empty(self::$legacy[$type])) {
$type = self::$legacy[$type];
}
self::$_packages[$type] = (array)$new;
self::objects($type, null, false);
}
return $paths;
return;
}

//Provides Backwards compatibility for old-style package names
$legacyPaths = array();
foreach ($paths as $type => $path) {
if (!empty(self::$legacy[$type])) {
$type = self::$legacy[$type];
}
$legacyPaths[$type] = $path;
}
$packageFormat = self::_packageFormat();

$paths = $legacyPaths;
$defaults = array();
foreach (self::$_packageFormat as $package => $format) {
foreach ($packageFormat as $package => $format) {
foreach ($format as $f) {
$defaults[$package][] = sprintf($f, APP);
}
Expand Down Expand Up @@ -853,6 +782,83 @@ protected static function _mapped($name, $plugin = null) {
return false;
}

protected static function _packageFormat() {
if (empty(self::$_packageFormat)) {
self::$_packageFormat = array(
'Model' => array(
'%s' . 'Model' . DS,
'%s' . 'models' . DS
),
'Model/Behavior' => array(
'%s' . 'Model' . DS . 'Behavior' . DS,
'%s' . 'models' . DS . 'behaviors' . DS
),
'Model/Datasource' => array(
'%s' . 'Model' . DS . 'Datasource' . DS,
'%s' . 'models' . DS . 'datasources' . DS
),
'Model/Datasource/Database' => array(
'%s' . 'Model' . DS . 'Datasource' . DS . 'Database' . DS,
'%s' . 'models' . DS . 'datasources' . DS . 'database' . DS
),
'Model/Datasource/Session' => array(
'%s' . 'Model' . DS . 'Datasource' . DS . 'Session' . DS,
'%s' . 'models' . DS . 'datasources' . DS . 'session' . DS
),
'Controller' => array(
'%s' . 'Controller' . DS,
'%s' . 'controllers' . DS
),
'Controller/Component' => array(
'%s' . 'Controller' . DS . 'Component' . DS,
'%s' . 'controllers' . DS . 'components' . DS
),
'Controller/Component/Auth' => array(
'%s' . 'Controller' . DS . 'Component' . DS . 'Auth' . DS,
'%s' . 'controllers' . DS . 'components' . DS . 'auth' . DS
),
'View' => array(
'%s' . 'View' . DS,
'%s' . 'views' . DS
),
'View/Helper' => array(
'%s' . 'View' . DS . 'Helper' . DS,
'%s' . 'views' . DS . 'helpers' . DS
),
'Console' => array(
'%s' . 'Console' . DS,
'%s' . 'console' . DS
),
'Console/Command' => array(
'%s' . 'Console' . DS . 'Command' . DS,
'%s' . 'console' . DS . 'shells' . DS,
),
'Console/Command/Task' => array(
'%s' . 'Console' . DS . 'Command' . DS . 'Task' . DS,
'%s' . 'console' . DS . 'shells' . DS . 'tasks' . DS
),
'Lib' => array(
'%s' . 'Lib' . DS,
'%s' . 'libs' . DS
),
'locales' => array(
'%s' . 'Locale' . DS,
'%s' . 'locale' . DS
),
'Vendor' => array(
'%s' . 'Vendor' . DS, VENDORS
),
'Plugin' => array(
APP . 'Plugin' . DS,
APP . 'plugins' . DS,
dirname(dirname(CAKE)) . DS . 'plugins' . DS
)
);
}

return self::$_packageFormat;
}

/**
* Object destructor.
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Test/Case/Core/AppTest.php
Expand Up @@ -345,7 +345,7 @@ public function testListObjectsIgnoreDotDirectories() {
$path = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS;
App::build(array(
'plugins' => array($path)
), true);
), App::RESET);
mkdir($path . '.svn');
$result = App::objects('plugin', null, false);
rmdir($path . '.svn');
Expand All @@ -362,7 +362,7 @@ public function testListObjectsInPlugin() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), true);
), App::RESET);
CakePlugin::loadAll();

$result = App::objects('TestPlugin.model');
Expand Down

0 comments on commit 0b6c93c

Please sign in to comment.