Browse Source

Merge pull request #4279 from cakephp/no-app-objects

Remove App::objects()
José Lorenzo Rodríguez 11 years ago
parent
commit
a2f1456f49

+ 0 - 5
config/bootstrap.php

@@ -16,8 +16,3 @@
 define('TIME_START', microtime(true));
 
 require CAKE . 'basics.php';
-
-use Cake\Core\App;
-use Cake\Core\Configure;
-
-App::init();

+ 36 - 9
src/Console/Command/Task/CommandTask.php

@@ -19,6 +19,7 @@ use Cake\Console\Shell;
 use Cake\Core\App;
 use Cake\Core\Plugin;
 use Cake\Utility\Inflector;
+use Cake\Utility\Folder;
 use \ReflectionClass;
 use \ReflectionMethod;
 
@@ -41,17 +42,19 @@ class CommandTask extends Shell {
 		$shellList = array_fill_keys($plugins, null) + ['CORE' => null, 'app' => null];
 
 		$corePath = App::core('Console/Command');
-		$shells = App::objects('file', $corePath[0]);
+		$shells = $this->_scanDir($corePath[0]);
 		$shells = array_diff($shells, $skipFiles, $hiddenCommands);
-		$this->_appendShells('CORE', $shells, $shellList);
+		$shellList = $this->_appendShells('CORE', $shells, $shellList);
 
-		$appShells = App::objects('Console/Command', null, false);
+		$appPath = App::path('Console/Command');
+		$appShells = $this->_scanDir($appPath[0]);
 		$appShells = array_diff($appShells, $shells, $skipFiles);
-		$this->_appendShells('app', $appShells, $shellList);
+		$shellList = $this->_appendShells('app', $appShells, $shellList);
 
 		foreach ($plugins as $plugin) {
-			$pluginShells = App::objects($plugin . '.Console/Command');
-			$this->_appendShells($plugin, $pluginShells, $shellList);
+			$pluginPath = Plugin::classPath($plugin) . 'Console' . DS . 'Command';
+			$pluginShells = $this->_scanDir($pluginPath);
+			$shellList = $this->_appendShells($plugin, $pluginShells, $shellList);
 		}
 
 		return array_filter($shellList);
@@ -62,13 +65,37 @@ class CommandTask extends Shell {
  *
  * @param string $type The type of object.
  * @param array $shells The shell name.
- * @param array &$shellList List of shells.
- * @return void
+ * @param array $shellList List of shells.
+ * @return array The updated $shellList
  */
-	protected function _appendShells($type, $shells, &$shellList) {
+	protected function _appendShells($type, $shells, $shellList) {
 		foreach ($shells as $shell) {
 			$shellList[$type][] = Inflector::underscore(str_replace('Shell', '', $shell));
 		}
+		return $shellList;
+	}
+
+/**
+ * Scan a directory for .php files and return the class names that
+ * should be within them.
+ *
+ * @param string $dir The directory to read.
+ * @return array The list of shell classnames based on conventions.
+ */
+	protected function _scanDir($dir) {
+		$dir = new Folder($dir);
+		$contents = $dir->read(true, true);
+		if (empty($contents[1])) {
+			return [];
+		}
+		$shells = [];
+		foreach ($contents[1] as $file) {
+			if (substr($file, -4) !== '.php') {
+				continue;
+			}
+			$shells[] = substr($file, 0, -4);
+		}
+		return $shells;
 	}
 
 /**

+ 3 - 2
src/Console/Command/Task/TemplateTask.php

@@ -16,6 +16,7 @@ namespace Cake\Console\Command\Task;
 
 use Cake\Console\Shell;
 use Cake\Core\App;
+use Cake\Core\Plugin;
 use Cake\Utility\ConventionsTrait;
 use Cake\Utility\Folder;
 use Cake\View\ViewVarsTrait;
@@ -57,9 +58,9 @@ class TemplateTask extends Shell {
 	protected function _findTemplates() {
 		$paths = App::path('Template');
 
-		$plugins = App::objects('Plugin');
+		$plugins = Plugin::loaded();
 		foreach ($plugins as $plugin) {
-			$paths[] = $this->_pluginPath($plugin) . 'src' . DS . 'Template' . DS;
+			$paths[] = Plugin::classPath($plugin) . DS . 'Template' . DS;
 		}
 
 		$core = current(App::core('Template'));

+ 0 - 135
src/Core/App.php

@@ -14,7 +14,6 @@
  */
 namespace Cake\Core;
 
-use Cake\Cache\Cache;
 use Cake\Core\Plugin;
 use Cake\Utility\Inflector;
 
@@ -40,31 +39,11 @@ use Cake\Utility\Inflector;
  * 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
  *
@@ -162,118 +141,4 @@ class App {
 		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_');
-		}
-	}
-
 }

+ 14 - 1
src/Core/Plugin.php

@@ -15,6 +15,7 @@
 namespace Cake\Core;
 
 use Cake\Core\ClassLoader;
+use \DirectoryIterator;
 
 /**
  * Plugin is used to load and locate plugins.
@@ -188,7 +189,19 @@ class Plugin {
  * @return void
  */
 	public static function loadAll(array $options = []) {
-		$plugins = App::objects('Plugin');
+		$plugins = [];
+		foreach (App::path('Plugin') as $path) {
+			if (!is_dir($path)) {
+				continue;
+			}
+			$dir = new DirectoryIterator($path);
+			foreach ($dir as $path) {
+				if ($path->isDir() && !$path->isDot()) {
+					$plugins[] = $path->getBaseName();
+				}
+			}
+		}
+
 		foreach ($plugins as $p) {
 			$opts = isset($options[$p]) ? $options[$p] : null;
 			if ($opts === null && isset($options[0])) {

+ 0 - 1
tests/TestCase/Controller/ControllerTest.php

@@ -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();
 	}

+ 0 - 89
tests/TestCase/Core/AppTest.php

@@ -163,93 +163,4 @@ class AppTest extends TestCase {
 		$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);
-	}
-
 }

+ 0 - 1
tests/TestCase/Core/ConfigureTest.php

@@ -35,7 +35,6 @@ class ConfigureTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 		Cache::disable();
-		App::objects('Plugin', null, true);
 	}
 
 /**

+ 0 - 10
tests/TestCase/Core/PluginTest.php

@@ -25,16 +25,6 @@ use Cake\TestSuite\TestCase;
 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
  *
  * @return void

+ 0 - 2
tests/TestCase/Routing/DispatcherTest.php

@@ -217,8 +217,6 @@ class DispatcherTest extends TestCase {
 		Configure::write('App.webroot', 'webroot');
 		Configure::write('App.namespace', 'TestApp');
 
-		App::objects('Plugin', null, false);
-
 		$this->dispatcher = new TestDispatcher();
 		$this->dispatcher->addFilter(new ControllerFactoryFilter());
 	}

+ 0 - 2
tests/TestCase/View/ViewTest.php

@@ -294,8 +294,6 @@ class ViewTest extends TestCase {
 		$this->ThemePostsController->index();
 		$this->ThemeView = $this->ThemePostsController->createView();
 
-		App::objects('Plugin', null, false);
-
 		Plugin::load(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']);
 		Configure::write('debug', true);
 	}