Skip to content

Commit

Permalink
Enhancing App::build() to allow appending paths. Closes #1680
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed May 1, 2011
1 parent 093d7f1 commit 1cb0e41
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
37 changes: 31 additions & 6 deletions lib/Cake/Core/App.php
Expand Up @@ -62,6 +62,27 @@
*/
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
*
Expand Down Expand Up @@ -222,15 +243,15 @@ public static function path($type, $plugin = null) {
*
* `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 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
*/
public static function build($paths = array(), $reset = false) {
public static function build($paths = array(), $mode = App::PREPEND) {
if (empty(self::$__packageFormat)) {
self::$__packageFormat = array(
'Model' => array(
Expand Down Expand Up @@ -296,7 +317,7 @@ public static function build($paths = array(), $reset = false) {
);
}

if ($reset == true) {
if ($mode === App::RESET) {
foreach ($paths as $type => $new) {
if (!empty(self::$legacy[$type])) {
$type = self::$legacy[$type];
Expand Down Expand Up @@ -329,7 +350,11 @@ public static function build($paths = array(), $reset = false) {
}

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 {
$path = self::$__packages[$type];
}
Expand Down
40 changes: 34 additions & 6 deletions lib/Cake/tests/Case/Core/AppTest.php
Expand Up @@ -22,16 +22,44 @@ function testBuild() {
$this->assertEqual($expected, $old);

App::build(array('Model' => array('/path/to/models/')));

$new = App::path('Model');

$expected = array(
'/path/to/models/',
APP . 'Model' . DS,
APP . 'models' . DS
);
$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
$defaults = App::path('Model');
$this->assertEqual($old, $defaults);
Expand Down Expand Up @@ -152,7 +180,7 @@ function testBuildWithReset() {
);
$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');

Expand Down Expand Up @@ -210,7 +238,7 @@ function testListObjects() {
'View' => App::core('View'),
'Model' => App::core('Model'),
'View/Helper' => App::core('View/Helper'),
), true);
), App::RESET);
$result = App::objects('behavior', null, false);
$this->assertTrue(in_array('TreeBehavior', $result));
$result = App::objects('Model/Behavior', null, false);
Expand Down Expand Up @@ -623,7 +651,7 @@ function testLoadingVendor() {
App::build(array(
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'vendors' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'vendors'. DS),
), true);
), App::RESET);

ob_start();
$result = App::import('Vendor', 'css/TestAsset', array('ext' => 'css'));
Expand Down Expand Up @@ -679,7 +707,7 @@ public function testLoadClassInLibs() {
App::build(array(
'libs' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
), true);
), App::RESET);

$this->assertFalse(class_exists('CustomLibClass', false));
App::uses('CustomLibClass', 'TestPlugin.Custom/Package');
Expand Down

0 comments on commit 1cb0e41

Please sign in to comment.