Skip to content

Commit

Permalink
Added support for using plugin syntax in App::objects(). Fixes #1366
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jan 4, 2011
1 parent c58f835 commit 4d2fdcd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cake/libs/app.php
Expand Up @@ -356,6 +356,11 @@ public static function core($type = null) {
*
* `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.
Expand All @@ -365,8 +370,22 @@ public static function core($type = null) {
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') {
Expand Down Expand Up @@ -410,6 +429,9 @@ public static function objects($type, $path = null, $cache = true) {
if ($cache === true) {
self::$__cache = true;
}
if ($plugin) {
return $objects;
}
self::$__objects[$name] = $objects;
}

Expand Down
36 changes: 36 additions & 0 deletions cake/tests/cases/libs/app.test.php
Expand Up @@ -141,6 +141,42 @@ function testListObjects() {
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.
*
Expand Down

0 comments on commit 4d2fdcd

Please sign in to comment.