@@ -7,58 +7,59 @@ class RotativeProcessor extends AbstractProcessor
77 private $ maxFiles = 366 ;
88
99 /**
10- * Max num file rotate
10+ * Log files are rotated count times before being removed
1111 *
12- * @param int $maxFiles
12+ * @param int $count
1313 * @return self
1414 */
15- public function setMaxFiles (int $ maxFiles ): self
15+ public function files (int $ count ): self
1616 {
17- $ this ->maxFiles = $ maxFiles ;
17+ $ this ->maxFiles = $ count ;
1818
1919 return $ this ;
2020 }
2121
2222 public function handler ($ file ): ?string
2323 {
24- $ fileInfo = pathinfo ( $ file ) ;
24+ $ nextFile = "{ $ this -> fileOriginal } .1 " ;
2525
26- $ extension_in = $ fileInfo [ ' extension ' ] ?? '' ;
26+ $ this -> rotate () ;
2727
28- $ fileInfo = pathinfo ( $ this -> fileOriginal );
28+ rename ( $ file , $ nextFile );
2929
30- $ extension_original = $ fileInfo ['extension ' ] ?? '' ;
30+ return $ this ->processed ($ nextFile );
31+ }
3132
32- $ glob = $ fileInfo ['dirname ' ].DIRECTORY_SEPARATOR .$ fileInfo ['filename ' ];
33+ private function rotate (int $ number = 1 ): string
34+ {
35+ $ file = "{$ this ->fileOriginal }. {$ number }{$ this ->suffix }" ;
3336
34- if (! empty ( $ fileInfo [ ' extension ' ] )) {
35- $ glob .= ' . ' . $ fileInfo [ ' extension ' ] ;
37+ if (!file_exists ( $ file )) {
38+ return "{ $ this -> fileOriginal } . { $ number }{ $ this -> suffix }" ;
3639 }
3740
38- if ($ extension_in != '' && $ extension_in != $ extension_original ) {
39- $ glob .= '. ' .$ extension_in ;
40- }
41+ if ($ this ->maxFiles > 0 && $ number >= $ this ->maxFiles ) {
42+ if (file_exists ($ file )) {
43+ unlink ($ file );
44+ }
4145
42- $ glob .= '.* ' ;
46+ return "{$ this ->fileOriginal }. {$ number }{$ this ->suffix }" ;
47+ }
4348
44- $ curFiles = glob ( $ glob );
49+ $ nextFile = $ this -> rotate ( $ number + 1 );
4550
46- for ($ n = count ($ curFiles ); $ n > 0 ; $ n --) {
47- $ file_to_move = str_replace ('* ' , $ n , $ glob );
51+ rename ($ file , $ nextFile );
4852
49- if (file_exists ($ file_to_move )) {
50- if ($ this ->maxFiles > 0 && $ n >= $ this ->maxFiles ) {
51- unlink ($ file_to_move );
52- } else {
53- rename ($ file_to_move , str_replace ('* ' , $ n + 1 , $ glob ));
54- }
55- }
56- }
53+ return "{$ this ->fileOriginal }. {$ number }{$ this ->suffix }" ;
54+ }
5755
58- $ nextFile = str_replace ('* ' , '1 ' , $ glob );
56+ private function getnumber (string $ file ): ?int
57+ {
58+ $ fileName = basename ($ file );
59+ $ fileOriginaleName = basename ($ this ->fileOriginal );
5960
60- rename ( $ file , $ nextFile );
61+ preg_match ( " / { $ fileOriginaleName } .([0-9]+) { $ this -> suffix } / " , $ fileName , $ output );
6162
62- return $ this -> processed ( $ nextFile ) ;
63+ return $ output [ 1 ] ?? null ;
6364 }
6465}
0 commit comments