Skip to content

Commit

Permalink
Remove plugin namespace aliasing and shortening.
Browse files Browse the repository at this point in the history
Aliasing a plugin namespace in app won't work as the plugin itself
could be using plugin notation with hardcoded name.
  • Loading branch information
ADmad committed Aug 11, 2014
1 parent 555d941 commit 9aca863
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 50 deletions.
27 changes: 3 additions & 24 deletions src/Core/Plugin.php
Expand Up @@ -123,23 +123,18 @@ public static function load($plugin, array $config = []) {
'autoload' => false,
'bootstrap' => false,
'routes' => false,
'namespace' => $plugin,
'classBase' => 'src',
'ignoreMissing' => false
];

if (empty($config['path'])) {
$paths = App::path('Plugin');
foreach ($paths as $path) {
$namespacePath = str_replace('\\', DS, $config['namespace']);
$pluginPath = str_replace('\\', DS, $plugin);
if (is_dir($path . $pluginPath)) {
$config += ['path' => $path . $pluginPath . DS];
break;
}
if ($plugin !== $config['namespace'] && is_dir($path . $namespacePath)) {
$config += ['path' => $path . $namespacePath . DS];
break;
}
}
}

Expand All @@ -164,11 +159,11 @@ public static function load($plugin, array $config = []) {
static::$_loader->register();
}
static::$_loader->addNamespace(
$config['namespace'],
$plugin,
$config['path'] . $config['classBase'] . DS
);
static::$_loader->addNamespace(
$config['namespace'] . '\Test',
$plugin . '\Test',
$config['path'] . 'tests' . DS
);
}
Expand Down Expand Up @@ -251,22 +246,6 @@ public static function configPath($plugin) {
return static::$_plugins[$plugin]['configPath'];
}

/**
* Return the namespace for a plugin
*
* If a plugin is unknown, the plugin name will be used as the namespace.
* This lets you access vendor libraries or unloaded plugins using `Plugin.Class`.
*
* @param string $plugin name of the plugin in CamelCase format
* @return string namespace to the plugin
*/
public static function getNamespace($plugin) {
if (empty(static::$_plugins[$plugin])) {
return $plugin;
}
return static::$_plugins[$plugin]['namespace'];
}

/**
* Loads the bootstrapping files for a plugin, or calls the initialization setup in the configuration
*
Expand Down
29 changes: 3 additions & 26 deletions tests/TestCase/Core/PluginTest.php
Expand Up @@ -44,24 +44,6 @@ public function tearDown() {
Plugin::unload();
}

/**
* Test the plugin namespace
*
* @return void
*/
public function testGetNamespace() {
Plugin::load('TestPlugin');
$this->assertEquals('TestPlugin', Plugin::getNamespace('TestPlugin'));

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

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'));
}

/**
* Tests loading a single plugin
*
Expand Down Expand Up @@ -124,11 +106,6 @@ public function testLoadSingleWithBootstrap() {
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 @@ -291,9 +268,9 @@ public function testLoadAll() {
* @return void
*/
public function testLoadAllWithPluginAlreadyLoaded() {
Plugin::load('PluginJs', ['namespace' => 'Company\TestPluginJs']);
Plugin::loadAll();
$this->assertEquals('Company\TestPluginJs', Plugin::getNamespace('PluginJs'));
Plugin::load('Company\TestPluginThree', ['bootstrap' => false]);
Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
$this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
}

/**
Expand Down

5 comments on commit 9aca863

@jadb
Copy link
Contributor

@jadb jadb commented on 9aca863 Aug 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this change, I could have a company plugin under a company namespace (i.e. Acme\MyPlugin) and load it using:

Plugin::load('MyPlugin', ['namespace' => 'Acme\\Plugin', 'autoload' => true]);

Now, I can't load it anymore. Looking at the tests, looks like I would need the plugin to reside under the 'plugin/Acme/MyPlugin'. Right now, using 'cakephp-plugin' in composer.json, it gets automatically installed to 'plugin/MyPlugin'.

What's the best way to get this to work again?

@ADmad
Copy link
Member Author

@ADmad ADmad commented on 9aca863 Aug 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specify installer-name similar to this.

@jadb
Copy link
Contributor

@jadb jadb commented on 9aca863 Aug 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Just to make sure, no other way than by updating the plugins? I was trying not to have to do that ;)

@ADmad
Copy link
Member Author

@ADmad ADmad commented on 9aca863 Aug 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment if you are using vendor namespace only option is to update your composer.json. We are looking into ways to possibly avoid that.

@jadb
Copy link
Contributor

@jadb jadb commented on 9aca863 Aug 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks.

Please sign in to comment.