Skip to content

Commit 7660c1f

Browse files
committed
add fix
1 parent 6b9889c commit 7660c1f

File tree

3 files changed

+147
-38
lines changed

3 files changed

+147
-38
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
os: [ubuntu-latest]
2424
php: [8.3]
2525
laravel: [12.*]
26-
stability: [prefer-lowest, prefer-stable]
26+
stability: [prefer-stable]
2727
include:
2828
- laravel: 12.*
2929
testbench: 10.*

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"minimum-stability": "stable",
1818
"require": {
1919
"php": "^8.3",
20-
"illuminate/contracts": "^12.32.5",
20+
"laravel/framework": "^12.32.5",
2121
"laravel/prompts": "^0.3.7"
2222
},
2323
"require-dev": {

src/Commands/CacheUiLaravelCommand.php

Lines changed: 145 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,19 @@ public function handle(): int
6262
}
6363

6464
// Get the key value to show information
65-
$value = $this->store->get($selectedKey);
66-
67-
// If value is null, try different approaches based on driver
68-
if ($value === null) {
69-
if ($this->driver === 'redis') {
70-
// For Redis, try with prefix
71-
$prefix = config('database.redis.options.prefix', '');
72-
if ($prefix) {
73-
$value = $this->store->get($prefix.$selectedKey);
74-
}
75-
} elseif ($this->driver === 'file') {
76-
// For file driver, the selectedKey might be a filename hash
77-
// We need to find the actual key by checking all files
78-
$value = $this->getFileKeyValue($selectedKey);
79-
}
65+
// For Redis, we need to add the prefix back since we removed it when listing keys
66+
if ($this->driver === 'redis') {
67+
$prefix = config('database.redis.options.prefix', '');
68+
$fullKey = $prefix ? $prefix.$selectedKey : $selectedKey;
69+
$value = $this->store->get($fullKey);
70+
} else {
71+
$value = $this->store->get($selectedKey);
72+
}
73+
74+
// If value is still null, try different approaches based on driver
75+
if ($value === null && $this->driver === 'file') {
76+
// For file driver, we need to find the file that contains this key
77+
$value = $this->getFileKeyValueByKey($selectedKey);
8078
}
8179

8280
$valuePreview = $this->getValuePreview($value);
@@ -98,20 +96,19 @@ public function handle(): int
9896
}
9997

10098
// Try to delete the key
101-
$deleted = $this->store->forget($selectedKey);
99+
// For Redis, we need to add the prefix back since we removed it when listing keys
100+
if ($this->driver === 'redis') {
101+
$prefix = config('database.redis.options.prefix', '');
102+
$fullKey = $prefix ? $prefix.$selectedKey : $selectedKey;
103+
$deleted = $this->store->forget($fullKey);
104+
} else {
105+
$deleted = $this->store->forget($selectedKey);
106+
}
102107

103108
// If not deleted, try different approaches based on driver
104-
if (! $deleted) {
105-
if ($this->driver === 'redis') {
106-
// For Redis, try with prefix
107-
$prefix = config('database.redis.options.prefix', '');
108-
if ($prefix) {
109-
$deleted = $this->store->forget($prefix.$selectedKey);
110-
}
111-
} elseif ($this->driver === 'file') {
112-
// For file driver, try to delete using the actual key
113-
$deleted = $this->deleteFileKey($selectedKey);
114-
}
109+
if (! $deleted && $this->driver === 'file') {
110+
// For file driver, try to delete using the actual key
111+
$deleted = $this->deleteFileKeyByKey($selectedKey);
115112
}
116113

117114
if ($deleted) {
@@ -152,7 +149,7 @@ private function getRedisKeys(): array
152149
return $key;
153150
}, $keys);
154151
} catch (Exception $e) {
155-
error('Error al obtener claves de Redis: '.$e->getMessage());
152+
error('Error getting Redis keys: '.$e->getMessage());
156153

157154
return [];
158155
}
@@ -171,17 +168,39 @@ private function getFileKeys(): array
171168
$keys = [];
172169

