Enhancing App::build() to allow appending paths. Closes #1680

This commit is contained in:
ADmad 2011-05-01 20:54:09 +05:30
parent 093d7f1064
commit 1cb0e413be
2 changed files with 65 additions and 12 deletions

View file

@ -62,6 +62,27 @@
*/ */
class App { class App {
/**
* Append paths
*
* @constant APPEND
*/
const APPEND = 'append';
/**
* Prepend paths
*
* @constant PREPEND
*/
const PREPEND = 'prepend';
/**
* Reset paths instead of merging
*
* @constant RESET
*/
const RESET = true;
/** /**
* List of object types and their properties * List of object types and their properties
* *
@ -222,15 +243,15 @@ class App {
* *
* `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package` * `App::build(array(Model' => array('/a/full/path/to/models/'))); will setup a new search path for the Model package`
* *
* `App::build(array('Model' => array('/path/to/models/')), true); will setup the path as the only valid path for searching models` * `App::build(array('Model' => array('/path/to/models/')), App::RESET); will setup the path as the only valid path for searching models`
* *
* `App::build(array('View/Helper' => array('/path/to/models/', '/another/path/))); will setup multiple search paths for helpers` * `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/))); will setup multiple search paths for helpers`
* *
* @param array $paths associative array with package names as keys and a list of directories for new search paths * @param array $paths associative array with package names as keys and a list of directories for new search paths
* @param boolean $reset true will set paths, false merges paths [default] false * @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
* @return void * @return void
*/ */
public static function build($paths = array(), $reset = false) { public static function build($paths = array(), $mode = App::PREPEND) {
if (empty(self::$__packageFormat)) { if (empty(self::$__packageFormat)) {
self::$__packageFormat = array( self::$__packageFormat = array(
'Model' => array( 'Model' => array(
@ -296,7 +317,7 @@ class App {
); );
} }
if ($reset == true) { if ($mode === App::RESET) {
foreach ($paths as $type => $new) { foreach ($paths as $type => $new) {
if (!empty(self::$legacy[$type])) { if (!empty(self::$legacy[$type])) {
$type = self::$legacy[$type]; $type = self::$legacy[$type];
@ -329,7 +350,11 @@ class App {
} }
if (!empty($paths[$type])) { if (!empty($paths[$type])) {
$path = array_merge((array)$paths[$type], self::$__packages[$type]); if ($mode === APP::PREPEND) {
$path = array_merge((array)$paths[$type], self::$__packages[$type]);
} else {
$path = array_merge(self::$__packages[$type], (array)$paths[$type]);
}
} else { } else {
$path = self::$__packages[$type]; $path = self::$__packages[$type];
} }

View file

@ -22,9 +22,7 @@ class AppImportTest extends CakeTestCase {
$this->assertEqual($expected, $old); $this->assertEqual($expected, $old);
App::build(array('Model' => array('/path/to/models/'))); App::build(array('Model' => array('/path/to/models/')));
$new = App::path('Model'); $new = App::path('Model');
$expected = array( $expected = array(
'/path/to/models/', '/path/to/models/',
APP . 'Model' . DS, APP . 'Model' . DS,
@ -32,6 +30,36 @@ class AppImportTest extends CakeTestCase {
); );
$this->assertEqual($expected, $new); $this->assertEqual($expected, $new);
App::build();
App::build(array('Model' => array('/path/to/models/')), APP::APPEND);
$new = App::path('Model');
$expected = array(
APP . 'Model' . DS,
APP . 'models' . DS,
'/path/to/models/'
);
$this->assertEqual($expected, $new);
App::build();
App::build(array(
'Model' => array('/path/to/models/'),
'Controller' => array('/path/to/controllers/'),
), APP::APPEND);
$new = App::path('Model');
$expected = array(
APP . 'Model' . DS,
APP . 'models' . DS,
'/path/to/models/'
);
$this->assertEqual($expected, $new);
$new = App::path('Controller');
$expected = array(
APP . 'Controller' . DS,
APP . 'controllers' . DS,
'/path/to/controllers/'
);
$this->assertEqual($expected, $new);
App::build(); //reset defaults App::build(); //reset defaults
$defaults = App::path('Model'); $defaults = App::path('Model');
$this->assertEqual($old, $defaults); $this->assertEqual($old, $defaults);
@ -152,7 +180,7 @@ class AppImportTest extends CakeTestCase {
); );
$this->assertEqual($expected, $old); $this->assertEqual($expected, $old);
App::build(array('Model' => array('/path/to/models/')), true); App::build(array('Model' => array('/path/to/models/')), App::RESET);
$new = App::path('Model'); $new = App::path('Model');
@ -210,7 +238,7 @@ class AppImportTest extends CakeTestCase {
'View' => App::core('View'), 'View' => App::core('View'),
'Model' => App::core('Model'), 'Model' => App::core('Model'),
'View/Helper' => App::core('View/Helper'), 'View/Helper' => App::core('View/Helper'),
), true); ), App::RESET);
$result = App::objects('behavior', null, false); $result = App::objects('behavior', null, false);
$this->assertTrue(in_array('TreeBehavior', $result)); $this->assertTrue(in_array('TreeBehavior', $result));
$result = App::objects('Model/Behavior', null, false); $result = App::objects('Model/Behavior', null, false);
@ -623,7 +651,7 @@ class AppImportTest extends CakeTestCase {
App::build(array( App::build(array(
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS), 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'vendors' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'vendors'. DS), 'vendors' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'vendors'. DS),
), true); ), App::RESET);
ob_start(); ob_start();
$result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css')); $result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
@ -679,7 +707,7 @@ class AppImportTest extends CakeTestCase {
App::build(array( App::build(array(
'libs' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'libs' . DS), 'libs' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) 'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
), true); ), App::RESET);
$this->assertFalse(class_exists('CustomLibClass', false)); $this->assertFalse(class_exists('CustomLibClass', false));
App::uses('CustomLibClass', 'TestPlugin.Custom/Package'); App::uses('CustomLibClass', 'TestPlugin.Custom/Package');