-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileConfig.php
121 lines (101 loc) · 3.15 KB
/
FileConfig.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace GBV\DAIA;
use DAIA\Entity;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
/** @package GBVDAIA */
class FileException extends \RuntimeException
{
}
/** @package GBVDAIA */
class FileConfig implements Config
{
protected $dir;
private $log;
private $indicators;
public function __construct(string $dir, $level = LogLevel::NOTICE)
{
$this->dir = $dir;
$this->logger($level); // make sure to have a logger
$this->readConfig();
}
protected function readConfig()
{
// TODO: read and enable logging configuration
// move to logging class?
// log standard to default log
// send critical errors to critical file
// log everything to detailled log
}
static function defaultLogger($level = LogLevel::NOTICE): LoggerInterface
{
// log warnings and errors to STDERR by default
$log = new \Monolog\Logger('GBVDAIA');
$stream = new \Monolog\Handler\StreamHandler('php://stderr', $level);
$stream->getFormatter()->ignoreEmptyContextAndExtra(true);
$log->pushHandler($stream);
return $log;
}
public function logger(): LoggerInterface
{
if (!$this->log) {
$this->log = $this->defaultLogger();
}
return $this->log;
}
private function readFile($file): string
{
$content = @file_get_contents($file);
if ($content === false) {
throw new FileException("Failed to read file $file");
}
return $content ;
}
private function readLoanIndicators()
{
$this->indicators = [];
$file = $this->dir . "/ausleihindikator.yaml";
try {
$this->indicators = Yaml::parse($this->readFile($file));
$this->log->debug("Read $file");
# TODO: validate file
} catch (FileException $e) {
$this->log->error($e->getMessage());
} catch (ParseException $e) {
$this->log->error("Failed to parse $file: ".$e->getMessage());
}
}
public function loanIndicator(string $dbkey, string $indicator): array
{
if (!$this->indicators) {
$this->readLoanIndicators();
}
$standard = $this->indicators[''] ?? [];
$dbconfig = $this->indicators[$dbkey] ?? $standard;
if ($indicator == '') {
$indicator = $dbconfig['default'] ?? $standard['default'] ?? '';
}
$services = $dbconfig[$indicator] ?? $standard[$indicator] ?? [];
krsort($services);
$this->log->debug("loanIndicator", [
'dbkey' => $dbkey,
'indicator' => $indicator,
'result' => $services
]);
return $services;
}
public function sst(string $isil, string $sst): array
{
// start with a dummy
$uri = "http://uri.gbv.de/organization/isil/$isil";
return [
'department' => new Entity(['uri'=>$uri]),
'storage' => null
];
}
public static $defaultLoanIndicator = [
];
}