Fixed bug where registering new packages would reset extra paths set for other packages. Closes #2666

This commit is contained in:
ADmad 2012-03-10 04:40:50 +05:30
parent 150c9fc6a3
commit 13b748ad86
2 changed files with 40 additions and 15 deletions

View file

@ -263,10 +263,13 @@ class App {
* *
* `App::build(array('View/Helper' => array('/path/to/helpers/', '/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`
* *
* `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. * 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 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 * @return void
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
*/ */
@ -289,24 +292,22 @@ class App {
return; return;
} }
if (empty($paths)) {
self::$_packageFormat = null;
}
$packageFormat = self::_packageFormat(); $packageFormat = self::_packageFormat();
if ($mode === App::REGISTER) { if ($mode === App::REGISTER) {
if (empty($paths)) { foreach ($paths as $package => $formats) {
self::$_packageFormat = null; if (empty($packageFormat[$package])) {
$packageFormat = self::_packageFormat(); $packageFormat[$package] = $formats;
} else { } else {
foreach ($paths as $package => $formats) { $formats = array_merge($packageFormat[$package], $formats);
if (!empty($packageFormat[$package])) {
$formats = array_merge($packageFormat[$package], $formats);
}
$packageFormat[$package] = array_values(array_unique($formats)); $packageFormat[$package] = array_values(array_unique($formats));
} }
self::$_packageFormat = $packageFormat;
$paths = array();
} }
self::$_packageFormat = $packageFormat;
} }
$defaults = array(); $defaults = array();
@ -321,9 +322,15 @@ class App {
return; return;
} }
if ($mode === App::REGISTER) {
$paths = array();
}
foreach ($defaults as $type => $default) { foreach ($defaults as $type => $default) {
if (!empty(self::$_packages[$type])) { if (!empty(self::$_packages[$type])) {
$path = self::$_packages[$type]; $path = self::$_packages[$type];
} else {
$path = $default;
} }
if (!empty($paths[$type])) { if (!empty($paths[$type])) {

View file

@ -191,6 +191,19 @@ class AppTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testBuildPackage() { 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'); $paths = App::path('Service');
$this->assertEquals(array(), $paths); $this->assertEquals(array(), $paths);
@ -203,11 +216,16 @@ class AppTest extends CakeTestCase {
$expected = array( $expected = array(
APP . 'Service' . DS, APP . 'Service' . DS,
); );
$result = App::path('Service'); $result = App::path('Service');
$this->assertEquals($expected, $result); $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);
} }
/** /**