-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLogReaderFactory.php
80 lines (64 loc) · 2.07 KB
/
LogReaderFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\LogViewer;
use Exception;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Plugins\LogViewer\Log\Reader\Database;
use Piwik\Plugins\LogViewer\Log\Reader\File;
class LogReaderFactory
{
private $supportedLogReader = array('file', 'database');
public function make($source)
{
if (empty($source)) {
$source = $this->tryToDetectBestLogReader();
}
switch ($source) {
case 'file':
$testFilePath = '';
try {
$testFilePath = StaticContainer::get('test.vars.logForReading');
} catch (\Throwable $th) {
// Do nothing
}
$path = $testFilePath ?: StaticContainer::get('log.file.filename');
return new File($path);
case 'database':
return new Database();
default:
throw new Exception('Wrong source specified. Such log reader does not exist: ' . $source);
}
}
public function getAvailableLogReaders()
{
return $this->supportedLogReader;
}
public function getConfiguredLogReaders()
{
$logWriters = Config::getInstance()->log['log_writers'];
if (!is_array($logWriters)) {
$logWriters = array($logWriters);
}
$configured = array();
foreach ($logWriters as $key => $logWriter) {
if (in_array($logWriter, $this->supportedLogReader)) {
$configured[] = $logWriter;
}
}
return $configured;
}
private function tryToDetectBestLogReader()
{
$logWriters = $this->getConfiguredLogReaders();
if (count($logWriters) === 1) {
return array_shift($logWriters);
}
throw new Exception('No source specified, please specify one of: ' . implode(', ', $this->supportedLogReader));
}
}