|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands\Plugin; |
| 4 | + |
| 5 | +use App\Enums\PluginCategory; |
| 6 | +use App\Enums\PluginStatus; |
| 7 | +use Illuminate\Console\Command; |
| 8 | +use Illuminate\Filesystem\Filesystem; |
| 9 | +use Illuminate\Support\Str; |
| 10 | + |
| 11 | +class MakePluginCommand extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'p:plugin:make |
| 14 | + {--name=} |
| 15 | + {--author=} |
| 16 | + {--description=} |
| 17 | + {--category=} |
| 18 | + {--url=} |
| 19 | + {--updateUrl=} |
| 20 | + {--panels=} |
| 21 | + {--panelVersion=}'; |
| 22 | + |
| 23 | + protected $description = 'Create a new plugin'; |
| 24 | + |
| 25 | + public function __construct(private Filesystem $filesystem) |
| 26 | + { |
| 27 | + parent::__construct(); |
| 28 | + } |
| 29 | + |
| 30 | + public function handle(): void |
| 31 | + { |
| 32 | + $name = $this->option('name') ?? $this->ask('Name'); |
| 33 | + $name = preg_replace('/[^A-Za-z0-9 ]/', '', Str::ascii($name)); |
| 34 | + |
| 35 | + $id = Str::slug($name); |
| 36 | + |
| 37 | + if ($this->filesystem->exists(plugin_path($id))) { |
| 38 | + $this->error('Plugin with that name already exists!'); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + $author = $this->option('author') ?? $this->ask('Author', cache('plugin.author')); |
| 44 | + $author = preg_replace('/[^A-Za-z0-9 ]/', '', Str::ascii($author)); |
| 45 | + cache()->forever('plugin.author', $author); |
| 46 | + |
| 47 | + $namespace = Str::studly($author) . '\\' . Str::studly($name); |
| 48 | + $class = Str::studly($name . 'Plugin'); |
| 49 | + |
| 50 | + if (class_exists('\\' . $namespace . '\\' . $class)) { |
| 51 | + $this->error('Plugin class with that name already exists!'); |
| 52 | + |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $this->info('Creating Plugin "' . $name . '" (' . $id . ') by ' . $author); |
| 57 | + |
| 58 | + $description = $this->option('description') ?? $this->ask('Description (can be empty)'); |
| 59 | + |
| 60 | + $category = $this->option('category') ?? $this->choice('Category', collect(PluginCategory::cases())->mapWithKeys(fn (PluginCategory $category) => [$category->value => $category->getLabel()])->toArray(), PluginCategory::Plugin->value); |
| 61 | + |
| 62 | + if (!PluginCategory::tryFrom($category)) { |
| 63 | + $this->error('Unknown plugin category!'); |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + $url = $this->option('url') ?? $this->ask('URL (can be empty)'); |
| 69 | + $updateUrl = $this->option('updateUrl') ?? $this->ask('Update URL (can be empty)'); |
| 70 | + |
| 71 | + $panels = $this->option('panels'); |
| 72 | + if (!$panels) { |
| 73 | + if ($this->confirm('Should the plugin be available on all panels?', true)) { |
| 74 | + $panels = null; |
| 75 | + } else { |
| 76 | + $panels = $this->choice('Panels (comma separated list)', [ |
| 77 | + 'admin' => 'Admin Area', |
| 78 | + 'server' => 'Client Area', |
| 79 | + 'app' => 'Server List', |
| 80 | + ], multiple: true); |
| 81 | + } |
| 82 | + } |
| 83 | + $panels = is_string($panels) ? explode(',', $panels) : $panels; |
| 84 | + |
| 85 | + $panelVersion = $this->option('panelVersion'); |
| 86 | + if (!$panelVersion) { |
| 87 | + $panelVersion = $this->ask('Required panel version (leave empty for no constraint)', config('app.version') === 'canary' ? null : config('app.version')); |
| 88 | + |
| 89 | + if ($panelVersion && $this->confirm("Should the version constraint be minimal instead of strict? ($panelVersion or higher instead of only $panelVersion)")) { |
| 90 | + $panelVersion = "^$panelVersion"; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $composerPackages = null; |
| 95 | + // TODO: ask for composer packages? |
| 96 | +
|
| 97 | + // Create base directory |
| 98 | + $this->filesystem->makeDirectory(plugin_path($id)); |
| 99 | + |
| 100 | + // Write plugin.json |
| 101 | + $this->filesystem->put(plugin_path($id, 'plugin.json'), json_encode([ |
| 102 | + 'id' => $id, |
| 103 | + 'name' => $name, |
| 104 | + 'author' => $author, |
| 105 | + 'version' => '1.0.0', |
| 106 | + 'description' => $description, |
| 107 | + 'category' => $category, |
| 108 | + 'url' => $url, |
| 109 | + 'update_url' => $updateUrl, |
| 110 | + 'namespace' => $namespace, |
| 111 | + 'class' => $class, |
| 112 | + 'panels' => $panels, |
| 113 | + 'panel_version' => $panelVersion, |
| 114 | + 'composer_packages' => $composerPackages, |
| 115 | + 'meta' => [ |
| 116 | + 'status' => PluginStatus::Enabled, |
| 117 | + 'status_message' => null, |
| 118 | + ], |
| 119 | + ], JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 120 | + |
| 121 | + // Create src directory and create main class |
| 122 | + $this->filesystem->makeDirectory(plugin_path($id, 'src')); |
| 123 | + $this->filesystem->put(plugin_path($id, 'src', $class . '.php'), Str::replace(['$namespace$', '$class$', '$id$'], [$namespace, $class, $id], file_get_contents(__DIR__ . '/Plugin.stub'))); |
| 124 | + |
| 125 | + // Create Providers directory and create service provider |
| 126 | + $this->filesystem->makeDirectory(plugin_path($id, 'src', 'Providers')); |
| 127 | + $this->filesystem->put(plugin_path($id, 'src', 'Providers', $class . 'Provider.php'), Str::replace(['$namespace$', '$class$'], [$namespace, $class], file_get_contents(__DIR__ . '/PluginProvider.stub'))); |
| 128 | + |
| 129 | + // Create config directory and create config file |
| 130 | + $this->filesystem->makeDirectory(plugin_path($id, 'config')); |
| 131 | + $this->filesystem->put(plugin_path($id, 'config', $id . '.php'), Str::replace(['$name$'], [$name], file_get_contents(__DIR__ . '/PluginConfig.stub'))); |
| 132 | + |
| 133 | + $this->info('Plugin created.'); |
| 134 | + } |
| 135 | +} |
0 commit comments