@@ -22,21 +22,13 @@ protected static function check(CLI $cli, ModelWithContent $model, array &$uuids
2222
2323 // the UUID already exists for another model
2424 if (isset ($ uuids [$ uuid ])) {
25- $ duplicates [] = $ uuid ;
26-
27- if ($ cli ->arg ('fix ' ) === true ) {
28- $ model ->uuid ()->clear ();
29- $ model ->update ([
30- 'uuid ' => Uuid::generate ()
31- ]);
32- $ model ->uuid ()->populate (true );
33- $ cli ->out ('✅ The duplicate UUID ' . $ uuid . ' for ' . $ model ->id () . ' has been regenerated ' );
34- } else {
35- $ cli ->error ('The UUID ' . $ uuid . ' for ' . $ model ->id () . ' exists ( ' . $ uuids [$ uuid ] . ') ' );
36- }
25+ // add this model and the one with the duplicate uuid
26+ // to the duplicates array to clean them up later
27+ $ duplicates [] = $ model ;
28+ $ duplicates [] = $ uuids [$ uuid ];
3729 }
3830
39- $ uuids [$ uuid ] = $ model-> id () ;
31+ $ uuids [$ uuid ] = $ model ;
4032 }
4133
4234 public static function command (CLI $ cli ): void
@@ -79,13 +71,42 @@ public static function command(CLI $cli): void
7971 }
8072 }
8173
82- if (count ($ uuids ) === 0 ) {
74+ // remove duplicates from duplicates array
75+ $ duplicates = array_unique ($ duplicates );
76+
77+ if (count ($ duplicates ) === 0 ) {
8378 $ cli ->success ('There are no UUID duplicates ' );
84- } elseif ($ fix === true ) {
79+ return ;
80+ }
81+
82+ // go through all collected models with duplicate UUIDs
83+ // and print info or fix them.
84+ foreach ($ duplicates as $ model ) {
85+ $ uuid = $ model ->uuid ()->toString ();
86+
87+ if ($ fix === true ) {
88+ static ::regenerate ($ model );
89+ $ cli ->out ('✅ The duplicate UUID ' . $ uuid . ' for ' . $ model ->id () . ' has been regenerated ' );
90+ } else {
91+ $ cli ->error ('The UUID ' . $ uuid . ' for ' . $ model ->id () . ' exists ' );
92+ }
93+ }
94+
95+ if ($ fix === true ) {
8596 $ cli ->success (count ($ duplicates ) . ' duplicates have been fixed ' );
86- } else {
87- $ cli ->out (count ($ duplicates ) . ' duplicates! You can fix them with kirby uuid:duplicates --fix ' );
88- exit (1 );
97+ return ;
8998 }
99+
100+ $ cli ->out (count ($ duplicates ) . ' duplicates! You can fix them with kirby uuid:duplicates --fix ' );
101+ exit (1 );
102+ }
103+
104+ protected static function regenerate (ModelWithContent $ model ): void
105+ {
106+ $ model ->uuid ()->clear ();
107+ $ model ->update ([
108+ 'uuid ' => Uuid::generate ()
109+ ]);
110+ $ model ->uuid ()->populate (true );
90111 }
91112}
0 commit comments