1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Cache; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use Illuminate\Support\Facades\Process; |
8
|
|
|
|
9
|
|
|
class UpdatePerformanceHelper |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Check if a file has changed since last update |
13
|
|
|
*/ |
14
|
|
|
public static function hasFileChanged(string $filePath, string $cacheKey = null): bool |
15
|
|
|
{ |
16
|
|
|
if (!File::exists($filePath)) { |
17
|
|
|
return false; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$cacheKey = $cacheKey ?? 'file_hash_' . md5($filePath); |
21
|
|
|
$currentHash = md5_file($filePath); |
22
|
|
|
$lastHash = Cache::get($cacheKey); |
23
|
|
|
|
24
|
|
|
if ($currentHash !== $lastHash) { |
25
|
|
|
Cache::put($cacheKey, $currentHash, now()->addDays(7)); |
26
|
|
|
return true; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Execute commands in parallel where possible |
34
|
|
|
*/ |
35
|
|
|
public static function executeParallel(array $commands, int $timeout = 300): array |
36
|
|
|
{ |
37
|
|
|
$processes = []; |
38
|
|
|
$results = []; |
39
|
|
|
|
40
|
|
|
// Start all processes |
41
|
|
|
foreach ($commands as $key => $command) { |
42
|
|
|
$processes[$key] = Process::timeout($timeout)->start($command); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Wait for all processes to complete |
46
|
|
|
foreach ($processes as $key => $process) { |
47
|
|
|
$process->wait(); |
48
|
|
|
$results[$key] = [ |
49
|
|
|
'successful' => $process->successful(), |
50
|
|
|
'output' => $process->output(), |
51
|
|
|
'errorOutput' => $process->errorOutput(), |
52
|
|
|
'exitCode' => $process->exitCode() |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $results; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Clear various application caches efficiently |
61
|
|
|
*/ |
62
|
|
|
public static function clearAllCaches(): array |
63
|
|
|
{ |
64
|
|
|
$cacheOperations = [ |
65
|
|
|
'config' => fn() => \Artisan::call('config:clear'), |
66
|
|
|
'route' => fn() => \Artisan::call('route:clear'), |
67
|
|
|
'view' => fn() => \Artisan::call('view:clear'), |
68
|
|
|
'cache' => fn() => \Artisan::call('cache:clear'), |
69
|
|
|
'opcache' => fn() => function_exists('opcache_reset') ? opcache_reset() : true, |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$results = []; |
73
|
|
|
foreach ($cacheOperations as $type => $operation) { |
74
|
|
|
try { |
75
|
|
|
$operation(); |
76
|
|
|
$results[$type] = 'success'; |
77
|
|
|
} catch (\Exception $e) { |
78
|
|
|
$results[$type] = 'failed: ' . $e->getMessage(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $results; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Optimize file permissions for better performance |
87
|
|
|
*/ |
88
|
|
|
public static function optimizePermissions(): void |
89
|
|
|
{ |
90
|
|
|
$paths = [ |
91
|
|
|
base_path('bootstrap/cache'), |
92
|
|
|
base_path('storage'), |
93
|
|
|
base_path('storage/logs'), |
94
|
|
|
base_path('storage/framework/cache'), |
95
|
|
|
base_path('storage/framework/sessions'), |
96
|
|
|
base_path('storage/framework/views'), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
foreach ($paths as $path) { |
100
|
|
|
if (File::exists($path)) { |
101
|
|
|
chmod($path, 0755); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check system resources and recommend optimizations |
108
|
|
|
*/ |
109
|
|
|
public static function checkSystemResources(): array |
110
|
|
|
{ |
111
|
|
|
$recommendations = []; |
112
|
|
|
|
113
|
|
|
// Check memory usage |
114
|
|
|
$memoryUsage = memory_get_usage(true); |
115
|
|
|
$memoryLimit = ini_get('memory_limit'); |
116
|
|
|
|
117
|
|
|
if ($memoryUsage > (int)$memoryLimit * 0.8) { |
118
|
|
|
$recommendations[] = 'Consider increasing PHP memory_limit'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Check disk space |
122
|
|
|
$freeSpace = disk_free_space(base_path()); |
123
|
|
|
$totalSpace = disk_total_space(base_path()); |
124
|
|
|
|
125
|
|
|
if ($freeSpace < ($totalSpace * 0.1)) { |
126
|
|
|
$recommendations[] = 'Low disk space detected'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Check PHP extensions |
130
|
|
|
$requiredExtensions = ['pdo', 'mbstring', 'openssl', 'json', 'curl']; |
131
|
|
|
foreach ($requiredExtensions as $ext) { |
132
|
|
|
if (!extension_loaded($ext)) { |
133
|
|
|
$recommendations[] = "Missing PHP extension: $ext"; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $recommendations; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Create a system performance snapshot |
142
|
|
|
*/ |
143
|
|
|
public static function createPerformanceSnapshot(): array |
144
|
|
|
{ |
145
|
|
|
return [ |
146
|
|
|
'timestamp' => now()->toISOString(), |
147
|
|
|
'memory_usage' => memory_get_usage(true), |
148
|
|
|
'memory_peak' => memory_get_peak_usage(true), |
149
|
|
|
'execution_time' => microtime(true) - LARAVEL_START, |
150
|
|
|
'php_version' => PHP_VERSION, |
151
|
|
|
'laravel_version' => app()->version(), |
|
|
|
|
152
|
|
|
'disk_free' => disk_free_space(base_path()), |
153
|
|
|
'load_average' => function_exists('sys_getloadavg') ? sys_getloadavg() : null, |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|