Skip to content

Commit e177dc7

Browse files
committed
Various improvements
1 parent c658896 commit e177dc7

File tree

6 files changed

+277
-131
lines changed

6 files changed

+277
-131
lines changed

lib/Console/Command/LintCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private function prepareCheckLine($checkLines, $check, $level = 0)
9999
$checkLines[] = $line;
100100

101101
if (isset($check['error']) && $check['error'] != '') {
102-
$prefix = $check['status'] == 'warn' ? '<fg=yellow>Warning:</>' : '<fg=red>Error:</>';
102+
$prefix = $check['status'] === 'warn' ? '<fg=yellow>Warning:</>' : '<fg=red>Error:</>';
103103
$checkLines[][] = new TableCell($indentation . ' ' . $prefix . ' ' . $check['error'], ['colspan' => 2]);
104104
}
105105

lib/LogApiHelper.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Syonix\LogViewer;
4+
5+
class LogApiHelper
6+
{
7+
private LogManager $manager;
8+
9+
public function __construct(LogManager $manager)
10+
{
11+
$this->manager = $manager;
12+
}
13+
14+
public function getLogs($urlPrefix = ''): array
15+
{
16+
$result = [];
17+
foreach ($this->manager->getCollections() as $collection) {
18+
$row = [
19+
'name' => $collection->getName(),
20+
'slug' => $collection->getSlug(),
21+
'url' => "$urlPrefix/" . $collection->getSlug(),
22+
];
23+
24+
foreach ($collection->getLogs() as $log)
25+
$row['logs'][] = [
26+
'name' => $log->getName(),
27+
'slug' => $log->getSlug(),
28+
'url' => "$urlPrefix/" . $log->getIdentifier(),
29+
];
30+
31+
$result[] = $row;
32+
}
33+
34+
return $result;
35+
}
36+
37+
public function getLog($collection, $log, $limit, $offset = 0, array $filter = [], $urlPrefix = ''): array
38+
{
39+
$collection = $this->manager->getLogCollection($collection);
40+
$log = $this->manager->loadLog($collection->getLog($log));
41+
$totalLines = $log->countLines($filter);
42+
43+
$prevOffset = max($offset - $limit, 0);
44+
$nextOffset = $offset + $limit;
45+
46+
$id = $log->getIdentifier();
47+
$prev = $offset > 0 ? "$urlPrefix/$id?limit=$limit&offset=$prevOffset" : null;
48+
$next = $nextOffset < $totalLines ? "$urlPrefix/$id?limit=$limit&offset=$nextOffset" : null;
49+
50+
foreach ($filter as $k => $f) {
51+
if ($f === null)
52+
continue;
53+
54+
if ($prev !== null)
55+
$prev .= "&$k=$f";
56+
57+
if ($next !== null)
58+
$next .= "&$k=$f";
59+
}
60+
61+
// TODO: Use proper URL Builder
62+
63+
return [
64+
'log' => $log,
65+
'name' => $log->getName(),
66+
'collection' => $collection,
67+
'lines' => $log->getLines($limit, $offset, $filter),
68+
'total_lines' => $totalLines,
69+
'offset' => $offset,
70+
'limit' => $limit,
71+
'loggers' => $log->getLoggers()->toArray(),
72+
'prev' => $prev,
73+
'next' => $next,
74+
];
75+
}
76+
}

0 commit comments

Comments
 (0)