1 | <?php |
||
33 | class ConfigFileLoader extends ObjectAbstract implements ConfigLoaderInterface |
||
34 | { |
||
35 | /** |
||
36 | * Config root directory |
||
37 | * |
||
38 | * @var string |
||
39 | * @access protected |
||
40 | */ |
||
41 | protected $root_dir; |
||
42 | |||
43 | /** |
||
44 | * config file type |
||
45 | * |
||
46 | * @var string |
||
47 | * @access protected |
||
48 | */ |
||
49 | protected $file_type; |
||
50 | |||
51 | /** |
||
52 | * cached subdirs to load files |
||
53 | * |
||
54 | * @var array |
||
55 | * @access protected |
||
56 | */ |
||
57 | protected $sub_dirs = []; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | * |
||
62 | * @param string $rootDir |
||
63 | * @param string $environment |
||
64 | * @param string $fileType |
||
65 | * @throws InvalidArgumentException if any argument invalid |
||
66 | * @access public |
||
67 | * @api |
||
68 | */ |
||
69 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * {@inheritDoc} |
||
82 | */ |
||
83 | public function load( |
||
84 | /*# string */ $group, |
||
85 | $environment = null |
||
86 | )/*# : array */ { |
||
87 | $data = []; |
||
88 | foreach ($this->globFiles($group, $environment) as $file) { |
||
89 | $grp = basename($file, '.' . $this->file_type); |
||
90 | if (!isset($data[$grp])) { |
||
91 | $data[$grp] = []; |
||
92 | } |
||
93 | try { |
||
94 | $data[$grp] = array_replace_recursive( |
||
95 | $data[$grp], |
||
96 | (array) Reader::readFile($file) |
||
97 | ); |
||
98 | } catch (\Exception $e) { |
||
99 | throw new LogicException($e->getMessage(), $e->getCode()); |
||
100 | } |
||
101 | } |
||
102 | return $data; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Set config file root directory |
||
107 | * |
||
108 | * @param string $rootDir |
||
109 | * @return $this |
||
110 | * @throws InvalidArgumentException if dir is bad |
||
111 | * @access public |
||
112 | * @api |
||
113 | */ |
||
114 | public function setRootDir(/*# string */ $rootDir) |
||
115 | { |
||
116 | $dir = realpath($rootDir); |
||
117 | if (false === $dir) { |
||
118 | throw new InvalidArgumentException( |
||
119 | Message::get(Message::CONFIG_ROOT_INVALID, $rootDir), |
||
120 | Message::CONFIG_ROOT_INVALID |
||
121 | ); |
||
122 | } |
||
123 | |||
124 | $this->root_dir = $dir . \DIRECTORY_SEPARATOR; |
||
125 | |||
126 | return $this; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Set config file type |
||
131 | * |
||
132 | * @param string $fileType |
||
133 | * @return $this |
||
134 | * @throws InvalidArgumentException if unsupported file type |
||
135 | * @access public |
||
136 | * @api |
||
137 | */ |
||
138 | public function setFileType(/*# string */ $fileType) |
||
143 | |||
144 | /** |
||
145 | * Set environment |
||
146 | * |
||
147 | * @param string $environment |
||
148 | * @return $this |
||
149 | * @access public |
||
150 | * @api |
||
151 | */ |
||
152 | public function setEnvironment(/*# string */ $environment) |
||
157 | |||
158 | /** |
||
159 | * Returns an array of files to read from |
||
160 | * |
||
161 | * @param string $group |
||
162 | * @param null|string $environment |
||
163 | * @return array |
||
164 | * @access protected |
||
165 | */ |
||
166 | protected function globFiles( |
||
178 | |||
179 | /** |
||
180 | * Build search directories |
||
181 | * |
||
182 | * @param null|string $environment |
||
183 | * @return array |
||
184 | * @access protected |
||
185 | */ |
||
186 | protected function buildSearchDirs($environment)/*# : array */ |
||
204 | } |
||
205 |