Skip to content
Permalink
Browse files

Remove App::build() and App::paths().

Replace App::build() with a configure value for view paths. Since view
paths are generally not modified a configure value should be enough.
Also maintaining the old method name when it does very little of what it
used to do is misleading.

Update App::path() to read from the new configure values and provide
a nice interface for developers to use.
  • Loading branch information...
markstory committed Sep 10, 2013
1 parent 1c3e70e commit bc01101b2aeff5a34c767360939d624334aac832
@@ -56,48 +56,13 @@
*/
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;
/**
* Holds and key => value array of object types.
*
* @var array
*/
protected static $_objects = [];
/**
* Holds the possible paths for each package name
*
* @var array
*/
protected static $_packages = [];
/**
* Holds the templates for each customizable package path in the application
*
* @var array
*/
protected static $_packageFormat = [];
/**
* Indicates whether the object cache should be stored again because of an addition to it
*
@@ -157,105 +122,16 @@ public static function classname($class, $type = '', $suffix = '') {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::path
*/
public static function path($type, $plugin = null) {
if (!empty($plugin)) {
return [static::pluginPath($plugin) . $type . DS];
}
if (!isset(static::$_packages[$type])) {
return [APP . $type . DS];
}
return static::$_packages[$type];
}
/**
* Get all the currently configured paths.
*
* This will only reflect paths for resources, and not classes.
* Class paths are not managed by App and it has no knowledge of them.
* For a paths to a specific package use App::path()
*
* @return array An array of packages and their associated paths.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::paths
*/
public static function paths() {
return static::$_packages;
}
/**
* Sets up package locations on the file system.
*
* You can configure multiple search paths for each package, those
* will be used to look for files one folder at a time in the specified
* order. All paths should be terminated with a Directory separator
*
* Usage:
*
* `App::build(['View' => ['/a/full/path/to/views/']]);`
*
* Will setup a new search path for the Model package
*
* `App::build(['View' => ['/path/to/views/']), App::RESET);`
*
* Will setup the path as the only valid path for searching models`
*
* `App::build(['View' => ['/path/to/views/', '/another/path/']]);`
*
* Will setup multiple search paths for views.
*
* 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 boolean|string $mode App::RESET will set paths, App::APPEND with append paths, App::PREPEND will prepend paths (default)
* @return void
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::build
*/
public static function build($paths = [], $mode = self::PREPEND) {
if ($mode === static::RESET) {
foreach ($paths as $type => $new) {
static::$_packages[$type] = (array)$new;
static::objects($type, null, false);
}
return;
if ($type === 'Plugin') {
return (array)Configure::read('App.pluginPaths');
}
if (empty($paths)) {
static::$_packageFormat = null;
if (empty($plugin) && $type === 'View') {
return (array)Configure::read('App.viewPaths');
}
$packageFormat = static::_packageFormat();
$defaults = [];
foreach ($packageFormat as $package => $format) {
foreach ($format as $f) {
$defaults[$package][] = sprintf($f, APP);
}
}
if (empty($paths)) {
static::$_packages = $defaults;
return;
}
foreach ($defaults as $type => $default) {
if (!empty(static::$_packages[$type])) {
$path = static::$_packages[$type];
} else {
$path = $default;
}
if (!empty($paths[$type])) {
$newPath = (array)$paths[$type];
if ($mode === static::PREPEND) {
$path = array_merge($newPath, $path);
} else {
$path = array_merge($path, $newPath);
}
$path = array_values(array_unique($path));
}
static::$_packages[$type] = $path;
if (!empty($plugin)) {
return [static::pluginPath($plugin) . $type . DS];
}
return [APP . $type . DS];
}
/**
@@ -288,12 +164,13 @@ public static function pluginPath($plugin) {
*/
public static function themePath($theme) {
$themeDir = 'Themed' . DS . Inflector::camelize($theme);
foreach (static::$_packages['View'] as $path) {
$paths = static::path('View');
foreach ($paths as $path) {
if (is_dir($path . $themeDir)) {
return $path . $themeDir . DS;
}
}
return static::$_packages['View'][0] . $themeDir . DS;
return $paths[0] . $themeDir . DS;
}
/**
@@ -413,26 +290,6 @@ public static function init() {
register_shutdown_function([get_called_class(), 'shutdown']);
}
/**
* Sets then returns the templates for each customizable package path
*
* @return array templates for each customizable package path
*/
protected static function _packageFormat() {
if (empty(static::$_packageFormat)) {
static::$_packageFormat = [
'View' => [
'%s' . 'View' . DS,
],
'Locale' => [
'%s' . 'Locale' . DS
],
];
}
return static::$_packageFormat;
}
/**
* Object destructor.
*
@@ -117,8 +117,8 @@ public static function load($plugin, $config = array()) {
$config += ['autoload' => false, 'bootstrap' => false, 'routes' => false, 'namespace' => $plugin, 'ignoreMissing' => false];
if (empty($config['path'])) {
$paths = Configure::read('App.pluginPaths');
foreach ((array)$paths as $path) {
$paths = App::path('Plugin');
foreach ($paths as $path) {
$namespacePath = str_replace('\\', DS, $config['namespace']);
if (is_dir($path . $plugin)) {
$config += ['path' => $path . $plugin . DS];
@@ -69,59 +69,6 @@ public function testClassname() {
);
}
/**
* testBuild method
*
* @return void
*/
public function testBuild() {
$old = App::path('View');
$expected = array(
APP . 'View' . DS,
);
$this->assertEquals($expected, $old);
App::build();
App::build(array('View' => array('/path/to/views/')), App::PREPEND);
$new = App::path('View');
$expected = array(
'/path/to/views/',
APP . 'View' . DS,
);
$this->assertEquals($expected, $new);
App::build();
App::build(array('View' => array('/path/to/views/')), App::APPEND);
$new = App::path('View');
$expected = array(
APP . 'View' . DS,
'/path/to/views/'
);
$this->assertEquals($expected, $new);
App::build();
App::build(array(
'View' => array('/path/to/views/'),
'Locale' => array('/path/to/locales/'),
), App::APPEND);
$new = App::path('View');
$expected = array(
APP . 'View' . DS,
'/path/to/views/'
);
$this->assertEquals($expected, $new);
$new = App::path('Locale');
$expected = array(
APP . 'Locale' . DS,
'/path/to/locales/'
);
$this->assertEquals($expected, $new);
App::build(); //reset defaults
$defaults = App::path('View');
$this->assertEquals($old, $defaults);
}
/**
* test path() with a plugin.
*
@@ -135,32 +82,6 @@ public function testPathWithPlugins() {
$this->assertEquals($basepath . 'TestPlugin' . DS . 'Controller' . DS, $result[0]);
}
/**
* testBuildWithReset method
*
* @return void
*/
public function testBuildWithReset() {
$old = App::path('View');
$expected = array(
APP . 'View' . DS,
);
$this->assertEquals($expected, $old);
App::build(array('View' => array('/path/to/views/')), App::RESET);
$new = App::path('View');
$expected = array(
'/path/to/views/'
);
$this->assertEquals($expected, $new);
App::build(); //reset defaults
$defaults = App::path('View');
$this->assertEquals($old, $defaults);
}
/**
* testCore method
*
@@ -234,9 +155,6 @@ public function testListObjectsIgnoreDotDirectories() {
$this->skipIf(!is_writable($path), $path . ' is not writable.');
App::build(array(
'Plugin' => array($path)
), App::RESET);
mkdir($path . '.svn');
$result = App::objects('Plugin', null, false);
rmdir($path . '.svn');
@@ -250,10 +168,6 @@ public function testListObjectsIgnoreDotDirectories() {
* @return void
*/
public function testListObjectsInPlugin() {
App::build(array(
'Model' => array(CAKE . 'Test/TestApp/Model/'),
'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
), App::RESET);
Plugin::load(array('TestPlugin', 'TestPluginTwo'));
$result = App::objects('TestPlugin.Model');
@@ -275,8 +189,6 @@ public function testListObjectsInPlugin() {
$result = App::objects('Model', null, false);
$this->assertTrue(in_array('Comment', $result));
$this->assertTrue(in_array('Post', $result));
App::build();
}
/**
@@ -285,9 +197,6 @@ public function testListObjectsInPlugin() {
* @return void
*/
public function testPluginPath() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS)
));
Plugin::load(array('TestPlugin', 'TestPluginTwo'));
$path = App::pluginPath('TestPlugin');
@@ -297,7 +206,6 @@ public function testPluginPath() {
$path = App::pluginPath('TestPluginTwo');
$expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
$this->assertEquals($expected, $path);
App::build();
}
/**
@@ -306,29 +214,13 @@ public function testPluginPath() {
* @return void
*/
public function testThemePath() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS)
));
$path = App::themePath('test_theme');
$expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
$this->assertEquals($expected, $path);
$path = App::themePath('TestTheme');
$expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
$this->assertEquals($expected, $path);
App::build();
}
/**
* Test that paths() works.
*
* @return void
*/
public function testPaths() {
$result = App::paths();
$this->assertArrayHasKey('View', $result);
$this->assertArrayHasKey('Locale', $result);
}
}
Oops, something went wrong.

0 comments on commit bc01101

Please sign in to comment.
You can’t perform that action at this time.