-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileLoader.php
More file actions
206 lines (171 loc) · 5.96 KB
/
FileLoader.php
File metadata and controls
206 lines (171 loc) · 5.96 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Orchestra\Config;
use Illuminate\Filesystem\Filesystem;
class FileLoader implements LoaderInterface
{
/**
* The filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* The default configuration path.
*
* @var string
*/
protected $defaultPath;
/**
* All of the named path hints.
*
* @var array
*/
protected $hints = [];
/**
* A cache of whether namespaces and groups exists.
*
* @var array
*/
protected $exists = [];
/**
* Create a new file configuration loader.
*/
public function __construct(Filesystem $files, string $defaultPath)
{
$this->files = $files;
$this->defaultPath = $defaultPath;
}
/**
* Load the given configuration group.
*/
public function load(string $environment, string $group, ?string $namespace = null): array
{
$items = [];
// First we'll get the root configuration path for the environment which is
// where all of the configuration files live for that namespace, as well
// as any environment folders with their specific configuration items.
$path = $this->getPath($namespace);
if (\is_null($path)) {
return $items;
}
// First we'll get the main configuration file for the groups. Once we have
// that we can check for any environment specific files, which will get
// merged on top of the main arrays to make the environments cascade.
$file = "{$path}/{$group}.php";
if ($this->files->exists($file)) {
$items = $this->files->getRequire($file);
}
// Finally we're ready to check for the environment specific configuration
// file which will be merged on top of the main arrays so that they get
// precedence over them if we are currently in an environments setup.
$file = "{$path}/{$environment}/{$group}.php";
if ($this->files->exists($file)) {
$items = $this->mergeEnvironment($items, $file);
}
return $items;
}
/**
* Merge the items in the given file into the items.
*/
protected function mergeEnvironment(array $items, string $file): array
{
return \array_replace_recursive($items, $this->files->getRequire($file));
}
/**
* Determine if the given group exists.
*/
public function exists(string $group, ?string $namespace = null): bool
{
$key = $group.$namespace;
// We'll first check to see if we have determined if this namespace and
// group combination have been checked before. If they have, we will
// just return the cached result so we don't have to hit the disk.
if (! isset($this->exists[$key])) {
$path = $this->getPath($namespace);
// To check if a group exists, we will simply get the path based on the
// namespace, and then check to see if this files exists within that
// namespace. False is returned if no path exists for a namespace.
if (\is_null($path)) {
return $this->exists[$key] = false;
}
$file = "{$path}/{$group}.php";
// Finally, we can simply check if this file exists. We will also cache
// the value in an array so we don't have to go through this process
// again on subsequent checks for the existing of the config file.
$this->exists[$key] = $this->files->exists($file);
}
return $this->exists[$key];
}
/**
* Apply any cascades to an array of package options.
*/
public function cascadePackage(string $env, string $package, string $group, array $items): array
{
// First we will look for a configuration file in the packages configuration
// folder. If it exists, we will load it and merge it with these original
// options so that we will easily "cascade" a package's configurations.
$file = "packages/{$package}/{$group}.php";
if ($this->files->exists($path = $this->defaultPath.'/'.$file)) {
$items = \array_merge($items, $this->getRequire($path));
}
// Once we have merged the regular package configuration we need to look for
// an environment specific configuration file. If one exists, we will get
// the contents and merge them on top of this array of options we have.
$path = $this->getPackagePath($env, $package, $group);
if ($this->files->exists($path)) {
$items = \array_merge($items, $this->getRequire($path));
}
return $items;
}
/**
* Get the package path for an environment and group.
*/
protected function getPackagePath(string $env, string $package, string $group): string
{
$file = "packages/{$package}/{$env}/{$group}.php";
return $this->defaultPath.'/'.$file;
}
/**
* Get the configuration path for a namespace.
*/
protected function getPath(?string $namespace): ?string
{
if (\is_null($namespace)) {
return $this->defaultPath;
} elseif (isset($this->hints[$namespace])) {
return $this->hints[$namespace];
}
return null;
}
/**
* Add a new namespace to the loader.
*/
public function addNamespace(string $namespace, string $hint): void
{
$this->hints[$namespace] = $hint;
}
/**
* Returns all registered namespaces with the config
* loader.
*/
public function getNamespaces(): array
{
return $this->hints;
}
/**
* Get a file's contents by requiring it.
*
* @return mixed
*/
protected function getRequire(string $path)
{
return $this->files->getRequire($path);
}
/**
* Get the Filesystem instance.
*/
public function getFilesystem(): Filesystem
{
return $this->files;
}
}