|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wonderland\Container\Configuration\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
6
|
|
|
use Wonderland\Container\Exception\InvalidResourceException; |
|
7
|
|
|
use Wonderland\Container\Exception\ResourceNotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Yaml |
|
11
|
|
|
* @package Wonderland\Container\Configuration\Parser |
|
12
|
|
|
* @author Alice Praud <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class YamlParser implements ParserInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $filePath |
|
19
|
|
|
* @return array |
|
20
|
|
|
* @throws ResourceNotFoundException |
|
21
|
|
|
* @throws InvalidResourceException |
|
22
|
|
|
*/ |
|
23
|
14 |
|
public function loadFile(string $filePath): array |
|
24
|
|
|
{ |
|
25
|
14 |
|
if (false === file_exists($filePath)) { |
|
26
|
1 |
|
throw new ResourceNotFoundException('The file "' . $filePath . '" does not exists'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
13 |
|
if (false === is_file($filePath)) { |
|
30
|
1 |
|
throw new InvalidResourceException('The resource "' . $filePath . '" is not a file'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
12 |
|
$parse = Yaml::parseFile($filePath); |
|
34
|
|
|
|
|
35
|
12 |
|
if (false === is_array($parse)) { |
|
36
|
1 |
|
$parse = []; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
12 |
|
return $parse; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $directorPath |
|
44
|
|
|
* @return array |
|
45
|
|
|
* @throws InvalidResourceException |
|
46
|
|
|
* @throws ResourceNotFoundException |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function loadDirectory(string $directorPath): array |
|
49
|
|
|
{ |
|
50
|
4 |
|
if (false === file_exists($directorPath)) { |
|
51
|
1 |
|
throw new ResourceNotFoundException('The directory "' . $directorPath . '" does not exists'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
3 |
|
if (false === is_dir($directorPath)) { |
|
55
|
1 |
|
throw new InvalidResourceException('The resource "' . $directorPath . '" is not a directory'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
$config = []; |
|
59
|
|
|
|
|
60
|
2 |
|
$files = array_diff(scandir($directorPath), array('.', '..')); |
|
61
|
2 |
|
foreach ($files as $file) { |
|
62
|
2 |
|
if (false !== preg_match('/^.+(yml|yaml)$/i', $file)) { |
|
63
|
2 |
|
$config[$file] = Yaml::parseFile(realpath($directorPath) . DIRECTORY_SEPARATOR . $file); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
return $config; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|