From 1cb0e413beefc5479dec19220b45675f9172fe25 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 1 May 2011 20:54:09 +0530 Subject: [PATCH] Enhancing App::build() to allow appending paths. Closes #1680 --- lib/Cake/Core/App.php | 37 ++++++++++++++++++++----- lib/Cake/tests/Case/Core/AppTest.php | 40 +++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index 5ac300484..8e5c594eb 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -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 * @@ -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('/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( @@ -296,7 +317,7 @@ class App { ); } - if ($reset == true) { + if ($mode === App::RESET) { foreach ($paths as $type => $new) { if (!empty(self::$legacy[$type])) { $type = self::$legacy[$type]; @@ -329,7 +350,11 @@ class App { } 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]; } diff --git a/lib/Cake/tests/Case/Core/AppTest.php b/lib/Cake/tests/Case/Core/AppTest.php index 10a5b90c1..98b84a453 100644 --- a/lib/Cake/tests/Case/Core/AppTest.php +++ b/lib/Cake/tests/Case/Core/AppTest.php @@ -22,9 +22,7 @@ class AppImportTest extends CakeTestCase { $this->assertEqual($expected, $old); App::build(array('Model' => array('/path/to/models/'))); - $new = App::path('Model'); - $expected = array( '/path/to/models/', APP . 'Model' . DS, @@ -32,6 +30,36 @@ class AppImportTest extends CakeTestCase { ); $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); @@ -152,7 +180,7 @@ class AppImportTest extends CakeTestCase { ); $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'); @@ -210,7 +238,7 @@ class AppImportTest extends CakeTestCase { '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); @@ -623,7 +651,7 @@ class AppImportTest extends CakeTestCase { 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')); @@ -679,7 +707,7 @@ class AppImportTest extends CakeTestCase { 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');