173170
foreach ($files as $file) {
174-
// El nombre del archivo en Laravel es un hash, pero podemos leer el contenido
175-
$content = File::get($file->getPathname());
176-
177-
// Formato del archivo de caché de Laravel: expiration_time + serialized_value
178-
// Intentar extraer el nombre de la clave del contenido serializado
179-
$keys[] = $file->getFilename();
171+
try {
172+
$content = File::get($file->getPathname());
173+
174+
// Laravel file cache format: expiration_time + serialized_value
175+
if (mb_strlen($content) < 10) {
176+
continue;
177+
}
178+
179+
$expiration = mb_substr($content, 0, 10);
180+
$serialized = mb_substr($content, 10);
181+
182+
// Check if expired
183+
if (time() > $expiration) {
184+
continue;
185+
}
186+
187+
// Try to unserialize to get the actual key
188+
$data = unserialize($serialized);
189+
if (is_array($data) && isset($data['key'])) {
190+
$keys[] = $data['key'];
191+
} else {
192+
// Fallback to filename if we can't extract the key
193+
$keys[] = $file->getFilename();
194+
}
195+
} catch (Exception) {
196+
// If we can't read this file, skip it
197+
continue;
198+
}
180199
}
181200

182201
return $keys;
183202
} catch (Exception $e) {
184-
error('Error al obtener claves del sistema de archivos: '.$e->getMessage());
203+
error('Error getting file system keys: '.$e->getMessage());
185204

186205
return [];
187206
}
@@ -194,7 +213,7 @@ private function getDatabaseKeys(): array
194213

195214
return DB::table($table)->pluck('key')->toArray();
196215
} catch (Exception $e) {
197-
error('Error al obtener claves de la base de datos: '.$e->getMessage());
216+
error('Error getting database keys: '.$e->getMessage());
198217

199218
return [];
200219
}
@@ -246,6 +265,51 @@ private function getValuePreview(mixed $value): string
246265
return $stringValue;
247266
}
248267

268+
private function getFileKeyValueByKey(string $key): mixed
269+
{
270+
try {
271+
$cachePath = config('cache.stores.file.path', storage_path('framework/cache/data'));
272+
273+
if (! File::exists($cachePath)) {
274+
return null;
275+
}
276+
277+
$files = File::allFiles($cachePath);
278+
279+
foreach ($files as $file) {
280+
try {
281+
$content = File::get($file->getPathname());
282+
283+
// Laravel file cache format: expiration_time + serialized_value
284+
if (mb_strlen($content) < 10) {
285+
continue;
286+
}
287+
288+
$expiration = mb_substr($content, 0, 10);
289+
$serialized = mb_substr($content, 10);
290+
291+
// Check if expired
292+
if (time() > $expiration) {
293+
continue;
294+
}
295+
296+
// Try to unserialize to get the data
297+
$data = unserialize($serialized);
298+
if (is_array($data) && isset($data['key']) && $data['key'] === $key) {
299+
return $data['value'] ?? null;
300+
}
301+
} catch (Exception) {
302+
// If we can't read this file, skip it
303+
continue;
304+
}
305+
}
306+
307+
return null;
308+
} catch (Exception) {
309+
return null;
310+
}
311+
}
312+
249313
private function getFileKeyValue(string $filename): mixed
250314
{
251315
try {
@@ -277,6 +341,51 @@ private function getFileKeyValue(string $filename): mixed
277341
}
278342
}
279343

344+
private function deleteFileKeyByKey(string $key): bool
345+
{
346+
try {
347+
$cachePath = config('cache.stores.file.path', storage_path('framework/cache/data'));
348+
349+
if (! File::exists($cachePath)) {
350+
return false;
351+
}
352+
353+
$files = File::allFiles($cachePath);
354+
355+
foreach ($files as $file) {
356+
try {
357+
$content = File::get($file->getPathname());
358+
359+
// Laravel file cache format: expiration_time + serialized_value
360+
if (mb_strlen($content) < 10) {
361+
continue;
362+
}
363+
364+
$expiration = mb_substr($content, 0, 10);
365+
$serialized = mb_substr($content, 10);
366+
367+
// Check if expired
368+
if (time() > $expiration) {
369+
continue;
370+
}
371+
372+
// Try to unserialize to get the data
373+
$data = unserialize($serialized);
374+
if (is_array($data) && isset($data['key']) && $data['key'] === $key) {
375+
return File::delete($file->getPathname());
376+
}
377+
} catch (Exception) {
378+
// If we can't read this file, skip it
379+
continue;
380+
}
381+
}
382+
383+
return false;
384+
} catch (Exception) {
385+
return false;
386+
}
387+
}
388+
280389
private function deleteFileKey(string $filename): bool
281390
{
282391
try {

0 commit comments

Comments
 (0)