@@ -19,6 +19,9 @@ class Rotation
19
19
20
20
private bool $ _truncate = false ;
21
21
22
+ /**
23
+ * @param mixed[] $options
24
+ */
22
25
public function __construct (array $ options = [])
23
26
{
24
27
$ this ->processor = new RotativeProcessor ();
@@ -199,25 +202,15 @@ private function copyAndTruncate(string $filename): ?string
199
202
{
200
203
clearstatcache ();
201
204
202
- $ filenameTarget = tempnam (dirname ($ filename ), 'LOG ' );
203
-
204
- $ fd = fopen ($ filename , 'r+ ' );
205
-
206
- if ($ fd === false ) {
207
- $ this ->exception (
208
- new Exception (sprintf ('the file %s not can open. ' , $ filename ), 20 )
209
- );
205
+ $ filenameTarget = $ this ->getTempFilename (dirname ($ filename ));
210
206
207
+ if (!$ filenameTarget ) {
211
208
return null ;
212
209
}
213
210
214
- if (!flock ($ fd , LOCK_EX )) {
215
- fclose ($ fd );
216
-
217
- $ this ->exception (
218
- new Exception (sprintf ('the file %s not can lock. ' , $ filename ), 21 )
219
- );
211
+ $ fd = $ this ->openFileWithLock ($ filename );
220
212
213
+ if (!$ fd ) {
221
214
return null ;
222
215
}
223
216
@@ -259,7 +252,11 @@ private function move(string $filename): ?string
259
252
{
260
253
clearstatcache ();
261
254
262
- $ filenameTarget = tempnam (dirname ($ filename ), 'LOG ' );
255
+ $ filenameTarget = $ this ->getTempFilename (dirname ($ filename ));
256
+
257
+ if (!$ filenameTarget ) {
258
+ return null ;
259
+ }
263
260
264
261
if (!rename ($ filename , $ filenameTarget )) {
265
262
$ this ->exception (
@@ -274,4 +271,47 @@ private function move(string $filename): ?string
274
271
275
272
return $ filenameTarget ;
276
273
}
274
+
275
+ private function getTempFilename (string $ path ): ?string
276
+ {
277
+ $ filename = tempnam ($ path , 'LOG ' );
278
+
279
+ if ($ filename === false ) {
280
+ $ this ->exception (
281
+ new Exception (sprintf ('the file %s not can create temp file. ' , $ path ), 19 )
282
+ );
283
+
284
+ return null ;
285
+ }
286
+
287
+ return $ filename ;
288
+ }
289
+
290
+ /**
291
+ * @return null|resource
292
+ */
293
+ private function openFileWithLock (string $ filename )
294
+ {
295
+ $ fd = fopen ($ filename , 'r+ ' );
296
+
297
+ if ($ fd === false ) {
298
+ $ this ->exception (
299
+ new Exception (sprintf ('the file %s not can open. ' , $ filename ), 20 )
300
+ );
301
+
302
+ return null ;
303
+ }
304
+
305
+ if (!flock ($ fd , LOCK_EX )) {
306
+ fclose ($ fd );
307
+
308
+ $ this ->exception (
309
+ new Exception (sprintf ('the file %s not can lock. ' , $ filename ), 21 )
310
+ );
311
+
312
+ return null ;
313
+ }
314
+
315
+ return $ fd ;
316
+ }
277
317
}
0 commit comments