Skip to content

Commit f09abdf

Browse files
authored
fix(AppStore/CreateCommand): correct namespace formatting and improve JSON output (#158)
* fix(AppStore/CreateCommand): correct namespace formatting and improve JSON output; fix class name in UninstallScript.stub * fix(CreateCommand): validate plugin path format and refactor name handling in JSON and namespace generation * fix(CreateCommand): remove fallback namespace generation to ensure correct namespace usage * fix(CreateCommand): improve plugin path validation to enforce stricter format and prevent invalid characters
1 parent d09e1bc commit f09abdf

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

src/Command/CreateCommand.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@ class CreateCommand extends AbstractCommand
3030
public function __invoke(): int
3131
{
3232
$path = $this->input->getArgument('path');
33-
$name = $this->input->getOption('name');
3433
$type = $this->input->getOption('type') ?? 'mix';
3534
$type = PluginTypeEnum::fromValue($type);
36-
if (empty($name)) {
37-
$this->output->error('Plugin name is empty');
38-
return AbstractCommand::FAILURE;
39-
}
4035
if ($type === null) {
4136
$this->output->error('Plugin type is empty');
4237
return AbstractCommand::FAILURE;
@@ -47,6 +42,13 @@ public function __invoke(): int
4742
$this->output->error(\sprintf('Plugin directory %s already exists', $path));
4843
return AbstractCommand::FAILURE;
4944
}
45+
46+
$path = str_replace('\\', '/', trim((string) $path));
47+
if (! preg_match('/^[A-Za-z0-9](?:[A-Za-z0-9_-]*[A-Za-z0-9])?\/[A-Za-z0-9](?:[A-Za-z0-9_-]*[A-Za-z0-9])?$/', $path) || str_contains($path, '..')) {
48+
$this->output->error('Invalid plugin path. Use: organization/plugin-name (letters or digits, dash/underscore allowed, no dot).');
49+
return AbstractCommand::FAILURE;
50+
}
51+
5052
$createDirectors = [
5153
$pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders', $pluginPath . '/web',
5254
];
@@ -56,23 +58,23 @@ public function __invoke(): int
5658
}
5759
}
5860

59-
$this->createMineJson($pluginPath, $name, $type);
61+
$this->createMineJson($pluginPath, $type);
6062
return AbstractCommand::SUCCESS;
6163
}
6264

63-
public function createNamespace(string $path, string $name): string
65+
public function createNamespace(string $path): string
6466
{
6567
$pluginPath = Str::replace(Plugin::PLUGIN_PATH . '/', '', $path);
66-
[$orgName] = explode('/', $pluginPath);
67-
return 'Plugin\\' . Str::studly($orgName) . '\\' . Str::studly($name);
68+
[$orgName, $extName] = explode('/', $pluginPath);
69+
return 'Plugin\\' . Str::studly($orgName) . '\\' . Str::studly($extName);
6870
}
6971

70-
public function createMineJson(string $path, string $name, PluginTypeEnum $pluginType): void
72+
public function createMineJson(string $path, PluginTypeEnum $pluginType): void
7173
{
7274
$pluginPath = Str::replace(Plugin::PLUGIN_PATH . '/', '', $path);
7375

7476
$output = new \stdClass();
75-
$output->name = $pluginPath ?? $name;
77+
$output->name = $pluginPath;
7678
$output->version = '1.0.0';
7779
$output->type = $pluginType->value;
7880
$output->description = $this->input->getOption('description') ?: 'This is a sample plugin';
@@ -83,7 +85,7 @@ public function createMineJson(string $path, string $name, PluginTypeEnum $plugi
8385
],
8486
];
8587
if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) {
86-
$namespace = $this->createNamespace($path, $name) ?? 'Plugin\\' . ucwords(str_replace('/', '\\', Str::studly($name)));
88+
$namespace = $this->createNamespace($path);
8789

8890
$this->createInstallScript($namespace, $path);
8991
$this->createUninstallScript($namespace, $path);
@@ -92,7 +94,7 @@ public function createMineJson(string $path, string $name, PluginTypeEnum $plugi
9294
$output->composer = [
9395
'require' => [],
9496
'psr-4' => [
95-
'\\' . $namespace . '\\' => 'src',
97+
$namespace . '\\' => 'src',
9698
],
9799
'installScript' => $namespace . '\InstallScript',
98100
'uninstallScript' => $namespace . '\UninstallScript',
@@ -106,7 +108,7 @@ public function createMineJson(string $path, string $name, PluginTypeEnum $plugi
106108
],
107109
];
108110
}
109-
$output = Json::encode($output);
111+
$output = Json::encode($output, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT);
110112
file_put_contents($path . '/mine.json', $output);
111113
$this->output->success(\sprintf('%s 创建成功', $path . '/mine.json'));
112114
}

src/Command/Stub/UninstallScript.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace %namespace%;
44

5-
class InstallScript {
5+
class UninstallScript {
66

77
public function __invoke(){
88
echo "Commands to be executed when uninstalling the plug-in";

0 commit comments

Comments
 (0)