|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Moox\Media\Console\Commands; |
| 6 | + |
| 7 | +use Illuminate\Console\Command; |
| 8 | +use Illuminate\Support\Facades\File; |
| 9 | +use Illuminate\Support\Facades\Schema; |
| 10 | +use function Laravel\Prompts\alert; |
| 11 | +use function Laravel\Prompts\confirm; |
| 12 | +use function Laravel\Prompts\info; |
| 13 | +use function Laravel\Prompts\multiselect; |
| 14 | +use function Laravel\Prompts\note; |
| 15 | +use function Laravel\Prompts\warning; |
| 16 | + |
| 17 | +class InstallCommand extends Command |
| 18 | +{ |
| 19 | + /** |
| 20 | + * The name and signature of the console command. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $signature = 'media:install'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The console command description. |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + protected $description = 'Installs Moox Media, publishes configuration, migrations and registers plugins.'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Execute the console command. |
| 35 | + */ |
| 36 | + public function handle(): void |
| 37 | + { |
| 38 | + $this->art(); |
| 39 | + $this->welcome(); |
| 40 | + $this->publishConfiguration(); |
| 41 | + $this->publishMigrations(); |
| 42 | + $this->runMigrations(); |
| 43 | + $this->registerPluginInPanelProvider(); |
| 44 | + $this->sayGoodbye(); |
| 45 | + } |
| 46 | + |
| 47 | + public function art(): void |
| 48 | + { |
| 49 | + info(' |
| 50 | +
|
| 51 | + ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ |
| 52 | + ▓▓▒░░▒▓▓▒▒░░░░░░▒▒▓▓▓▒░░░░░░░▒▓▓ ▓▓▓▓▒░░░░░░░▒▓▓▓▓ ▓▓▓▓▓▒░░░░░░░▒▒▓▓▓▓▓▒▒▒▒▓▓ ▓▓▓▒▒▒▒▓▓ |
| 53 | + ▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▓▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓░░░░░▒▓▓ ▓▓▒░░░░░▓▓ |
| 54 | + ▓▒░░░░░░▒▓▓▓▓▒░░░░░░░▒▓▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓▓▓▒░░░░░░░▓▓▓▓░░░░░░▒▓▓▓▓▓░░░░░░▒▓▓░░░░░▒▓▓▓▓▓░░░░░▒▓▓ |
| 55 | + ▓▒░░░░▓▓▓▓ ▓▓░░░░░▓▓▓ ▓▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▓░░░░░▓░░░░░░▓▓▓▓ ▓▓▓▒░░░░▓▓▓▒░░░░░▓▓▓░░░░░▓▓▓ |
| 56 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▓░░▒░░░░░▓▓▓ ▓▓░░░░▒▓▓▓▓░░░░░░░░░░░▓▓ |
| 57 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓ ▓▓▓░░░░░▒▓▓ ▓▓▒░░░░▓ ▓▓▓░░░░░░░░░▓▓ |
| 58 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓░░░░▒▓▓ ▓▓▒░░░░░▒░░▒▓▓ ▓▓░░░░▒▓▓▓▒░░░░░▒░░░░░▒▓ |
| 59 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓░░░░▒▓▓▓ ▓▓▓▒░░░░░▒▒░░░░░▒▓▓▓ ▓▓▓░░░░░▓▓▓░░░░░▒▓▓▓░░░░░▒▓▓ |
| 60 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓░░░░░░▒▒▓▓▒░░░░░░▒▓▓▓▓░░░░░░░▒▒▓▓▒░░░░░░▓▓▓░░░░░▒▓▓▓▓▓▒░░░░░▓▓ |
| 61 | + ▓▒░░░░▒▓ ▓▓░░░░░▓▓ ▓▓░░░░▒▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▓ ▓▓▓▓▒░░░░░░░░░░░░░▒▓▓▒░░░░░▓▓▓ ▓▓▒░░░░░▒▓ |
| 62 | + ▓▓░░░▒▓▓ ▓▓▒░░░▒▓▓ ▓▓░░░░▓▓ ▓▓▓▓▒░░░░░░▒▒▓▓▓▓ ▓▓▓▓▓▒▒░░░░░▒▒▓▓▓▓▓░░░░▒▓▓ ▓▓▓░░░░▒▓ |
| 63 | + ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓ |
| 64 | +
|
| 65 | + '); |
| 66 | + } |
| 67 | + |
| 68 | + public function welcome(): void |
| 69 | + { |
| 70 | + info('Welcome to Moox Media Installer'); |
| 71 | + } |
| 72 | + |
| 73 | + public function publishConfiguration(): void |
| 74 | + { |
| 75 | + if (confirm('Do you wish to publish the configuration?', true)) { |
| 76 | + if (!File::exists('config/media.php')) { |
| 77 | + info('Publishing Media Configuration...'); |
| 78 | + $this->callSilent('vendor:publish', ['--tag' => 'media-config']); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + warning('The Media config already exists. The config will not be published.'); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public function publishMigrations(): void |
| 88 | + { |
| 89 | + if (confirm('Do you wish to publish the migrations?', true)) { |
| 90 | + if (Schema::hasTable('media')) { |
| 91 | + warning('The media table already exists. The migrations will not be published.'); |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + info('Publishing Media Migrations...'); |
| 97 | + $this->callSilent('vendor:publish', ['--tag' => 'media-migrations']); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public function runMigrations(): void |
| 102 | + { |
| 103 | + if (confirm('Do you wish to run the migrations?', true)) { |
| 104 | + info('Running Media Migrations...'); |
| 105 | + $this->callSilent('migrate'); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public function registerPlugins(string $providerPath): void |
| 110 | + { |
| 111 | + if (File::exists($providerPath)) { |
| 112 | + $content = File::get($providerPath); |
| 113 | + |
| 114 | + $intend = ' '; |
| 115 | + |
| 116 | + $namespace = "\Moox\Media"; |
| 117 | + |
| 118 | + $pluginsToAdd = multiselect( |
| 119 | + label: 'These plugins will be installed:', |
| 120 | + options: ['MediaPlugin', 'MediaCollectionPlugin'], |
| 121 | + default: ['MediaPlugin', 'MediaCollectionPlugin'], |
| 122 | + ); |
| 123 | + |
| 124 | + $function = '::make(),'; |
| 125 | + |
| 126 | + $pattern = '/->plugins\(\[([\s\S]*?)\]\);/'; |
| 127 | + $newPlugins = ''; |
| 128 | + |
| 129 | + foreach ($pluginsToAdd as $plugin) { |
| 130 | + $searchPlugin = '/' . $plugin . '/'; |
| 131 | + if (preg_match($searchPlugin, $content)) { |
| 132 | + warning($plugin . ' already registered.'); |
| 133 | + } else { |
| 134 | + $newPlugins .= $intend . $namespace . '\\' . $plugin . $function . "\n"; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if ($newPlugins !== '' && $newPlugins !== '0') { |
| 139 | + if (preg_match($pattern, $content)) { |
| 140 | + info('Plugins section found. Adding new plugins...'); |
| 141 | + |
| 142 | + $replacement = "->plugins([$1\n{$newPlugins}\n ]);"; |
| 143 | + $newContent = preg_replace($pattern, $replacement, $content); |
| 144 | + } else { |
| 145 | + info('Plugins section created. Adding new plugins...'); |
| 146 | + |
| 147 | + $pluginsSection = " ->plugins([\n{$newPlugins}\n ]);"; |
| 148 | + $placeholderPattern = '/(\->authMiddleware\(\[.*?\]\))\s*\;/s'; |
| 149 | + $replacement = "$1\n" . $pluginsSection; |
| 150 | + $newContent = preg_replace($placeholderPattern, $replacement, $content, 1); |
| 151 | + } |
| 152 | + |
| 153 | + File::put($providerPath, $newContent); |
| 154 | + } |
| 155 | + } else { |
| 156 | + alert('There are no new plugins detected.'); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + public function registerPluginInPanelProvider(): void |
| 161 | + { |
| 162 | + $providerPath = app_path('Providers/Filament'); |
| 163 | + $panelsToregister = $this->getPanelProviderPath(); |
| 164 | + if ($panelsToregister != null) { |
| 165 | + if (is_array($panelsToregister)) { |
| 166 | + foreach ($panelsToregister as $panelprovider) { |
| 167 | + $this->registerPlugins($providerPath . '/' . $panelprovider); |
| 168 | + } |
| 169 | + } else { |
| 170 | + $this->registerPlugins($panelsToregister); |
| 171 | + } |
| 172 | + } else { |
| 173 | + alert('No PanelProvider Detected please register Plugins manually.'); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + public function getPanelProviderPath(): string|array |
| 178 | + { |
| 179 | + $providerPath = app_path('Providers/Filament'); |
| 180 | + $providers = File::allFiles($providerPath); |
| 181 | + if (count($providers) > 1) { |
| 182 | + $providerNames = []; |
| 183 | + foreach ($providers as $provider) { |
| 184 | + $providerNames[] = $provider->getBasename(); |
| 185 | + } |
| 186 | + |
| 187 | + $providerPath = multiselect( |
| 188 | + label: 'Which Panel should it be registered', |
| 189 | + options: [...$providerNames], |
| 190 | + default: [$providerNames[0]], |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + if (count($providers) == 1) { |
| 195 | + $providerPath .= '/' . $providers[0]->getBasename(); |
| 196 | + } |
| 197 | + |
| 198 | + return $providerPath; |
| 199 | + } |
| 200 | + |
| 201 | + public function sayGoodbye(): void |
| 202 | + { |
| 203 | + note('Moox Media installed successfully. Enjoy!'); |
| 204 | + } |
| 205 | +} |
0 commit comments