|
1
|
|
|
<?php |
|
2
|
|
|
namespace ConfigTree\FileParsing; |
|
3
|
|
|
|
|
4
|
|
|
use ConfigTree\Exception\FileFormatNotParsable; |
|
5
|
|
|
use ConfigTree\FileParsing\Formats\IniFile; |
|
6
|
|
|
use ConfigTree\FileParsing\Formats\JsonFile; |
|
7
|
|
|
use ConfigTree\FileParsing\Formats\PhpArrayFile; |
|
8
|
|
|
use ConfigTree\FileParsing\Formats\YamlFile; |
|
9
|
|
|
use ConfigTree\FileParsing\Reading\FileContentsRetriever; |
|
10
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
|
11
|
|
|
|
|
12
|
|
|
class ArrayableFileFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @throws FileFormatNotParsable |
|
16
|
|
|
*/ |
|
17
|
|
|
public function getArrayableFileFromPath(string $pathToFile) : ArrayableFile |
|
18
|
|
|
{ |
|
19
|
|
|
$fileExtension = pathinfo($pathToFile)['extension']; |
|
20
|
|
|
|
|
21
|
|
|
switch ($fileExtension) { |
|
22
|
|
|
case ('ini'): |
|
23
|
|
|
return $this->buildIniArrayableFile($pathToFile); |
|
24
|
|
|
case ('php'): |
|
25
|
|
|
return $this->buildPhpArrayableFile($pathToFile); |
|
26
|
|
|
case ('json'): |
|
27
|
|
|
return $this->buildJsonArrayableFile($pathToFile); |
|
28
|
|
|
case ('yaml'): |
|
29
|
|
|
return $this->buildYamlArrayableFile($pathToFile); |
|
30
|
|
|
case ('yml'): |
|
31
|
|
|
return $this->buildYamlArrayableFile($pathToFile); |
|
32
|
|
|
default: |
|
33
|
|
|
throw FileFormatNotParsable::constructWithFilePath($pathToFile); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function buildIniArrayableFile(string $path) : IniFile |
|
38
|
|
|
{ |
|
39
|
|
|
$fileContentsRetriever = new FileContentsRetriever(); |
|
40
|
|
|
return new IniFile($path, $fileContentsRetriever); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function buildPhpArrayableFile(string $path) : PhpArrayFile |
|
44
|
|
|
{ |
|
45
|
|
|
return new PhpArrayFile($path); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private function buildJsonArrayableFile(string $path) : JsonFile |
|
49
|
|
|
{ |
|
50
|
|
|
$fileContentsRetriever = new FileContentsRetriever(); |
|
51
|
|
|
return new JsonFile($path, $fileContentsRetriever); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function buildYamlArrayableFile(string $path) : YamlFile |
|
55
|
|
|
{ |
|
56
|
|
|
$fileContentsRetriever = new FileContentsRetriever(); |
|
57
|
|
|
$yamlParser = new YamlParser(); |
|
58
|
|
|
return new YamlFile($path, $fileContentsRetriever, $yamlParser); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|