|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Moox\Scopes\Installers; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\Artisan; |
| 8 | +use Illuminate\Support\Facades\DB; |
| 9 | +use Illuminate\Support\Facades\Schema; |
| 10 | +use Moox\Core\Installer\AbstractAssetInstaller; |
| 11 | + |
| 12 | +use function Moox\Prompts\error; |
| 13 | +use function Moox\Prompts\note; |
| 14 | + |
| 15 | +/** |
| 16 | + * Installer für das Scopes-Package. |
| 17 | + * |
| 18 | + * Synchronisiert Scopes aus den Package-Configs (resources.*.scopes.allowed) |
| 19 | + * in die scopes-Tabelle — gleiche Logik wie `php artisan moox:scope`. |
| 20 | + * Voraussetzung: Migrationen (moox/core, scopes-Tabelle). |
| 21 | + */ |
| 22 | +class ScopesInstaller extends AbstractAssetInstaller |
| 23 | +{ |
| 24 | + public function getType(): string |
| 25 | + { |
| 26 | + return 'scopes'; |
| 27 | + } |
| 28 | + |
| 29 | + public function getLabel(): string |
| 30 | + { |
| 31 | + return 'Scopes (sync from config)'; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Nach Migrationen und Config-Publish, vor Plugins. |
| 36 | + */ |
| 37 | + protected function getDefaultConfig(): array |
| 38 | + { |
| 39 | + $config = parent::getDefaultConfig(); |
| 40 | + $config['priority'] = 25; |
| 41 | + |
| 42 | + return $config; |
| 43 | + } |
| 44 | + |
| 45 | + public function hasItemSelection(): bool |
| 46 | + { |
| 47 | + // Keine Item-Auswahl, Sync läuft als Ganzes. |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + public function checkExists(string $packageName, array $items): bool |
| 52 | + { |
| 53 | + // Wenn es bereits Scopes gibt, war der Sync schon einmal gelaufen. |
| 54 | + if (! Schema::hasTable('scopes')) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + return DB::table('scopes')->exists(); |
| 59 | + } |
| 60 | + |
| 61 | + public function install(array $assets): bool |
| 62 | + { |
| 63 | + if (! Schema::hasTable('scopes')) { |
| 64 | + note('ℹ️ Table scopes not found. Run migrations first (moox core).'); |
| 65 | + |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + note('🔗 Syncing scopes from package configs (moox:scope) …'); |
| 71 | + |
| 72 | + if ($this->command) { |
| 73 | + $exitCode = $this->command->call('moox:scope'); |
| 74 | + } else { |
| 75 | + $exitCode = Artisan::call('moox:scope'); |
| 76 | + } |
| 77 | + |
| 78 | + if ($exitCode !== 0) { |
| 79 | + error('⚠️ Scope sync failed (moox:scope exit code '.$exitCode.').'); |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + note('✅ Scopes synced from config.'); |
| 85 | + |
| 86 | + return true; |
| 87 | + } catch (\Throwable $e) { |
| 88 | + error('⚠️ Scope sync failed: '.$e->getMessage()); |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments