Skip to content

Commit

Permalink
Remove App::objects().
Browse files Browse the repository at this point in the history
Since it has no callers and frequently does the wrong thing, it no
longer needs to be around.

Refs #4256
  • Loading branch information
markstory committed Aug 16, 2014
1 parent a02794a commit 47f88fa
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 245 deletions.
5 changes: 0 additions & 5 deletions config/bootstrap.php
Expand Up @@ -16,8 +16,3 @@
define('TIME_START', microtime(true));

require CAKE . 'basics.php';

use Cake\Core\App;
use Cake\Core\Configure;

App::init();
135 changes: 0 additions & 135 deletions src/Core/App.php
Expand Up @@ -14,7 +14,6 @@
*/
namespace Cake\Core;

use Cake\Cache\Cache;
use Cake\Core\Plugin;
use Cake\Utility\Inflector;

Expand All @@ -40,30 +39,10 @@
* Plugins can be located with App as well. Using Plugin::path('DebugKit') for example, will
* give you the full path to the DebugKit plugin.
*
* ### Inspecting known objects
*
* You can find out which objects App knows about using App::objects('Controller') for example to find
* which application controllers App knows about. This method will not find objects in sub-namespaces
* by default.
*
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
*/
class App {

/**
* Holds and key => value array of object types.
*
* @var array
*/
protected static $_objects = [];

/**
* Indicates whether the object cache should be stored again because of an addition to it
*
* @var bool
*/
protected static $_objectCacheChange = false;

/**
* Return the class name namespaced. This method checks if the class is defined on the
* application/plugin, otherwise try to load from the CakePHP core
Expand Down Expand Up @@ -162,118 +141,4 @@ public static function core($type) {
return [CAKE . str_replace('/', DS, $type) . DS];
}

/**
* Returns an array of objects of the given type.
*
* Example usage:
*
* `App::objects('Plugin');` returns `['DebugKit', 'Blog', 'User'];`
*
* `App::objects('Controller');` returns `['PagesController', 'BlogController'];`
*
* You can also search only within a plugin's objects by using the plugin dot
* syntax.
*
* `App::objects('MyPlugin.Model');` returns `['MyPluginPost', 'MyPluginComment'];`
*
* When scanning directories, files and directories beginning with `.` will be excluded as these
* are commonly used by version control systems.
*
* @param string $type Type of object, i.e. 'Model', 'Controller', 'View/Helper', 'file', 'class' or 'Plugin'
* @param string|array $path Optional Scan only the path given. If null, paths for the chosen type will be used.
* @param bool $cache Set to false to rescan objects of the chosen type. Defaults to true.
* @return mixed Either false on incorrect / miss. Or an array of found objects.
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::objects
*/
public static function objects($type, $path = null, $cache = true) {
if (empty(static::$_objects) && $cache === true) {
static::$_objects = (array)Cache::read('object_map', '_cake_core_');
}

$extension = '/\.php$/';
$includeDirectories = false;
$name = $type;

if ($type === 'Plugin') {
$extension = '/.*/';
$includeDirectories = true;
}

list($plugin, $type) = pluginSplit($type);

if ($type === 'file' && !$path) {
return false;
} elseif ($type === 'file') {
$extension = '/\.php$/';
$name = $type . str_replace(DS, '', $path);
}

$cacheLocation = empty($plugin) ? 'app' : $plugin;

if ($cache !== true || !isset(static::$_objects[$cacheLocation][$name])) {
$objects = [];

if (empty($path)) {
$path = static::path($type, $plugin);
}
foreach ((array)$path as $dir) {
if ($dir != APP && is_dir($dir)) {
$files = new \RegexIterator(new \DirectoryIterator($dir), $extension);
foreach ($files as $file) {
$fileName = basename($file);
if (!$file->isDot() && $fileName[0] !== '.') {
$isDir = $file->isDir();
if ($isDir && $includeDirectories) {
$objects[] = $fileName;
} elseif (!$includeDirectories && !$isDir) {
$objects[] = substr($fileName, 0, -4);
}
}
}
}
}

if ($type !== 'file') {
foreach ($objects as $key => $value) {
$objects[$key] = Inflector::camelize($value);
}
}

sort($objects);
if ($plugin) {
return $objects;
}

static::$_objects[$cacheLocation][$name] = $objects;
if ($cache) {
static::$_objectCacheChange = true;
}
}

return static::$_objects[$cacheLocation][$name];
}

/**
* Initializes the App, registers a shutdown function.
*
* @return void
*/
public static function init() {
register_shutdown_function([get_called_class(), 'shutdown']);
}

/**
* Object destructor.
*
* Writes cache file if changes have been made to the $_map. Also, check if a fatal
* error happened and call the handler.
*
* @return void
*/
public static function shutdown() {
if (static::$_objectCacheChange) {
Cache::write('object_map', static::$_objects, '_cake_core_');
}
}

}
1 change: 0 additions & 1 deletion tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -218,7 +218,6 @@ class ControllerTest extends TestCase {
public function setUp() {
parent::setUp();

App::objects('Plugin', null, false);
Configure::write('App.namespace', 'TestApp');
Router::reload();
}
Expand Down
89 changes: 0 additions & 89 deletions tests/TestCase/Core/AppTest.php
Expand Up @@ -163,93 +163,4 @@ public function testCore() {
$this->assertEquals(array(CAKE . 'Model' . DS . 'Datasource' . DS), str_replace('/', DS, $datasource));
}

/**
* testListObjects method
*
* @return void
*/
public function testListObjects() {
$result = App::objects('class', CAKE . 'Routing', false);
$this->assertTrue(in_array('Dispatcher', $result));
$this->assertTrue(in_array('Router', $result));

$result = App::objects('Model/Behavior', null, false);
$this->assertContains('SluggableBehavior', $result);

$result = App::objects('Controller/Component', null, false);
$this->assertContains('AppleComponent', $result);

$result = App::objects('View', null, false);
$this->assertContains('CustomJsonView', $result);

$result = App::objects('View/Helper', null, false);
$this->assertContains('BananaHelper', $result);

$result = App::objects('Model/Table', null, false);
$this->assertContains('ArticlesTable', $result);

$result = App::objects('file');
$this->assertFalse($result);

$result = App::objects('file', 'non_existing_configure');
$expected = array();
$this->assertEquals($expected, $result);

$result = App::objects('NonExistingType');
$this->assertSame(array(), $result);

$result = App::objects('Plugin', null, false);
$this->assertContains('TestPlugin', $result);
$this->assertContains('TestPluginTwo', $result);
}

/**
* Make sure that .svn and friends are excluded from App::objects('Plugin')
*
* @return void
*/
public function testListObjectsIgnoreDotDirectories() {
$path = TEST_APP . 'Plugin/';

$this->skipIf(!is_writable($path), $path . ' is not writable.');

mkdir($path . '.svn');
$result = App::objects('Plugin', null, false);
rmdir($path . '.svn');

$this->assertNotContains('.svn', $result);
}

/**
* Tests listing objects within a plugin
*
* @return void
*/
public function testListObjectsInPlugin() {
Plugin::load(array('TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree'));

$result = App::objects('TestPlugin.Model/Table');
$this->assertContains('TestPluginCommentsTable', $result);

$result = App::objects('Company/TestPluginThree.Model/Table');
$this->assertContains('TestPluginThreeCommentsTable', $result);

$result = App::objects('TestPlugin.Model/Behavior');
$this->assertTrue(in_array('PersisterOneBehavior', $result));

$result = App::objects('TestPlugin.View/Helper');
$expected = array('OtherHelperHelper', 'PluggedHelperHelper', 'TestPluginAppHelper');
$this->assertEquals($expected, $result);

$result = App::objects('TestPlugin.Controller/Component');
$this->assertTrue(in_array('OtherComponent', $result));

$result = App::objects('TestPluginTwo.Model/Behavior');
$this->assertSame(array(), $result);

$result = App::objects('Model/Table', null, false);
$this->assertContains('PostsTable', $result);
$this->assertContains('ArticlesTable', $result);
}

}
1 change: 0 additions & 1 deletion tests/TestCase/Core/ConfigureTest.php
Expand Up @@ -35,7 +35,6 @@ class ConfigureTest extends TestCase {
public function setUp() {
parent::setUp();
Cache::disable();
App::objects('Plugin', null, true);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions tests/TestCase/Core/PluginTest.php
Expand Up @@ -24,16 +24,6 @@
*/
class PluginTest extends TestCase {

/**
* Sets the plugins folder for this test
*
* @return void
*/
public function setUp() {
parent::setUp();
App::objects('Plugin', null, false);
}

/**
* Reverts the changes done to the environment while testing
*
Expand Down
2 changes: 0 additions & 2 deletions tests/TestCase/Routing/DispatcherTest.php
Expand Up @@ -217,8 +217,6 @@ public function setUp() {
Configure::write('App.webroot', 'webroot');
Configure::write('App.namespace', 'TestApp');

App::objects('Plugin', null, false);

$this->dispatcher = new TestDispatcher();
$this->dispatcher->addFilter(new ControllerFactoryFilter());
}
Expand Down
2 changes: 0 additions & 2 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -294,8 +294,6 @@ public function setUp() {
$this->ThemePostsController->index();
$this->ThemeView = $this->ThemePostsController->createView();

App::objects('Plugin', null, false);

Plugin::load(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']);
Configure::write('debug', true);
}
Expand Down

0 comments on commit 47f88fa

Please sign in to comment.