-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModuliteOptimizeCommand.php
More file actions
209 lines (174 loc) · 6.92 KB
/
ModuliteOptimizeCommand.php
File metadata and controls
209 lines (174 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
namespace PanicDevs\Modulite\Console\Commands;
use Illuminate\Console\Command;
use PanicDevs\Modulite\Contracts\CacheManagerInterface;
use PanicDevs\Modulite\Contracts\PanelScannerInterface;
use PanicDevs\Modulite\Contracts\ComponentScannerInterface;
use PanicDevs\Modulite\Contracts\ModuleResolverInterface;
use Throwable;
/**
* ModuliteOptimizeCommand
*
* Optimizes Modulite by warming all caches (panels and components).
* This command is automatically called by Laravel's `optimize` command.
*
* @package PanicDevs\Modulite\Console\Commands
* @author PanicDevs
* @since 1.0.0
*/
class ModuliteOptimizeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'modulite:cache
{--force : Force cache regeneration even if cache exists}
{--enable-cache : Temporarily enable caching even in debug mode}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Cache Filament panels and components for better performance';
/**
* Execute the console command.
*/
public function handle(
CacheManagerInterface $cacheManager,
PanelScannerInterface $panelScanner,
ComponentScannerInterface $componentScanner,
ModuleResolverInterface $moduleResolver
): int {
$this->info('Optimizing Modulite caches...');
try
{
// Temporarily enable cache if requested
if ($this->option('enable-cache'))
{
$this->info('Temporarily enabling cache for this operation...');
// Enable cache in the manager for this command only
$this->enableCacheForCommand($cacheManager);
}
// Clear existing caches if force flag is used
if ($this->option('force'))
{
$this->info('Clearing existing caches...');
$cacheManager->flush();
}
// Warm panel discovery cache
$this->line('• Warming panel discovery cache...');
$panels = $panelScanner->discoverPanels();
$panelCount = count($panels);
// Store panels in cache using the same key as the service provider
$panelCacheKey = $this->generatePanelCacheKey($moduleResolver);
$cacheManager->put($panelCacheKey, $panels);
$this->line(" <fg=green>✓</> Found {$panelCount} panel providers");
// Warm component discovery cache for all discovered panels
$this->line('• Warming component discovery cache...');
$totalComponents = 0;
foreach ($panels as $panelClass)
{
// Extract panel ID from class name (e.g., UserPanelProvider -> user)
$panelId = $this->extractPanelIdFromClass($panelClass);
// Force cache warming by calling discoverComponents which stores in cache
$components = $componentScanner->discoverComponents($panelId);
$componentCount = array_sum(array_map('count', $components));
$totalComponents += $componentCount;
$this->line(" - {$panelId}: {$componentCount} components");
}
$this->line(" <fg=green>✓</> Found {$totalComponents} components");
// Display cache file locations
$this->displayCacheInfo();
$this->info('Modulite caches optimized successfully!');
$this->line("- <fg=green>{$panelCount}</> panel providers cached");
$this->line("- <fg=green>{$totalComponents}</> components cached");
return self::SUCCESS;
} catch (Throwable $e)
{
$this->error("Failed to optimize Modulite caches: {$e->getMessage()}");
if ($this->getOutput()->isVerbose())
{
$this->error($e->getTraceAsString());
}
return self::FAILURE;
}
}
/**
* Extract panel ID from panel provider class name.
*
* Examples:
* - AdminPanelProvider -> admin
* - UserPanelProvider -> user
* - ManagerPanelProvider -> manager
*/
protected function extractPanelIdFromClass(string $className): string
{
// Get just the class name without namespace
$parts = explode('\\', $className);
$shortName = end($parts);
// Remove 'Provider' suffix and 'Panel'
$panelId = str_replace(['PanelProvider', 'Panel', 'Provider'], '', $shortName);
// Convert to lowercase
return mb_strtolower($panelId);
}
/**
* Temporarily enable cache for this command operation.
*/
protected function enableCacheForCommand(CacheManagerInterface $cacheManager): void
{
// Check if cache manager supports runtime enable (UnifiedCacheManager does)
if (method_exists($cacheManager, 'enableTemporarily'))
{
$cacheManager->enableTemporarily();
} else
{
$this->warn('Cache manager does not support temporary enabling. Use MODULITE_CACHE_ENABLED=true instead.');
}
}
/**
* Generate cache key for panels (same logic as ModuliteServiceProvider).
*/
protected function generatePanelCacheKey(ModuleResolverInterface $moduleResolver): string
{
// Use the module resolver to get enabled modules
$enabledModules = $moduleResolver->getEnabledModules();
// Include module names - use simple array for consistent hashing
$moduleData = $enabledModules->sort()->values()->toArray();
// Include the module resolver approach in cache key
$approach = config('modulite.modules.approach');
// Include configuration in cache key
$configHash = md5(serialize([
'panels' => config('modulite.panels', []),
'components' => config('modulite.components', []),
'modules' => config('modulite.modules', [])
]));
// Include environment
$environment = config('app.env', 'production');
$keyData = [
'modules' => $moduleData,
'approach' => $approach,
'config' => $configHash,
'environment' => $environment,
];
return 'panels:'.md5(serialize($keyData));
}
/**
* Display cache file information.
*/
protected function displayCacheInfo(): void
{
$cacheFile = config('modulite.cache.file', base_path('bootstrap/cache/modulite.php'));
if (file_exists($cacheFile))
{
$this->line("- Cache file: <fg=blue>{$cacheFile}</>");
$size = filesize($cacheFile);
$this->line("- Cache size: <fg=blue>".number_format($size)." bytes</>");
} else
{
$this->line("- Cache file: <fg=yellow>Not created yet</>");
}
}
}