diff --git a/src/Core/PluginApp.php b/src/Core/PluginApp.php index 05377ee8b9c..ff91308b7cc 100644 --- a/src/Core/PluginApp.php +++ b/src/Core/PluginApp.php @@ -13,6 +13,8 @@ */ namespace Cake\Core; +use ReflectionClass; + /** * Base Plugin Class * @@ -50,6 +52,13 @@ class PluginApp implements PluginInterface */ protected $consoleEnabled = true; + /** + * The path to this plugin. + * + * @var string + */ + protected $path; + /** * Constructor * @@ -62,6 +71,9 @@ public function __construct(array $options = []) $this->{"{$key}Enabled"} = (bool)$options[$key]; } } + if (isset($options['path'])) { + $this->path = $options['path']; + } $this->initialize(); } @@ -84,6 +96,25 @@ public function getName() return implode('/', $parts); } + /** + * {@inheritDoc} + */ + public function getPath() + { + if ($this->path) { + return $this->path; + } + $reflection = new ReflectionClass($this); + $path = dirname($reflection->getFileName()); + + // Trim off src + if (substr($path, -3) === 'src') { + $path = substr($path, 0, -3); + } + + return rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; + } + /** * {@inheritdoc} */ diff --git a/src/Core/PluginInterface.php b/src/Core/PluginInterface.php index b61e40932c7..48dc640a7e2 100644 --- a/src/Core/PluginInterface.php +++ b/src/Core/PluginInterface.php @@ -25,6 +25,13 @@ interface PluginInterface */ public function getName(); + /** + * Get the filesystem path to this plugin + * + * @return void + */ + public function getPath(); + /** * Load all the application configuration and bootstrap logic. * diff --git a/tests/TestCase/Core/PluginAppTest.php b/tests/TestCase/Core/PluginAppTest.php index 76db98d4a0c..c279f3e2a6f 100644 --- a/tests/TestCase/Core/PluginAppTest.php +++ b/tests/TestCase/Core/PluginAppTest.php @@ -90,4 +90,24 @@ public function testConstructorArguments() $this->assertFalse($plugin->isConsoleEnabled()); $this->assertFalse($plugin->isMiddlewareEnabled()); } + + public function testGetPathBaseClass() + { + $plugin = new PluginApp(); + + $expected = CAKE . 'Core' . DS; + $this->assertSame($expected, $plugin->getPath()); + } + + public function testGetPathOptionValue() + { + $plugin = new PluginApp(['path' => '/some/path']); + $this->assertSame('/some/path', $plugin->getPath()); + } + + public function testGetPathSubclass() + { + $plugin = new TestPlugin(); + $this->assertSame(TEST_APP . 'Plugin/TestPlugin' . DS, $plugin->getPath()); + } }