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
|
1 |
|
public function load($dir, $type = self::FILE_TYPE_YAML) |
24
|
|
|
{ |
25
|
1 |
|
$dir = rtrim($dir, DIRECTORY_SEPARATOR); |
26
|
|
|
|
27
|
1 |
|
if (!is_dir($dir)) { |
28
|
|
|
throw new InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir)); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
return $this->scanDirectory($dir, $type); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Scan directory for files matching extention type |
36
|
|
|
* |
37
|
|
|
* @param string $dir |
38
|
|
|
* @param string $type |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
1 |
|
private function scanDirectory($dir, $type) |
43
|
|
|
{ |
44
|
1 |
|
$loaders = []; |
45
|
|
|
|
46
|
1 |
|
foreach (scandir($dir) as $file) { |
47
|
1 |
|
if (false === $this->checkFileType($file, $type)) { |
48
|
1 |
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->isReadable($dir, $file); |
52
|
|
|
|
53
|
|
|
$loaders[] = $this->createLoader($dir . DIRECTORY_SEPARATOR . $file, $type); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $loaders; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks if file extension matches type |
61
|
|
|
* |
62
|
|
|
* @param string $file |
63
|
|
|
* @param string $type |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
1 |
|
private function checkFileType($file, $type) |
68
|
|
|
{ |
69
|
1 |
|
if (in_array($file, array(".", "..")) |
70
|
1 |
|
|| $type !== strtolower(pathinfo($file, PATHINFO_EXTENSION)) |
71
|
|
|
) { |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Is file readable |
80
|
|
|
* |
81
|
|
|
* @param string $dir |
82
|
|
|
* @param string $file |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
private function isReadable($dir, $file) |
87
|
|
|
{ |
88
|
|
|
if (!is_readable($dir . DIRECTORY_SEPARATOR . $file)) { |
89
|
|
|
throw new InvalidArgumentException( |
90
|
|
|
sprintf( |
91
|
|
|
'The file "%s" is not readable.', |
92
|
|
|
$dir . DIRECTORY_SEPARATOR . $file |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Create config loaders |
102
|
|
|
* |
103
|
|
|
* @param $file |
104
|
|
|
* @param $type |
105
|
|
|
* |
106
|
|
|
* @return XmlFileLoader|YamlFileLoader |
107
|
|
|
*/ |
108
|
|
|
private function createLoader($file, $type) |
109
|
|
|
{ |
110
|
|
|
if (self::FILE_TYPE_YAML === $type) { |
111
|
|
|
return new YamlFileLoader($file); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (self::FILE_TYPE_XML === $type) { |
115
|
|
|
return new XmlFileLoader($file); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
throw new InvalidArgumentException(sprintf('The file type "%s" is not supported.', $type)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|