mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Enhancing App::build() to allow appending paths. Closes #1680
This commit is contained in:
parent
093d7f1064
commit
1cb0e413be
2 changed files with 65 additions and 12 deletions
|
@ -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])) {
|
||||
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];
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue