Browse Source

Add Plugin::classPath() method.

This method will return path to plugin's folder containing folders with class files.
ADmad 11 years ago
parent
commit
808f4aceb0

+ 1 - 3
src/Configure/Engine/IniConfig.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * IniConfig
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -220,7 +218,7 @@ class IniConfig implements ConfigEngineInterface {
 		}
 
 		if ($plugin) {
-			$file = App::pluginPath($plugin) . 'Config' . DS . $key;
+			$file = App::path('Config', $plugin)[0] . $key;
 		} else {
 			$file = $this->_path . $key;
 		}

+ 3 - 5
src/Configure/Engine/PhpConfig.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * PhpConfig file
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -51,8 +49,8 @@ class PhpConfig implements ConfigEngineInterface {
 /**
  * Read a config file and return its contents.
  *
- * Files with `.` in the name will be treated as values in plugins. Instead of reading from
- * the initialized path, plugin keys will be located using App::pluginPath().
+ * Files with `.` in the name will be treated as values in plugins. Instead of
+ * reading from the initialized path, plugin keys will be located using App::path().
  *
  * @param string $key The identifier to read from. If the key has a . it will be treated
  *  as a plugin prefix.
@@ -108,7 +106,7 @@ class PhpConfig implements ConfigEngineInterface {
 		$key .= '.php';
 
 		if ($plugin) {
-			$file = App::pluginPath($plugin) . 'Config' . DS . $key;
+			$file = App::path('Config', $plugin)[0] . $key;
 		} else {
 			$file = $this->_path . $key;
 		}

+ 4 - 3
src/Core/App.php

@@ -15,6 +15,7 @@
 namespace Cake\Core;
 
 use Cake\Cache\Cache;
+use Cake\Core\Plugin;
 use Cake\Error\ErrorHandler;
 use Cake\Utility\Inflector;
 
@@ -140,7 +141,7 @@ class App {
 			return (array)Configure::read('App.paths.templates');
 		}
 		if (!empty($plugin)) {
-			return [static::pluginPath($plugin) . $type . DS];
+			return [Plugin::classPath($plugin) . $type . DS];
 		}
 		return [APP . $type . DS];
 	}
@@ -154,12 +155,12 @@ class App {
  *
  * Will return the full path to 'MyPlugin' plugin
  *
- * @param string $plugin CamelCased/lower_cased plugin name to find the path of.
+ * @param string $plugin Name of the plugin in CamelCase format.
  * @return string full path to the plugin.
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/app.html#App::pluginPath
  */
 	public static function pluginPath($plugin) {
-		return Plugin::path($plugin) . 'src' . DS;
+		return Plugin::path($plugin);
 	}
 
 /**

+ 14 - 0
src/Core/Plugin.php

@@ -218,6 +218,20 @@ class Plugin {
 	}
 
 /**
+ * Returns the filesystem path for plugin's folder containing class folders.
+ *
+ * @param string $plugin name of the plugin in CamelCase format.
+ * @return string Path to the plugin folder container class folders.
+ * @throws \Cake\Core\Error\MissingPluginException If plugin has not been loaded.
+ */
+	public static function classPath($plugin) {
+		if (empty(static::$_plugins[$plugin])) {
+			throw new Error\MissingPluginException(['plugin' => $plugin]);
+		}
+		return static::$_plugins[$plugin]['path'] . 'src' . DS;
+	}
+
+/**
  * Return the namespace for a plugin
  *
  * If a plugin is unknown, the plugin name will be used as the namespace.

+ 1 - 1
src/I18n/I18n.php

@@ -385,7 +385,7 @@ class I18n {
 			foreach ($plugins as $plugin) {
 				$pluginDomain = Inflector::underscore($plugin);
 				if ($pluginDomain === $domain) {
-					$searchPaths[] = App::pluginPath($plugin) . 'Locale/';
+					$searchPaths[] = App::path('Locale', $plugin)[0];
 					$searchPaths = array_reverse($searchPaths);
 					break;
 				}

+ 3 - 3
tests/TestCase/Core/AppTest.php

@@ -261,15 +261,15 @@ class AppTest extends TestCase {
 		Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
 
 		$path = App::pluginPath('TestPlugin');
-		$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
+		$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
 		$this->assertPathEquals($expected, $path);
 
 		$path = App::pluginPath('TestPluginTwo');
-		$expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
+		$expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
 		$this->assertPathEquals($expected, $path);
 
 		$path = App::pluginPath('Company/TestPluginThree');
-		$expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
+		$expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
 		$this->assertPathEquals($expected, $path);
 	}
 

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

@@ -248,6 +248,33 @@ class PluginTest extends TestCase {
 	}
 
 /**
+ * Tests that Plugin::classPath() returns the correct path for the loaded plugins
+ *
+ * @return void
+ */
+	public function testClassPath() {
+		Plugin::load(array('TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree'));
+		$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
+		$this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
+
+		$expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
+		$this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
+
+		$expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
+		$this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
+	}
+
+/**
+ * Tests that Plugin::classPath() throws an exception on unknown plugin
+ *
+ * @return void
+ * @expectedException \Cake\Core\Error\MissingPluginException
+ */
+	public function testClassPathNotFound() {
+		Plugin::classPath('TestPlugin');
+	}
+
+/**
  * Tests that Plugin::loadAll() will load all plgins in the configured folder
  *
  * @return void