Skip to content

Commit

Permalink
Add support for loading plugin with name of style `Vendorname/Pluginn…
Browse files Browse the repository at this point in the history
…ame`
  • Loading branch information
ADmad committed Jun 2, 2014
1 parent dc0f75c commit 305f316
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
13 changes: 10 additions & 3 deletions src/Core/Plugin.php
Expand Up @@ -119,13 +119,20 @@ public static function load($plugin, array $config = []) {
return;
}

$config += ['autoload' => false, 'bootstrap' => false, 'routes' => false, 'namespace' => $plugin, 'ignoreMissing' => false];
$config += [
'autoload' => false,
'bootstrap' => false,
'routes' => false,
'namespace' => str_replace('/', '\\', $plugin),
'ignoreMissing' => false
];
if (empty($config['path'])) {
$paths = App::path('Plugin');
foreach ($paths as $path) {
$namespacePath = str_replace('\\', DS, $config['namespace']);
if (is_dir($path . $plugin)) {
$config += ['path' => $path . $plugin . DS];
$pluginPath = str_replace('/', DS, $plugin);
if (is_dir($path . $pluginPath)) {
$config += ['path' => $path . $pluginPath . DS];
break;
}
if ($plugin !== $config['namespace'] && is_dir($path . $namespacePath)) {
Expand Down
25 changes: 19 additions & 6 deletions tests/TestCase/Core/PluginTest.php
Expand Up @@ -55,8 +55,8 @@ public function testGetNamespace() {

$this->assertEquals('TestPluginTwo', Plugin::getNamespace('TestPluginTwo'));

Plugin::load('TestPluginThree', array('namespace' => 'Company\TestPluginThree'));
$this->assertEquals('Company\TestPluginThree', Plugin::getNamespace('TestPluginThree'));
Plugin::load('Company/TestPluginThree');
$this->assertEquals('Company\TestPluginThree', Plugin::getNamespace('Company/TestPluginThree'));

Plugin::load('CustomPlugin', array('namespace' => 'Company\TestPluginThree'));
$this->assertEquals('Company\TestPluginThree', Plugin::getNamespace('CustomPlugin'));
Expand Down Expand Up @@ -102,9 +102,7 @@ public function testUnload() {
*/
public function testLoadSingleWithAutoload() {
$this->assertFalse(class_exists('Company\TestPluginThree\Utility\Hello'));
Plugin::load('TestPluginThree', [
'namespace' => 'Company\TestPluginThree',
'path' => TEST_APP . 'Plugin/Company/TestPluginThree',
Plugin::load('Company/TestPluginThree', [
'autoload' => true,
]);
$this->assertTrue(
Expand All @@ -122,6 +120,15 @@ public function testLoadSingleWithBootstrap() {
Plugin::load('TestPlugin', array('bootstrap' => true));
$this->assertTrue(Plugin::loaded('TestPlugin'));
$this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));

Plugin::load('Company/TestPluginThree', array('bootstrap' => true));
$this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
$this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));

Configure::delete('PluginTest.test_plugin_three.bootstrap');
Plugin::load('NewName', array('namespace' => 'Company\TestPluginThree', 'bootstrap' => true));
$this->assertTrue(Plugin::loaded('NewName'));
$this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
}

/**
Expand Down Expand Up @@ -219,12 +226,15 @@ public function testLoadNotFound() {
* @return void
*/
public function testPath() {
Plugin::load(array('TestPlugin', 'TestPluginTwo'));
Plugin::load(array('TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree'));
$expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
$this->assertPathEquals(Plugin::path('TestPlugin'), $expected);

$expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
$this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);

$expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
$this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
}

/**
Expand Down Expand Up @@ -293,6 +303,9 @@ public function testLoadAllWithDefaultsAndOverride() {
$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'));

// TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
$this->assertEquals(null, Configure::read('PluginTest.test_plugin_three.bootstrap'));
}

}
@@ -0,0 +1,6 @@
<?php
namespace Company\TestPluginThree\Config;

use Cake\Core\Configure;

Configure::write('PluginTest.test_plugin_three.bootstrap', 'loaded plugin three bootstrap');

0 comments on commit 305f316

Please sign in to comment.