Skip to content

Commit

Permalink
Add getPath() to plugins.
Browse files Browse the repository at this point in the history
Plugins need to know their paths in order to be compatible with the
current features offered by Plugin.
  • Loading branch information
markstory committed Feb 13, 2018
1 parent 4ebf10b commit 4efb602
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Core/PluginApp.php
Expand Up @@ -13,6 +13,8 @@
*/
namespace Cake\Core;

use ReflectionClass;

/**
* Base Plugin Class
*
Expand Down Expand Up @@ -50,6 +52,13 @@ class PluginApp implements PluginInterface
*/
protected $consoleEnabled = true;

/**
* The path to this plugin.
*
* @var string
*/
protected $path;

/**
* Constructor
*
Expand All @@ -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();
}
Expand All @@ -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}
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Core/PluginInterface.php
Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Core/PluginAppTest.php
Expand Up @@ -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());
}
}

0 comments on commit 4efb602

Please sign in to comment.