1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Config |
5
|
|
|
* |
6
|
|
|
* Platine Config is the library used to manage the application |
7
|
|
|
* configuration using differents loaders |
8
|
|
|
* |
9
|
|
|
* This content is released under the MIT License (MIT) |
10
|
|
|
* |
11
|
|
|
* Copyright (c) 2020 Platine Config |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file FileLoader.php |
34
|
|
|
* |
35
|
|
|
* The Configuration Loader class uses by default PHP file as |
36
|
|
|
* application managed configuration |
37
|
|
|
* |
38
|
|
|
* @package Platine\Config |
39
|
|
|
* @author Platine Developers Team |
40
|
|
|
* @copyright Copyright (c) 2020 |
41
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
42
|
|
|
* @link https://www.platine-php.com |
43
|
|
|
* @version 1.0.0 |
44
|
|
|
* @filesource |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
declare(strict_types=1); |
48
|
|
|
|
49
|
|
|
namespace Platine\Config; |
50
|
|
|
|
51
|
|
|
class FileLoader implements LoaderInterface |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* The application base path to use for |
55
|
|
|
* configuration scanning |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected string $path; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create new instance of file loader |
62
|
|
|
* @param string $path the base path to use |
63
|
|
|
*/ |
64
|
|
|
public function __construct(string $path) |
65
|
|
|
{ |
66
|
|
|
$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function load(string $environment, string $group): array |
73
|
|
|
{ |
74
|
|
|
$configPath = ''; |
75
|
|
|
$items = []; |
76
|
|
|
|
77
|
|
|
foreach ($this->parse($environment) as $env) { |
78
|
|
|
$configPath .= $env ? $env . DIRECTORY_SEPARATOR : ''; |
79
|
|
|
$file = sprintf('%s%s%s.php', $this->path, $configPath, $group); |
80
|
|
|
if (is_file($file)) { |
81
|
|
|
$items = $this->merge($items, $this->readFile($file)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $items; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return the file path |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getPath(): string |
93
|
|
|
{ |
94
|
|
|
return $this->path; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the file path |
99
|
|
|
* |
100
|
|
|
* @param string $path |
101
|
|
|
* @return FileLoader |
102
|
|
|
*/ |
103
|
|
|
public function setPath(string $path): self |
104
|
|
|
{ |
105
|
|
|
$this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Read the content for the given file |
111
|
|
|
* @param string $file the file path to read |
112
|
|
|
* @return array<string, mixed> the content of file in array |
113
|
|
|
*/ |
114
|
|
|
protected function readFile(string $file): array |
115
|
|
|
{ |
116
|
|
|
/** @var array<string, mixed> */ |
117
|
|
|
$items = include $file; |
118
|
|
|
return $items; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Split the environment at dots or slashes creating |
123
|
|
|
* an array of name spaces to look through |
124
|
|
|
* |
125
|
|
|
* @param string $env |
126
|
|
|
* @return array<int, string> |
127
|
|
|
*/ |
128
|
|
|
protected function parse(string $env): array |
129
|
|
|
{ |
130
|
|
|
$environments = array_filter((array)preg_split('/(\/|\.)/', $env)); |
131
|
|
|
array_unshift($environments, ''); |
132
|
|
|
|
133
|
|
|
return $environments; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Merge two array items |
138
|
|
|
* @param array<string, mixed> $items1 |
139
|
|
|
* @param array<string, mixed> $items2 |
140
|
|
|
* @return array<string, mixed> |
141
|
|
|
*/ |
142
|
|
|
protected function merge(array $items1, array $items2): array |
143
|
|
|
{ |
144
|
|
|
return array_replace_recursive($items1, $items2); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|