-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
128 lines (117 loc) · 2.72 KB
/
Config.php
File metadata and controls
128 lines (117 loc) · 2.72 KB
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
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
namespace Hurtcode\Config\Yaml;
use Hurtcode\Config\{ConfigInterface, ConfigureException};
use Yiisoft\Json\Json;
use JsonException;
/**
* Configuration resources
*
* Contains folder and enter point configuration file.
* Gets content files of yaml files
*
* @package Hurtcode\Config\Yaml
*/
final class Config implements ConfigInterface
{
/**
* Name of enter point file
*
* @var string
*/
private string $file;
/**
* Configurations path
*
* Path of directory with configuration.
*
* @var string
*/
private string $dir;
/**
* Config constructor
*
* @param string $dir
* @param string $file
*
* @throws ConfigureException
*/
public function __construct(string $dir, string $file)
{
$this->ensureDir($dir);
$this->ensureEnterFile($dir, $file);
$this->dir = $dir;
$this->file = $file;
}
/**
* Ensures is directory correct
*
* @param string $dir
*
* @throws ConfigureException
*/
private function ensureDir(string $dir): void
{
if (!file_exists($dir) || !is_dir($dir) || !is_readable($dir)) {
throw new ConfigureException("$dir doesn't exist or readable!");
}
}
/**
* Ensures is enter point file correct
*
* @param string $dir
* @param string $file
*
* @throws ConfigureException
*/
private function ensureEnterFile(string $dir, string $file): void
{
$main = "$dir/$file.yaml";
if (!file_exists($main) || !is_readable($main)) {
throw new ConfigureException("Enter file $main doesn't exist or readable!");
}
}
/**
* {@inheritdoc}
*/
public function dir(): string
{
return $this->dir;
}
/**
* {@inheritdoc}
*/
public function main(): string
{
return $this->getContent("$this->dir/$this->file.yaml");
}
/**
* {@inheritdoc}
*/
public function sub(string $path): string
{
return $this->getContent("$this->dir/$path.yaml");
}
/**
* Gets file content
*
* @param string $path
*
* @return string
*
* @throws ConfigureException
*/
private function getContent(string $path): string
{
$res = file_get_contents($path);
if (!$res) {
try {
$error = Json::encode(error_get_last());
throw new ConfigureException("Error: $error, while reading $path!");
} catch (JsonException) {
throw new ConfigureException("There is an error while reading $path!");
}
}
return $res;
}
}