Skip to content

Commit beef537

Browse files
committed
add method then
1 parent 253d1b3 commit beef537

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

README.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@ composer require cesargb/php-log-rotation
1717

1818
## Usage
1919

20-
This is an example:
21-
2220
```php
2321
use Cesargb\Log\Rotation;
2422

2523
$rotation = new Rotation();
2624

27-
// Rotate a file
28-
$rotation->rotate('file.log');
29-
30-
// Log files are rotated 10 times before being removed, 366 default
31-
$rotation->files(30)->rotate('file.log');
32-
33-
// Compress file rotated
34-
$rotation->compress()->rotate('file.log');
35-
36-
// Log files are rotated only if they grow bigger then 1024 bytes
37-
$rotation->minSize(1024)->rotate('file.log');
25+
$rotation
26+
->compress() // Optional, compress the file after rotated
27+
->files(30) // Optional, files are rotated 30 times before being removed
28+
->minSize(1024) // Optional, are rotated when they grow bigger than 1024 bytes
29+
->rotate('file.log');
3830
```
3931

4032
## Test

src/Rotation.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Cesargb\Log;
44

55
use Cesargb\Log\Compress\Gz;
6-
use LogicException;
76
use Cesargb\Log\Processors\RotativeProcessor;
7+
use LogicException;
88

99
class Rotation
1010
{
@@ -14,6 +14,8 @@ class Rotation
1414

1515
private int $_minSize = 0;
1616

17+
private $thenCallback = null;
18+
1719
public function __construct()
1820
{
1921
$this->processor = new RotativeProcessor();
@@ -59,16 +61,30 @@ public function minSize(int $bytes): self
5961
return $this;
6062
}
6163

64+
/**
65+
* Call function if roteted was sucessfull and pass
66+
* the file as argument.
67+
*
68+
* @param callable $callable
69+
* @return self
70+
*/
71+
public function then(callable $callable): self
72+
{
73+
$this->thenCallback = $callable;
74+
75+
return $this;
76+
}
77+
6278
/**
6379
* Rotate file
6480
*
6581
* @param string $file
66-
* @return string|null
82+
* @return boolean true if rotated was successful
6783
*/
68-
public function rotate(string $file): ?string
84+
public function rotate(string $file): bool
6985
{
7086
if (!$this->canRotate($file)) {
71-
return null;
87+
return false;
7288
}
7389

7490
$fileRotate = $this->moveContentToTempFile($file);
@@ -81,7 +97,11 @@ public function rotate(string $file): ?string
8197
$fileRotated = $gz->handler($fileRotated);
8298
}
8399

84-
return $fileRotated;
100+
if ($fileRotated && $this->thenCallback) {
101+
call_user_func($this->thenCallback, $fileRotated);
102+
}
103+
104+
return ! empty($fileRotated);
85105
}
86106

87107
/**

tests/Compress/GzTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Cesargb\Log\Test\Processors;
44

5-
use LogicException;
65
use Cesargb\Log\Rotation;
76
use Cesargb\Log\Test\TestCase;
87

@@ -24,12 +23,12 @@ public function test_rotation_processor_with_gz_processor()
2423

2524
file_put_contents(self::DIR_WORK.'file.log', $content);
2625

27-
$rotated_file = $rotation->rotate(self::DIR_WORK.'file.log');
26+
$rotation->then(function ($fileRotated) {
27+
$this->assertEquals(self::DIR_WORK.'file.log.1.gz', $fileRotated);
28+
})->rotate(self::DIR_WORK.'file.log');
2829

2930
$this->assertStringEqualsFile(self::DIR_WORK.'file.log', '');
3031

31-
$this->assertEquals(self::DIR_WORK.'file.log.1.gz', $rotated_file);
32-
3332
$this->assertFileExists(self::DIR_WORK.'file.log.1.gz');
3433

3534
$this->assertEquals($content, implode("", gzfile(self::DIR_WORK.'file.log.1.gz')));

tests/RotationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Cesargb\Log\Rotation;
77
use Cesargb\Log\Test\TestCase;
88

9-
class RotativeTest extends TestCase
9+
class RotationTest extends TestCase
1010
{
1111
public function test_log_rotating_if_file_not_exists()
1212
{

0 commit comments

Comments
 (0)