Skip to content

Commit

Permalink
Use Plugin::load() when creating classless plugins.
Browse files Browse the repository at this point in the history
I completely missed that Plugin::load() does a bunch of work to generate
the correct paths for plugins. By using Plugin::load() and fetching the
created plugin we can leverage the same logic. There is an argument to
be had around deprecating Plugin::load(). Given the troubles plugin
classes have given us I'm not comfortable in doing that right now.

Refs #122271
  • Loading branch information
markstory committed Jun 20, 2018
1 parent b32cf61 commit 7ceefd9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/Http/BaseApplication.php
Expand Up @@ -125,11 +125,12 @@ public function makePlugin($name, array $config)
if (strpos($className, '\\') === false) {
$className = str_replace('/', '\\', $className) . '\\' . 'Plugin';
}
if (!class_exists($className)) {
$config['name'] = $name;
$className = BasePlugin::class;
if (class_exists($className)) {
$plugin = new $className($config);
} else {
Plugin::load($name, $config);
$plugin = $this->plugins->get($name);
}
$plugin = new $className($config);
if (!$plugin instanceof PluginInterface) {
throw new InvalidArgumentException("The `{$name}` plugin does not implement Cake\Core\PluginInterface.");
}
Expand Down
17 changes: 15 additions & 2 deletions tests/TestCase/Http/BaseApplicationTest.php
Expand Up @@ -84,9 +84,22 @@ public function testInvoke()
public function testAddPluginUnknownClass()
{
$app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
$app->addPlugin('SomethingBad');
$plugin = $app->getPlugins()->get('SomethingBad');
$app->addPlugin('PluginJs');
$plugin = $app->getPlugins()->get('PluginJs');
$this->assertInstanceOf(BasePlugin::class, $plugin);

$this->assertEquals(
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
$plugin->getPath()
);
$this->assertEquals(
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
$plugin->getConfigPath()
);
$this->assertEquals(
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
$plugin->getClassPath()
);
}

/**
Expand Down

0 comments on commit 7ceefd9

Please sign in to comment.