Added support for using plugin syntax in App::objects(). Fixes #1366

This commit is contained in:
Jeremy Harris 2011-01-03 19:27:21 -08:00
parent c58f835d9b
commit 4d2fdcd15e
2 changed files with 58 additions and 0 deletions

View file

@ -356,6 +356,11 @@ class App {
*
* `App::objects('plugin');` returns `array('DebugKit', 'Blog', 'User');`
*
* You can also search only within a plugin's objects by using the plugin dot
* syntax (these objects are not cached):
*
* `App::objects('MyPlugin.model');` returns `array('Post', 'Comment');`
*
* @param string $type Type of object, i.e. 'model', 'controller', 'helper', or 'plugin'
* @param mixed $path Optional Scan only the path given. If null, paths for the chosen
* type will be used.
@ -365,8 +370,22 @@ class App {
public static function objects($type, $path = null, $cache = true) {
$objects = array();
$extension = false;
list($plugin, $type) = pluginSplit($type);
$name = $type;
if ($plugin) {
$path = Inflector::pluralize($type);
if ($path == 'helpers') {
$path = 'views' . DS .$path;
} elseif ($path == 'behaviors') {
$path = 'models' . DS .$path;
} elseif ($path == 'components') {
$path = 'controllers' . DS .$path;
}
$path = self::pluginPath($plugin) . $path;
$cache = false;
}
if ($type === 'file' && !$path) {
return false;
} elseif ($type === 'file') {
@ -410,6 +429,9 @@ class App {
if ($cache === true) {
self::$__cache = true;
}
if ($plugin) {
return $objects;
}
self::$__objects[$name] = $objects;
}

View file

@ -141,6 +141,42 @@ class AppImportTest extends CakeTestCase {
App::build();
}
/**
* Tests listing objects within a plugin
*
* @return void
*/
function testListObjectsInPlugin() {
App::build(array(
'models' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS),
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
));
$oldCache = App::$models;
$result = App::objects('TestPlugin.model');
$this->assertTrue(in_array('TestPluginPost', $result));
$this->assertEquals($oldCache, App::$models);
$result = App::objects('TestPlugin.behavior');
$this->assertTrue(in_array('TestPluginPersisterOne', $result));
$result = App::objects('TestPlugin.helper');
$expected = array('OtherHelper', 'PluggedHelper', 'TestPluginApp');
$this->assertEquals($result, $expected);
$result = App::objects('TestPlugin.component');
$this->assertTrue(in_array('OtherComponent', $result));
$result = App::objects('TestPluginTwo.behavior');
$this->assertEquals($result, array());
$result = App::objects('model', null, false);
$this->assertTrue(in_array('Comment', $result));
$this->assertTrue(in_array('Post', $result));
App::build();
}
/**
* test that pluginPath can find paths for plugins.
*