Browse Source

Added 'classBase' option to Plugin::load() options.

This allows you to customize the directory under which folders containing
class files are nested.
ADmad 11 years ago
parent
commit
1760782b28

+ 1 - 1
src/Console/Command/BakeShell.php

@@ -108,7 +108,7 @@ class BakeShell extends Shell {
 		foreach (Plugin::loaded() as $plugin) {
 			$tasks = $this->_findTasks(
 				$tasks,
-				Plugin::path($plugin) . 'src' . DS,
+				Plugin::classPath($plugin),
 				Plugin::getNamespace($plugin),
 				$plugin
 			);

+ 1 - 1
src/Console/Command/Task/TestTask.php

@@ -154,7 +154,7 @@ class TestTask extends BakeTask {
 		$classes = [];
 		$base = APP;
 		if ($this->plugin) {
-			$base = Plugin::path($this->plugin) . 'src' . DS;
+			$base = Plugin::classPath($this->plugin);
 		}
 		$path = $base . str_replace('\\', DS, $namespace);
 		$folder = new Folder($path);

+ 9 - 4
src/Core/Plugin.php

@@ -101,6 +101,8 @@ class Plugin {
  * - `namespace` - string - A custom namespace for the plugin. It will default to the plugin name.
  * - `ignoreMissing` - boolean - Set to true to ignore missing bootstrap/routes files.
  * - `path` - string - The path the plugin can be found on. If empty the default plugin path (App.pluginPaths) will be used.
+ * - `classBase` - The path relative to `path` which contains the folders with class files.
+ *    Defaults to "src".
  * - `autoload` - boolean - Whether or not you want an autoloader registered. This defaults to false. The framework
  *   assumes you have configured autoloaders using composer. However, if your application source tree is made up of
  *   plugins, this can be a useful option.
@@ -124,6 +126,7 @@ class Plugin {
 			'bootstrap' => false,
 			'routes' => false,
 			'namespace' => str_replace('/', '\\', $plugin),
+			'classBase' => 'src',
 			'ignoreMissing' => false
 		];
 		if (empty($config['path'])) {
@@ -146,6 +149,8 @@ class Plugin {
 			throw new Error\MissingPluginException(['plugin' => $plugin]);
 		}
 
+		$config['classPath'] = $config['path'] . $config['classBase'] . DS;
+
 		static::$_plugins[$plugin] = $config;
 
 		if ($config['bootstrap'] === true) {
@@ -159,7 +164,7 @@ class Plugin {
 			}
 			static::$_loader->addNamespace(
 				$config['namespace'],
-				$config['path'] . 'src' . DS
+				$config['path'] . $config['classBase'] . DS
 			);
 			static::$_loader->addNamespace(
 				$config['namespace'] . '\Test',
@@ -228,7 +233,7 @@ class Plugin {
 		if (empty(static::$_plugins[$plugin])) {
 			throw new Error\MissingPluginException(['plugin' => $plugin]);
 		}
-		return static::$_plugins[$plugin]['path'] . 'src' . DS;
+		return static::$_plugins[$plugin]['classPath'];
 	}
 
 /**
@@ -262,7 +267,7 @@ class Plugin {
 		$path = static::path($plugin);
 		if ($config['bootstrap'] === true) {
 			return static::_includeFile(
-				$path . 'src' . DS . 'Config' . DS . 'bootstrap.php',
+				$config['classPath'] . 'Config' . DS . 'bootstrap.php',
 				$config['ignoreMissing']
 			);
 		}
@@ -287,7 +292,7 @@ class Plugin {
 			return false;
 		}
 		return (bool)static::_includeFile(
-			static::path($plugin) . 'src' . DS . 'Config' . DS . 'routes.php',
+			$config['classPath'] . 'Config' . DS . 'routes.php',
 			$config['ignoreMissing']
 		);
 	}

+ 6 - 4
tests/TestCase/Core/PluginTest.php

@@ -281,7 +281,7 @@ class PluginTest extends TestCase {
  */
 	public function testLoadAll() {
 		Plugin::loadAll();
-		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
+		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
 		$this->assertEquals($expected, Plugin::loaded());
 	}
 
@@ -304,7 +304,7 @@ class PluginTest extends TestCase {
 	public function testLoadAllWithDefaults() {
 		$defaults = array('bootstrap' => true, 'ignoreMissing' => true);
 		Plugin::loadAll(array($defaults));
-		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
+		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
 		$this->assertEquals($expected, Plugin::loaded());
 		$this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
 		$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
@@ -320,16 +320,18 @@ class PluginTest extends TestCase {
 	public function testLoadAllWithDefaultsAndOverride() {
 		Plugin::loadAll(array(
 			array('bootstrap' => true, 'ignoreMissing' => true),
-			'TestPlugin' => array('routes' => true)
+			'TestPlugin' => array('routes' => true),
+			'TestPluginFour' => array('bootstrap' => true, 'classBase' => '')
 		));
 		Plugin::routes();
 
-		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
+		$expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
 		$this->assertEquals($expected, Plugin::loaded());
 		$this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
 		$this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
 		$this->assertEquals(null, Configure::read('PluginTest.test_plugin.bootstrap'));
 		$this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
+		$this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
 
 		// TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
 		$this->assertEquals(null, Configure::read('PluginTest.test_plugin_three.bootstrap'));

+ 6 - 0
tests/test_app/Plugin/TestPluginFour/Config/bootstrap.php

@@ -0,0 +1,6 @@
+<?php
+namespace TestPluginFour\Config;
+
+use Cake\Core\Configure;
+
+Configure::write('PluginTest.test_plugin_four.bootstrap', 'loaded plugin four bootstrap');