1 | <?php |
||
2 | |||
3 | namespace App\Console\Commands; |
||
4 | |||
5 | use Illuminate\Console\Command; |
||
6 | use Illuminate\Support\Facades\File; |
||
7 | use Illuminate\Support\Facades\Process; |
||
8 | |||
9 | class UpdateNNTmuxComposer extends Command |
||
10 | { |
||
11 | /** |
||
12 | * The name and signature of the console command. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $signature = 'nntmux:composer |
||
17 | {--no-dev : Skip development dependencies} |
||
18 | {--optimize : Optimize autoloader} |
||
19 | {--prefer-dist : Prefer distribution packages}'; |
||
20 | |||
21 | /** |
||
22 | * The console command description. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $description = 'Update composer dependencies with optimizations'; |
||
27 | |||
28 | /** |
||
29 | * Execute the console command. |
||
30 | */ |
||
31 | public function handle(): int |
||
32 | { |
||
33 | try { |
||
34 | $this->info('📦 Starting composer update process...'); |
||
35 | |||
36 | // Check if composer.json exists |
||
37 | if (! File::exists(base_path('composer.json'))) { |
||
38 | $this->error('composer.json not found'); |
||
39 | |||
40 | return Command::FAILURE; |
||
41 | } |
||
42 | |||
43 | // Check if composer.lock exists to determine install vs update |
||
44 | $hasLockFile = File::exists(base_path('composer.lock')); |
||
45 | |||
46 | if ($hasLockFile) { |
||
47 | $this->info('🔄 Installing dependencies from lock file...'); |
||
48 | $this->composerInstall(); |
||
49 | } else { |
||
50 | $this->info('🆕 Creating new lock file and installing dependencies...'); |
||
51 | $this->composerUpdate(); |
||
52 | } |
||
53 | |||
54 | // Clear autoloader cache |
||
55 | $this->info('🧹 Clearing autoloader cache...'); |
||
56 | $this->clearAutoloaderCache(); |
||
57 | |||
58 | $this->info('✅ Composer update completed successfully'); |
||
59 | |||
60 | return Command::SUCCESS; |
||
61 | |||
62 | } catch (\Exception $e) { |
||
63 | $this->error('❌ Composer update failed: '.$e->getMessage()); |
||
64 | |||
65 | return Command::FAILURE; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Run composer install |
||
71 | */ |
||
72 | private function composerInstall(): void |
||
73 | { |
||
74 | $command = $this->buildComposerCommand('install'); |
||
75 | |||
76 | $process = Process::timeout(600) |
||
77 | ->path(base_path()) |
||
78 | ->run($command); |
||
79 | |||
80 | if (! $process->successful()) { |
||
81 | throw new \Exception('Composer install failed: '.$process->errorOutput()); |
||
82 | } |
||
83 | |||
84 | $this->line(' ✓ Dependencies installed successfully'); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Run composer update |
||
89 | */ |
||
90 | private function composerUpdate(): void |
||
91 | { |
||
92 | $command = $this->buildComposerCommand('update'); |
||
93 | |||
94 | $process = Process::timeout(600) |
||
95 | ->path(base_path()) |
||
96 | ->run($command); |
||
97 | |||
98 | if (! $process->successful()) { |
||
99 | throw new \Exception('Composer update failed: '.$process->errorOutput()); |
||
100 | } |
||
101 | |||
102 | $this->line(' ✓ Dependencies updated successfully'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Build composer command with options |
||
107 | */ |
||
108 | private function buildComposerCommand(string $action): string |
||
109 | { |
||
110 | $command = "composer $action"; |
||
111 | |||
112 | // Add common flags for performance |
||
113 | $command .= ' --no-interaction --no-progress'; |
||
114 | |||
115 | if ($this->option('no-dev')) { |
||
116 | $command .= ' --no-dev'; |
||
117 | } |
||
118 | |||
119 | if ($this->option('prefer-dist')) { |
||
120 | $command .= ' --prefer-dist'; |
||
121 | } |
||
122 | |||
123 | if ($this->option('optimize') || app()->environment('production')) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
124 | $command .= ' --optimize-autoloader --classmap-authoritative'; |
||
125 | } |
||
126 | |||
127 | return $command; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Clear autoloader cache |
||
132 | */ |
||
133 | private function clearAutoloaderCache(): void |
||
134 | { |
||
135 | $process = Process::timeout(30) |
||
136 | ->path(base_path()) |
||
137 | ->run('composer dump-autoload --optimize'); |
||
138 | |||
139 | if (! $process->successful()) { |
||
140 | $this->warn(' ⚠ Failed to optimize autoloader'); |
||
141 | } else { |
||
142 | $this->line(' ✓ Autoloader optimized'); |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 |