Skip to content

Commit aaf36d3

Browse files
committed
refactor the LogFile to only have to accept a single argument in the constructor - the path of the log file
1 parent 0957781 commit aaf36d3

11 files changed

+55
-48
lines changed

src/LogFile.php

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,26 @@
1414

1515
class LogFile
1616
{
17+
public string $path;
18+
public string $name;
1719
public string $identifier;
18-
1920
public string $subFolder = '';
20-
2121
protected array $metaData;
22-
2322
private array $_logIndexCache;
2423

25-
public function __construct(
26-
public string $name,
27-
public string $path,
28-
) {
29-
$this->identifier = Str::substr(md5($path), -8, 8).'-'.$name;
24+
public function __construct(string $path)
25+
{
26+
$this->path = $path;
27+
$this->name = basename($path);
28+
$this->identifier = Str::substr(md5($path), -8, 8).'-'.$this->name;
3029

3130
// Let's remove the file name because we already know it.
32-
$this->subFolder = str_replace($name, '', $path);
31+
$this->subFolder = str_replace($this->name, '', $path);
3332
$this->subFolder = rtrim($this->subFolder, DIRECTORY_SEPARATOR);
3433

3534
$this->metaData = Cache::get($this->metaDataCacheKey(), []);
3635
}
3736

38-
public static function fromPath(string $filePath): LogFile
39-
{
40-
return new self(
41-
basename($filePath),
42-
$filePath,
43-
);
44-
}
45-
4637
public function index(string $query = null): LogIndex
4738
{
4839
if (! isset($this->_logIndexCache[$query])) {

src/LogViewerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getFiles(): LogFileCollection
8787
if (! isset($this->_cachedFiles)) {
8888
$this->_cachedFiles = (new LogFileCollection($this->getFilePaths()))
8989
->unique()
90-
->map(fn ($file) => LogFile::fromPath($file))
90+
->map(fn ($filePath) => new LogFile($filePath))
9191
->values();
9292
}
9393

tests/Pest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function generateLogFile(string $fileName = null, string $content = null, bool $
4545
// we perform a regular PHP assertion, so it doesn't count towards the unit test assertion count.
4646
assert(file_exists($path));
4747

48-
return new LogFile($fileName, $path);
48+
return new LogFile($path);
4949
}
5050

5151
function dummyLogData(int $lines = null): string
@@ -76,7 +76,7 @@ function makeLogEntry(CarbonInterface $date = null, string $level = 'debug', str
7676
function createLogIndex($file = null, $query = null, array $predefinedLogs = []): LogIndex
7777
{
7878
if (is_null($file)) {
79-
$file = new LogFile('test.log', 'test.log');
79+
$file = new LogFile('test.log');
8080
}
8181

8282
$logIndex = new LogIndex($file, $query);

tests/Unit/LogFileCollectionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
});
1111

1212
test('LogFileCollection can sort its files by earliest logs first', function () {
13-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
13+
$firstFile = Mockery::mock(new LogFile('test.log'))
1414
->allows(['earliestTimestamp' => now()->subDay()->timestamp]);
15-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
15+
$secondFile = Mockery::mock(new LogFile('test2.log'))
1616
->allows(['earliestTimestamp' => now()->subDays(2)->timestamp]);
1717
$collection = new LogFileCollection([$firstFile, $secondFile]);
1818

@@ -23,9 +23,9 @@
2323
});
2424

2525
test('LogFileCollection can sort its files by latest logs first', function () {
26-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
26+
$firstFile = Mockery::mock(new LogFile('test.log'))
2727
->allows(['latestTimestamp' => now()->subDays(2)->timestamp]);
28-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
28+
$secondFile = Mockery::mock(new LogFile('test2.log'))
2929
->allows(['latestTimestamp' => now()->subDay()->timestamp]);
3030
$collection = new LogFileCollection([$firstFile, $secondFile]);
3131

@@ -42,9 +42,9 @@
4242
});
4343

4444
test('LogFileCollection can return the latest log file', function () {
45-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
45+
$firstFile = Mockery::mock(new LogFile('test.log'))
4646
->allows(['latestTimestamp' => now()->subDays(2)->timestamp]);
47-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
47+
$secondFile = Mockery::mock(new LogFile('test2.log'))
4848
->allows(['latestTimestamp' => now()->subDay()->timestamp]);
4949
$collection = new LogFileCollection([$firstFile, $secondFile]);
5050

@@ -54,9 +54,9 @@
5454
});
5555

5656
test('LogFileCollection can return the earliest log file', function () {
57-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
57+
$firstFile = Mockery::mock(new LogFile('test.log'))
5858
->allows(['earliestTimestamp' => now()->subDay()->timestamp]);
59-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
59+
$secondFile = Mockery::mock(new LogFile('test2.log'))
6060
->allows(['earliestTimestamp' => now()->subDays(2)->timestamp]);
6161
$collection = new LogFileCollection([$firstFile, $secondFile]);
6262

tests/Unit/LogFileTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Opcodes\LogViewer\LogFile;
4+
5+
test('log file can be instantiated with just a path to the file', function () {
6+
$path = storage_path('logs/laravel.log');
7+
file_put_contents($path, str_repeat('0', 10)); // 10 bytes
8+
9+
$logFile = new LogFile($path);
10+
11+
expect($logFile->path)->toBe($path)
12+
->and($logFile->name)->toBe('laravel.log')
13+
->and($logFile->size())->toBe(10);
14+
});

tests/Unit/LogFolderCollectionTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
// Let's assume we have a simple collection of log files from different folders.
1515
// When we try and turn that flat collection into a LogFolderCollection, it
1616
// could automatically group the logs into folders!
17+
$debugFolder = 'debug';
18+
$infoFolder = 'info';
1719
$logFileCollection = new LogFileCollection([
18-
$debugFile1 = new LogFile('debug1.log', $debugFolder = 'debug'),
19-
$infoFile1 = new LogFile('info1.log', $infoFolder = 'info'),
20-
$debugFile2 = new Logfile('debug2.log', $debugFolder),
21-
$infoFile2 = new LogFile('info2.log', $infoFolder),
20+
$debugFile1 = new LogFile($debugFolder.'/debug1.log'),
21+
$infoFile1 = new LogFile($infoFolder.'/info1.log'),
22+
$debugFile2 = new Logfile($debugFolder.'/debug2.log'),
23+
$infoFile2 = new LogFile($infoFolder.'/info2.log'),
2224
]);
2325

2426
$folderCollection = LogFolderCollection::fromFiles($logFileCollection);
@@ -67,9 +69,9 @@
6769
});
6870

6971
test('LogFolderCollection can sort its folders by earliest first, including its files', function () {
70-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
72+
$firstFile = Mockery::mock(new LogFile('test.log'))
7173
->allows(['earliestTimestamp' => now()->subDay()->timestamp]);
72-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
74+
$secondFile = Mockery::mock(new LogFile('test2.log'))
7375
->allows(['earliestTimestamp' => now()->subDays(2)->timestamp]);
7476

7577
$dummyFolder = Mockery::mock(new LogFolder('folder2', []))
@@ -94,9 +96,9 @@
9496
});
9597

9698
test('LogFolderCollection can sort its folders by latest first, including its files', function () {
97-
$firstFile = Mockery::mock(new LogFile('test.log', 'test.log'))
99+
$firstFile = Mockery::mock(new LogFile('test.log'))
98100
->allows(['latestTimestamp' => now()->subDays(2)->timestamp]);
99-
$secondFile = Mockery::mock(new LogFile('test2.log', 'test2.log'))
101+
$secondFile = Mockery::mock(new LogFile('test2.log'))
100102
->allows(['latestTimestamp' => now()->subDay()->timestamp]);
101103

102104
$dummyFolder = Mockery::mock(new LogFolder('folder2', []))

tests/Unit/LogFolderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
use Opcodes\LogViewer\LogFolder;
55

66
test('LogFolder can get the earliest timestamp of the files it contains', function () {
7-
$firstFile = Mockery::mock(new LogFile('test.log', 'folder'))
7+
$firstFile = Mockery::mock(new LogFile('folder/test.log'))
88
->allows(['earliestTimestamp' => now()->subDay()->timestamp]);
9-
$secondFile = Mockery::mock(new LogFile('test2.log', 'folder'))
9+
$secondFile = Mockery::mock(new LogFile('folder/test2.log'))
1010
->allows(['earliestTimestamp' => now()->subDays(2)->timestamp]);
1111
$folder = new LogFolder('folder', [$firstFile, $secondFile]);
1212

1313
expect($folder->earliestTimestamp())->toBe($secondFile->earliestTimestamp());
1414
});
1515

1616
test('LogFolder can get the latest timestamp of the files it contains', function () {
17-
$firstFile = Mockery::mock(new LogFile('test.log', 'folder'))
17+
$firstFile = Mockery::mock(new LogFile('folder/test.log'))
1818
->allows(['latestTimestamp' => now()->subDay()->timestamp]);
19-
$secondFile = Mockery::mock(new LogFile('test2.log', 'folder'))
19+
$secondFile = Mockery::mock(new LogFile('folder/test2.log'))
2020
->allows(['latestTimestamp' => now()->subDays(2)->timestamp]);
2121
$folder = new LogFolder('folder', [$firstFile, $secondFile]);
2222

tests/Unit/LogIndex/CacheKeysTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Opcodes\LogViewer\LogFile;
44

55
it('can generate a cache key', function () {
6-
$file = Mockery::mock(new LogFile('test.log', 'test.log'))
6+
$file = Mockery::mock(new LogFile('test.log'))
77
->allows(['cacheKey' => $fileCacheKey = 'file-cache-key']);
88
$logIndex = createLogIndex($file);
99

@@ -19,7 +19,7 @@
1919
});
2020

2121
it('can generate a cache key for metadata', function () {
22-
$file = Mockery::mock(new LogFile('test.log', 'test.log'))
22+
$file = Mockery::mock(new LogFile('test.log'))
2323
->allows(['cacheKey' => $fileCacheKey = 'file-cache-key']);
2424
$logIndex = createLogIndex($file);
2525

@@ -35,7 +35,7 @@
3535
});
3636

3737
it('can generate a cache key for an index chunk', function () {
38-
$file = Mockery::mock(new LogFile('test.log', 'test.log'))
38+
$file = Mockery::mock(new LogFile('test.log'))
3939
->allows(['cacheKey' => $fileCacheKey = 'file-cache-key']);
4040
$logIndex = createLogIndex($file);
4141

tests/Unit/LogIndex/ChunkedIndicesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
});
8181

8282
it('remembers the number of chunks after re-instantiation', function () {
83-
$file = Mockery::mock(new LogFile('laravel.log', 'laravel.log'));
83+
$file = Mockery::mock(new LogFile('laravel.log'));
8484
$logIndex = createLogIndex($file);
8585
$logIndex->setMaxChunkSize(1);
8686
// 2 log entries at 1 per chunk = 3 chunks in total (2 full + 1 empty)

tests/Unit/LogIndex/LogIndexTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@
201201

202202
it('can check whether the index is incomplete', function () {
203203
$logIndex = createLogIndex(
204-
Mockery::mock(new LogFile('test.log', 'test.log'))->allows(['size' => 0])
204+
Mockery::mock(new LogFile('test.log'))->allows(['size' => 0])
205205
);
206206

207207
expect($logIndex->incomplete())->toBeFalse();
208208

209209
// if we then provide a file with some data in it (fake file size),
210210
// the log index should be considered incomplete
211211
$logIndex = createLogIndex(
212-
Mockery::mock(new LogFile('test.log', 'test.log'))->allows(['size' => 1000])
212+
Mockery::mock(new LogFile('test.log'))->allows(['size' => 1000])
213213
);
214214

215215
expect($logIndex->incomplete())->toBeTrue();
@@ -218,7 +218,7 @@
218218
// that the file was scanned at, was 1000. If the file size is still at 1000,
219219
// then the index should be considered complete.
220220
$logIndex = createLogIndex(
221-
Mockery::mock(new LogFile('test.log', 'test.log'))->allows(['size' => 1000])
221+
Mockery::mock(new LogFile('test.log'))->allows(['size' => 1000])
222222
);
223223
$logIndex->setLastScannedFilePosition(1000);
224224

0 commit comments

Comments
 (0)