From 0b6c93cf82d5aa32624095c4c2fa7e77b47ad1f1 Mon Sep 17 00:00:00 2001 From: Yosuke Basuke Suzuki Date: Thu, 6 Oct 2011 10:47:26 +0900 Subject: [PATCH] Refactoring App::build() so it looks more readable --- lib/Cake/Core/App.php | 178 ++++++++++++++-------------- lib/Cake/Test/Case/Core/AppTest.php | 4 +- 2 files changed, 94 insertions(+), 88 deletions(-) diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index e67081a14..8b6f5f00a 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -214,8 +214,9 @@ class App { 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); } } @@ -260,88 +261,6 @@ class App { * @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, - ) - ); - } - - 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; - } - //Provides Backwards compatibility for old-style package names $legacyPaths = array(); foreach ($paths as $type => $path) { @@ -350,10 +269,20 @@ class App { } $legacyPaths[$type] = $path; } - $paths = $legacyPaths; + + if ($mode === App::RESET) { + foreach ($paths as $type => $new) { + self::$_packages[$type] = (array)$new; + self::objects($type, null, false); + } + return; + } + + $packageFormat = self::_packageFormat(); + $defaults = array(); - foreach (self::$_packageFormat as $package => $format) { + foreach ($packageFormat as $package => $format) { foreach ($format as $f) { $defaults[$package][] = sprintf($f, APP); } @@ -853,6 +782,83 @@ class App { 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. * diff --git a/lib/Cake/Test/Case/Core/AppTest.php b/lib/Cake/Test/Case/Core/AppTest.php index 18f082c41..3520fd461 100644 --- a/lib/Cake/Test/Case/Core/AppTest.php +++ b/lib/Cake/Test/Case/Core/AppTest.php @@ -345,7 +345,7 @@ class AppTest extends CakeTestCase { $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'); @@ -362,7 +362,7 @@ class AppTest extends CakeTestCase { 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');