Skip to content

Commit

Permalink
Fixed bug where registering new packages would reset extra paths set …
Browse files Browse the repository at this point in the history
…for other packages. Closes #2666
  • Loading branch information
ADmad committed Mar 9, 2012
1 parent 150c9fc commit 13b748a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
33 changes: 20 additions & 13 deletions lib/Cake/Core/App.php
Expand Up @@ -263,10 +263,13 @@ public static function paths() {
*
* `App::build(array('View/Helper' => array('/path/to/helpers/', '/another/path/'))); will setup multiple search paths for helpers`
*
* `App::build(array('Service' => array('%s' . 'Service' . DS)), App::REGISTER); will register new package 'Service'`
*
* If reset is set to true, all loaded plugins will be forgotten and they will be needed to be loaded again.
*
* @param array $paths associative array with package names as keys and a list of directories for new search paths
* @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths, [default] App::PREPEND
* @param mixed $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default)
* App::REGISTER will register new packages and their paths, %s in path will be replaced by APP path
* @return void
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
*/
Expand All @@ -289,24 +292,22 @@ public static function build($paths = array(), $mode = App::PREPEND) {
return;
}

if (empty($paths)) {
self::$_packageFormat = null;
}

$packageFormat = self::_packageFormat();

if ($mode === App::REGISTER) {
if (empty($paths)) {
self::$_packageFormat = null;
$packageFormat = self::_packageFormat();
} else {
foreach ($paths as $package => $formats) {
if (!empty($packageFormat[$package])) {
$formats = array_merge($packageFormat[$package], $formats);
}

foreach ($paths as $package => $formats) {
if (empty($packageFormat[$package])) {
$packageFormat[$package] = $formats;
} else {
$formats = array_merge($packageFormat[$package], $formats);
$packageFormat[$package] = array_values(array_unique($formats));
}

self::$_packageFormat = $packageFormat;
$paths = array();
}
self::$_packageFormat = $packageFormat;
}

$defaults = array();
Expand All @@ -321,9 +322,15 @@ public static function build($paths = array(), $mode = App::PREPEND) {
return;
}

if ($mode === App::REGISTER) {
$paths = array();
}

foreach ($defaults as $type => $default) {
if (!empty(self::$_packages[$type])) {
$path = self::$_packages[$type];
} else {
$path = $default;
}

if (!empty($paths[$type])) {
Expand Down
22 changes: 20 additions & 2 deletions lib/Cake/Test/Case/Core/AppTest.php
Expand Up @@ -191,6 +191,19 @@ public function testCompatibleBuild() {
* @return void
*/
public function testBuildPackage() {
$pluginPaths = array(
'/foo/bar',
APP . 'Plugin' . DS,
dirname(dirname(CAKE)) . DS . 'plugins' . DS
);
App::build(array(
'Plugin' => array(
'/foo/bar'
)
));
$result = App::path('Plugin');
$this->assertEquals($pluginPaths, $result);

$paths = App::path('Service');
$this->assertEquals(array(), $paths);

Expand All @@ -203,11 +216,16 @@ public function testBuildPackage() {
$expected = array(
APP . 'Service' . DS,
);

$result = App::path('Service');
$this->assertEquals($expected, $result);

App::build(array(), App::REGISTER);
//Ensure new paths registered for other packages are not affected
$result = App::path('Plugin');
$this->assertEquals($pluginPaths, $result);

App::build();
$paths = App::path('Service');
$this->assertEquals(array(), $paths);
}

/**
Expand Down

0 comments on commit 13b748a

Please sign in to comment.