Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/AppStore/src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ class CreateCommand extends AbstractCommand
public function __invoke(): int
{
$path = $this->input->getArgument('path');
$name = $this->input->getOption('name');
$type = $this->input->getOption('type') ?? 'mix';
$type = PluginTypeEnum::fromValue($type);
if (empty($name)) {
$this->output->error('Plugin name is empty');
return AbstractCommand::FAILURE;
}
if ($type === null) {
$this->output->error('Plugin type is empty');
return AbstractCommand::FAILURE;
Expand All @@ -47,6 +42,13 @@ public function __invoke(): int
$this->output->error(\sprintf('Plugin directory %s already exists', $path));
return AbstractCommand::FAILURE;
}

$path = str_replace('\\', '/', trim((string) $path));
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, '..')) {
$this->output->error('Invalid plugin path. Use: organization/plugin-name (letters or digits, dash/underscore allowed, no dot).');
return AbstractCommand::FAILURE;
}

$createDirectors = [
$pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders', $pluginPath . '/web',
];
Expand All @@ -56,23 +58,23 @@ public function __invoke(): int
}
}

$this->createMineJson($pluginPath, $name, $type);
$this->createMineJson($pluginPath, $type);
return AbstractCommand::SUCCESS;
}

public function createNamespace(string $path, string $name): string
public function createNamespace(string $path): string
{
$pluginPath = Str::replace(Plugin::PLUGIN_PATH . '/', '', $path);
[$orgName] = explode('/', $pluginPath);
return 'Plugin\\' . Str::studly($orgName) . '\\' . Str::studly($name);
[$orgName, $extName] = explode('/', $pluginPath);
return 'Plugin\\' . Str::studly($orgName) . '\\' . Str::studly($extName);
}

public function createMineJson(string $path, string $name, PluginTypeEnum $pluginType): void
public function createMineJson(string $path, PluginTypeEnum $pluginType): void
{
$pluginPath = Str::replace(Plugin::PLUGIN_PATH . '/', '', $path);

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

$this->createInstallScript($namespace, $path);
$this->createUninstallScript($namespace, $path);
Expand All @@ -92,7 +94,7 @@ public function createMineJson(string $path, string $name, PluginTypeEnum $plugi
$output->composer = [
'require' => [],
'psr-4' => [
'\\' . $namespace . '\\' => 'src',
$namespace . '\\' => 'src',
],
'installScript' => $namespace . '\InstallScript',
'uninstallScript' => $namespace . '\UninstallScript',
Expand All @@ -106,7 +108,7 @@ public function createMineJson(string $path, string $name, PluginTypeEnum $plugi
],
];
}
$output = Json::encode($output);
$output = Json::encode($output, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT);
file_put_contents($path . '/mine.json', $output);
$this->output->success(\sprintf('%s 创建成功', $path . '/mine.json'));
}
Expand Down
2 changes: 1 addition & 1 deletion src/AppStore/src/Command/Stub/UninstallScript.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace %namespace%;

class InstallScript {
class UninstallScript {

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