Skip to content

Commit 7ceefd9

Browse files
committed
Use Plugin::load() when creating classless plugins.
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
1 parent b32cf61 commit 7ceefd9

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/Http/BaseApplication.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,12 @@ public function makePlugin($name, array $config)
125125
if (strpos($className, '\\') === false) {
126126
$className = str_replace('/', '\\', $className) . '\\' . 'Plugin';
127127
}
128-
if (!class_exists($className)) {
129-
$config['name'] = $name;
130-
$className = BasePlugin::class;
128+
if (class_exists($className)) {
129+
$plugin = new $className($config);
130+
} else {
131+
Plugin::load($name, $config);
132+
$plugin = $this->plugins->get($name);
131133
}
132-
$plugin = new $className($config);
133134
if (!$plugin instanceof PluginInterface) {
134135
throw new InvalidArgumentException("The `{$name}` plugin does not implement Cake\Core\PluginInterface.");
135136
}

tests/TestCase/Http/BaseApplicationTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,22 @@ public function testInvoke()
8484
public function testAddPluginUnknownClass()
8585
{
8686
$app = $this->getMockForAbstractClass(BaseApplication::class, [$this->path]);
87-
$app->addPlugin('SomethingBad');
88-
$plugin = $app->getPlugins()->get('SomethingBad');
87+
$app->addPlugin('PluginJs');
88+
$plugin = $app->getPlugins()->get('PluginJs');
8989
$this->assertInstanceOf(BasePlugin::class, $plugin);
90+
91+
$this->assertEquals(
92+
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS,
93+
$plugin->getPath()
94+
);
95+
$this->assertEquals(
96+
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'config' . DS,
97+
$plugin->getConfigPath()
98+
);
99+
$this->assertEquals(
100+
TEST_APP . 'Plugin' . DS . 'PluginJs' . DS . 'src' . DS,
101+
$plugin->getClassPath()
102+
);
90103
}
91104

92105
/**

0 commit comments

Comments
 (0)