-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Config.php
84 lines (70 loc) · 1.55 KB
/
Config.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
<?php
namespace Kirby\Retour;
use Kirby\Data\Data;
use Kirby\Exception\LogicException;
use Throwable;
/**
* Handles reading from/writing to the config file
*
* @package Retour for Kirby
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://github.com/distantnative/retour-for-kirby
* @copyright Nico Hoffmann
* @license https://opensource.org/licenses/MIT
*/
class Config
{
protected array $data;
public function __construct(
protected Retour $retour
) {
$this->read();
}
public function data(string|null $key = null): mixed
{
if ($key === null) {
return $this->data;
}
return $this->data[$key] ?? null;
}
public function file(): string
{
$retour = $this->retour();
$default = $retour->kirby()->root('config') . '/retour.yml';
$path = $retour->option('config', $default);
if (is_callable($path) === true) {
$path = $path();
}
return $path;
}
public function read(): array
{
try {
$file = $this->file();
return $this->data = Data::read($file);
} catch (Throwable) {
return $this->data = [];
}
}
public function retour(): Retour
{
return $this->retour;
}
public function write(
string|array $key,
mixed $value = null
): array {
$data = $this->data;
if (is_array($key) === true) {
$data = array_merge($data, $key);
} else {
$data[$key] = $value;
}
if (Data::write($this->file(), $data) === true) {
return $this->data = $data;
}
// @codeCoverageIgnoreStart
throw new LogicException('Retour: writing config file failed');
// @codeCoverageIgnoreEnd
}
}