Skip to content

Commit 95ec095

Browse files
authored
Merge pull request #161 from opcodesio/carbon-interface
use CarbonInterface typehints for compatibility with immutable Carbon instances
2 parents 436d997 + c35dd87 commit 95ec095

File tree

7 files changed

+21
-18
lines changed

7 files changed

+21
-18
lines changed

config/log-viewer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@
122122
* $matches[7] - the log text, the rest of the text.
123123
*/
124124
'log_parsing_regex' => '/^\[(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}\.?(\d{6}([\+-]\d\d:\d\d)?)?)\](.*?(\w+)\.|.*?)('
125-
. implode('|', array_filter(Level::caseValues()))
126-
. ')?: (.*?)( in [\/].*?:[0-9]+)?$/is',
125+
.implode('|', array_filter(Level::caseValues()))
126+
.')?: (.*?)( in [\/].*?:[0-9]+)?$/is',
127127
],
128128
],
129129

src/Concerns/CanFilterIndex.php

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

33
namespace Opcodes\LogViewer\Concerns;
44

5-
use Carbon\Carbon;
5+
use Carbon\CarbonInterface;
66
use Opcodes\LogViewer\Level;
77

88
trait CanFilterIndex
@@ -33,13 +33,13 @@ public function getQuery(): ?string
3333
return $this->query;
3434
}
3535

36-
public function forDateRange(int|Carbon $from = null, int|Carbon $to = null): self
36+
public function forDateRange(int|CarbonInterface $from = null, int|CarbonInterface $to = null): self
3737
{
38-
if ($from instanceof Carbon) {
38+
if ($from instanceof CarbonInterface) {
3939
$from = $from->timestamp;
4040
}
4141

42-
if ($to instanceof Carbon) {
42+
if ($to instanceof CarbonInterface) {
4343
$to = $to->timestamp;
4444
}
4545

src/Log.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Opcodes\LogViewer;
44

5+
use Carbon\CarbonInterface;
56
use Illuminate\Support\Carbon;
67
use Illuminate\Support\Str;
78
use Opcodes\LogViewer\Facades\LogViewer;
@@ -11,7 +12,7 @@ class Log
1112
{
1213
public int $index;
1314

14-
public Carbon $time;
15+
public CarbonInterface $time;
1516

1617
public Level $level;
1718

src/LogFile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Opcodes\LogViewer;
44

5+
use Carbon\CarbonInterface;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Facades\Cache;
78
use Illuminate\Support\Str;
@@ -88,7 +89,7 @@ public function download(): BinaryFileResponse
8889
return response()->download($this->path);
8990
}
9091

91-
protected function cacheTtl()
92+
protected function cacheTtl(): CarbonInterface
9293
{
9394
return now()->addWeek();
9495
}

src/LogIndex.php

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

33
namespace Opcodes\LogViewer;
44

5-
use Carbon\Carbon;
5+
use Carbon\CarbonInterface;
6+
use Illuminate\Support\Carbon;
67
use Illuminate\Support\Collection;
78
use Illuminate\Support\Facades\Cache;
89

@@ -47,7 +48,7 @@ public function metaCacheKey(): string
4748
return $this->file->cacheKey().':'.$this->identifier().':metadata';
4849
}
4950

50-
public function cacheTtl(): Carbon
51+
public function cacheTtl(): CarbonInterface
5152
{
5253
if (! empty($this->query)) {
5354
// There will be a lot more search queries, and they're usually just one-off searches.
@@ -69,11 +70,11 @@ public function clearCache(): void
6970
$this->loadMetadata();
7071
}
7172

72-
public function addToIndex(int $filePosition, int|Carbon $timestamp, string $severity, int $index = null): int
73+
public function addToIndex(int $filePosition, int|CarbonInterface $timestamp, string $severity, int $index = null): int
7374
{
7475
$logIndex = $index ?? $this->nextLogIndexToCreate ?? 0;
7576

76-
if ($timestamp instanceof Carbon) {
77+
if ($timestamp instanceof CarbonInterface) {
7778
$timestamp = $timestamp->timestamp;
7879
}
7980

@@ -338,7 +339,7 @@ public function getEarliestTimestamp(): ?int
338339
return $earliestTimestamp;
339340
}
340341

341-
public function getEarliestDate(): Carbon
342+
public function getEarliestDate(): CarbonInterface
342343
{
343344
return Carbon::createFromTimestamp($this->getEarliestTimestamp());
344345
}
@@ -369,7 +370,7 @@ public function getLatestTimestamp(): ?int
369370
return $latestTimestamp;
370371
}
371372

372-
public function getLatestDate(): Carbon
373+
public function getLatestDate(): CarbonInterface
373374
{
374375
return Carbon::createFromTimestamp($this->getLatestTimestamp());
375376
}

src/LogViewerServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function boot()
7272

7373
private function basePath(string $path): string
7474
{
75-
return __DIR__ . '/..' . $path;
75+
return __DIR__.'/..'.$path;
7676
}
7777

7878
private function isEnabled(): bool

tests/Pest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Carbon\Carbon;
3+
use Carbon\CarbonInterface;
44
use Illuminate\Support\Facades\File;
55
use Opcodes\LogViewer\LogFile;
66
use Opcodes\LogViewer\LogIndex;
@@ -65,9 +65,9 @@ function clearGeneratedLogFiles(): void
6565
File::cleanDirectory(storage_path('logs'));
6666
}
6767

68-
function makeLogEntry(Carbon $date = null, string $level = 'debug', string $message = 'Testing log entry'): string
68+
function makeLogEntry(CarbonInterface $date = null, string $level = 'debug', string $message = 'Testing log entry'): string
6969
{
70-
$dateFormatted = $date instanceof Carbon ? $date->toDateTimeString() : now()->toDateTimeString();
70+
$dateFormatted = $date instanceof CarbonInterface ? $date->toDateTimeString() : now()->toDateTimeString();
7171
$level = strtoupper($level);
7272

7373
return "[$dateFormatted] local.$level: $message";

0 commit comments

Comments
 (0)