Skip to content

Commit 042cadd

Browse files
committed
Make ext-intl optional; (re)introduce fallback
Hard-requiring extensions for non-core feature is overkill
1 parent 2dcf321 commit 042cadd

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Note: you may refer to `README.md` for description of features.
33

44
## Dev (WIP)
55

6+
## 2.0.1 (2025-01-13)
7+
- Added fallback of Laravel's `Number::fileSize()` if `ext-intl` is not available
8+
- `ext-intl` is now suggested instead of required
9+
610
## 2.0.0 (2025-01-12)
711
- Adopted Laravel's `Number::fileSize()` to show the estimated evicted storage size stats
812
- Therefore, further requires `ext-intl`

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ via Composer:
3030
composer require vectorial1024/laravel-cache-evict
3131
```
3232

33+
Suggestions:
34+
- (Optional) `ext-intl` allows using Laravel's own `Number::fileSize()` for size reporting
35+
3336
### Supported cache types
3437
The following cache drivers from `cache.php` are currently supported:
3538
- `database`

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
],
3434
"require": {
3535
"php": "^8.1",
36-
"ext-intl": "*",
3736
"illuminate/support": "^10.0|^11.0",
3837
"wilderborn/partyline": "^1.0"
3938
},
@@ -42,6 +41,9 @@
4241
"phpunit/phpunit": "^10.4",
4342
"orchestra/testbench": "^8.0|^9.0"
4443
},
44+
"suggest": {
45+
"ext-intl": "Enables using Laravel's `Number::fileSize()` for size reporting"
46+
},
4547
"scripts": {
4648
"test": "vendor/bin/phpunit test"
4749
},

src/AbstractEvictStrategy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ abstract public function execute();
4343
protected function bytesToHuman(int $bytes): string
4444
{
4545
// it turns out Laravel already has a helper for this
46-
return Number::fileSize($bytes, 2, 2);
46+
// but it requires the intl extension
47+
// prefer the Laravel solution; if they don't have it, then we can still use the fallback hand-crafted solution
48+
if (extension_loaded('intl')) {
49+
return Number::fileSize($bytes, 2, 2);
50+
}
51+
// see https://stackoverflow.com/questions/15188033/human-readable-file-size
52+
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
53+
for ($i = 0; $bytes > 1024; $i++) $bytes /= 1024;
54+
return round($bytes, 2) . ' ' . $units[$i];
4755
}
4856
}

0 commit comments

Comments
 (0)