1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CCT\Component\Rest\Serializer\Mapping\Loader; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; |
7
|
|
|
use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Load recursively all YAML or XML configuration files in services directories |
11
|
|
|
*/ |
12
|
|
|
class DirectoryLoader |
13
|
|
|
{ |
14
|
|
|
const FILE_TYPE_YAML = 'yaml'; |
15
|
|
|
const FILE_TYPE_XML = 'xml'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param $dir |
19
|
|
|
* @param string $type supported types are DirectoryLoader::FILE_TYPE_YAML or DirectoryLoader::FILE_TYPE_XML |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
7 |
|
public function load($dir, $type = self::FILE_TYPE_YAML) |
24
|
|
|
{ |
25
|
7 |
|
$dir = rtrim($dir, DIRECTORY_SEPARATOR); |
26
|
|
|
|
27
|
7 |
|
if (!is_dir($dir)) { |
28
|
1 |
|
throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir)); |
29
|
|
|
} |
30
|
|
|
|
31
|
6 |
|
if (!is_readable($dir)) { |
32
|
1 |
|
throw new InvalidArgumentException(sprintf('The directory "%s" is not readable.', $dir)); |
33
|
|
|
} |
34
|
|
|
|
35
|
5 |
|
return $this->scanDirectory($dir, $type); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Scan directory for files matching extension type |
40
|
|
|
* |
41
|
|
|
* @param string $dir |
42
|
|
|
* @param string $type |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
5 |
|
private function scanDirectory($dir, $type) |
47
|
|
|
{ |
48
|
5 |
|
$loaders = []; |
49
|
|
|
|
50
|
5 |
|
foreach (scandir($dir) as $file) { |
51
|
5 |
|
if (false === $this->checkFileType($file, $type)) { |
52
|
5 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
$this->isReadable($dir, $file); |
56
|
|
|
|
57
|
3 |
|
$loaders[] = $this->createLoader($dir . DIRECTORY_SEPARATOR . $file, $type); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
return $loaders; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks if file extension matches type |
65
|
|
|
* |
66
|
|
|
* @param string $file |
67
|
|
|
* @param string $type |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
5 |
|
private function checkFileType($file, $type) |
72
|
|
|
{ |
73
|
5 |
|
if (in_array($file, array(".", "..")) |
74
|
5 |
|
|| $type !== strtolower(pathinfo($file, PATHINFO_EXTENSION)) |
75
|
|
|
) { |
76
|
5 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Is file readable |
84
|
|
|
* |
85
|
|
|
* @param string $dir |
86
|
|
|
* @param string $file |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
3 |
|
private function isReadable($dir, $file) |
91
|
|
|
{ |
92
|
3 |
|
if (!is_readable($dir . DIRECTORY_SEPARATOR . $file)) { |
93
|
|
|
throw new InvalidArgumentException( |
94
|
|
|
sprintf( |
95
|
|
|
'The file "%s" is not readable.', |
96
|
|
|
$dir . DIRECTORY_SEPARATOR . $file |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create config loaders |
106
|
|
|
* |
107
|
|
|
* @param $file |
108
|
|
|
* @param $type |
109
|
|
|
* |
110
|
|
|
* @return XmlFileLoader|YamlFileLoader |
111
|
|
|
*/ |
112
|
3 |
|
private function createLoader($file, $type) |
113
|
|
|
{ |
114
|
3 |
|
if (self::FILE_TYPE_YAML === $type) { |
115
|
1 |
|
return new YamlFileLoader($file); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
if (self::FILE_TYPE_XML === $type) { |
119
|
1 |
|
return new XmlFileLoader($file); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
throw new InvalidArgumentException(sprintf('The file type "%s" is not supported.', $type)); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|