44
55use Illuminate \Console \Command ;
66use Illuminate \Console \Scheduling \Schedule ;
7+ use Illuminate \Support \Collection ;
78use Lorisleiva \CronTranslator \CronTranslator ;
89use Spatie \Emoji \Emoji ;
910use Symfony \Component \Console \Helper \TableSeparator ;
11+ use Vormkracht10 \PermanentCache \CachesValue ;
1012use Vormkracht10 \PermanentCache \Facades \PermanentCache ;
13+ use Vormkracht10 \PermanentCache \Scheduled ;
1114
1215class PermanentCachesStatusCommand extends Command
1316{
@@ -16,7 +19,11 @@ class PermanentCachesStatusCommand extends Command
1619 *
1720 * @var string
1821 */
19- protected $ signature = 'permanent-cache:status {--P|parameters} {--F|filter=} ' ;
22+ protected $ signature = 'permanent-cache:status
23+ {--P|parameters}
24+ {--F|filter=}
25+ {--S|sort= : The statistic to sort on, this can be one of ["size", "updated", "frequency"]}
26+ {--A|ascending : Whether the sorting should be done ascending instead of descending} ' ;
2027
2128 /**
2229 * The console command description.
@@ -25,12 +32,65 @@ class PermanentCachesStatusCommand extends Command
2532 */
2633 protected $ description = 'Show status for all registered Permanent Caches ' ;
2734
35+ protected function getSize ($ cache ): int
36+ {
37+ return strlen (serialize ($ cache ));
38+ }
39+
40+ protected function sortOnSize ($ a , $ b ): int
41+ {
42+ $ sizeA = $ this ->getSize ($ a [0 ]);
43+ $ sizeB = $ this ->getSize ($ b [0 ]);
44+
45+ return match (true ) {
46+ $ sizeA == $ sizeB => 0 ,
47+ default => $ sizeA > $ sizeB ? 1 : -1 ,
48+ };
49+ }
50+
51+ protected function sortOnUpdated ($ a , $ b ): int
52+ {
53+ return $ a [2 ]->updated_at > $ b [2 ]->updated_at ? 1 : -1 ;
54+ }
55+
56+ protected function sortOnFrequency ($ a , $ b ): int
57+ {
58+ $ a = $ a [0 ]->expression ()?->getNextRunDate();
59+ $ b = $ b [0 ]->expression ()?->getNextRunDate();
60+
61+ if (is_null ($ a )) return is_null ($ b ) ? 0 : -1 ;
62+ if (is_null ($ b )) return 1 ;
63+
64+ return $ a > $ b ? 1 : -1 ;
65+ }
66+
2867 /**
2968 * Execute the console command.
3069 */
3170 public function handle ()
3271 {
33- $ caches = PermanentCache::configuredCaches ();
72+ $ sortOn = $ this ->option ('sort ' );
73+
74+ $ items = new Collection ;
75+
76+ foreach (($ caches = PermanentCache::configuredCaches ()) as $ cache ) {
77+ $ items ->push ([
78+ $ cache ,
79+ $ parameters = $ caches ->getInfo (),
80+ $ cache ->getMeta ($ parameters ),
81+ ]);
82+ }
83+
84+ $ caches = $ items ->when ($ sortOn , fn ($ collection ) => $ collection ->sort (function ($ a , $ b ) use ($ sortOn ) {
85+ $ result = match ($ sortOn ) {
86+ 'size ' => $ this ->sortOnSize ($ a , $ b ),
87+ 'updated ' => $ this ->sortOnUpdated ($ a , $ b ),
88+ 'frequency ' => $ this ->sortOnFrequency ($ a , $ b ),
89+ default => throw new \Exception ("Invalid sorting method: \"{$ sortOn }\"" )
90+ };
91+
92+ return $ this ->option ('ascending ' ) ? $ result : -$ result ;
93+ }));
3494
3595 $ frequencies = collect (app (Schedule::class)->events ())
3696 ->mapWithKeys (function ($ schedule ) {
@@ -39,24 +99,19 @@ public function handle()
3999
40100 $ tableRows = [];
41101
42- foreach ($ caches as $ c ) {
43- $ cache = $ caches ->current ();
44- $ parameters = $ caches ->getInfo ();
45-
102+ foreach ($ caches as [$ cache , $ parameters , $ meta ]) {
46103 if (
47104 $ this ->option ('filter ' ) &&
48105 ! str_contains (strtolower ($ cache ->getName ()), strtolower ($ this ->option ('filter ' )))
49106 ) {
50107 continue ;
51108 }
52109
53- $ cached = $ cache ->getMeta ($ parameters );
54-
55110 $ row = [
56111 $ cache ->isCached ($ parameters ) ? Emoji::checkMarkButton () : Emoji::crossMark (),
57112 $ cache ->getName (),
58- $ cached ? readable_size (strlen ( serialize ( $ cached ) )) : 'N/A ' ,
59- $ cached ?->updated_at?->diffForHumans() ?: 'N/A ' ,
113+ $ meta ? readable_size ($ this -> getSize ( $ meta )) : 'N/A ' ,
114+ $ meta ?->updated_at?->diffForHumans() ?: 'N/A ' ,
60115 $ frequencies [$ cache ->getName ()] ?? 'N/A ' ,
61116 ];
62117
0 commit comments