diff --git a/cake/libs/app.php b/cake/libs/app.php index 7d8868814f6..ce7fb4a0521 100644 --- a/cake/libs/app.php +++ b/cake/libs/app.php @@ -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. @@ -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') { @@ -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; } diff --git a/cake/tests/cases/libs/app.test.php b/cake/tests/cases/libs/app.test.php index 6e7793072c0..701f32f60c0 100644 --- a/cake/tests/cases/libs/app.test.php +++ b/cake/tests/cases/libs/app.test.php @@ -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. *