1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Mapping\Loader; |
13
|
|
|
|
14
|
|
|
use Ivory\Serializer\Mapping\ClassMetadataInterface; |
15
|
|
|
use Ivory\Serializer\Type\Parser\TypeParser; |
16
|
|
|
use Ivory\Serializer\Type\Parser\TypeParserInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author GeLo <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DirectoryClassMetadataLoader implements ClassMetadataLoaderInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
private $directories; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TypeParserInterface |
30
|
|
|
*/ |
31
|
|
|
private $typeParser; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ClassMetadataLoaderInterface|null |
35
|
|
|
*/ |
36
|
|
|
private $loader; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var bool |
40
|
|
|
*/ |
41
|
|
|
private $initialized = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|string[] $directories |
45
|
|
|
* @param TypeParserInterface|null $typeParser |
46
|
|
|
*/ |
47
|
204 |
|
public function __construct($directories, TypeParserInterface $typeParser = null) |
48
|
|
|
{ |
49
|
204 |
|
$directories = is_array($directories) ? $directories : [$directories]; |
50
|
|
|
|
51
|
204 |
|
foreach ($directories as $directory) { |
52
|
204 |
|
if (!is_dir($directory)) { |
53
|
44 |
|
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $directory)); |
54
|
|
|
} |
55
|
|
|
|
56
|
204 |
|
if (!is_readable($directory)) { |
57
|
102 |
|
throw new \InvalidArgumentException(sprintf('The directory "%s" is not readable.', $directory)); |
58
|
|
|
} |
59
|
102 |
|
} |
60
|
|
|
|
61
|
204 |
|
$this->directories = $directories; |
62
|
204 |
|
$this->typeParser = $typeParser ?: new TypeParser(); |
63
|
204 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
156 |
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
69
|
|
|
{ |
70
|
156 |
|
if (!$this->initialized) { |
71
|
|
|
$extensions = [ |
72
|
156 |
|
FileClassMetadataLoader::EXTENSION_JSON, |
73
|
156 |
|
FileClassMetadataLoader::EXTENSION_XML, |
74
|
156 |
|
FileClassMetadataLoader::EXTENSION_YAML, |
75
|
78 |
|
]; |
76
|
|
|
|
77
|
156 |
|
$loaders = []; |
78
|
|
|
|
79
|
156 |
|
foreach ($this->directories as $directory) { |
80
|
156 |
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); |
81
|
|
|
|
82
|
156 |
|
foreach ($iterator as $file) { |
83
|
156 |
|
if ($file->isDir()) { |
84
|
156 |
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
156 |
|
$path = $file->getRealPath(); |
88
|
|
|
|
89
|
156 |
|
if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $extensions, true)) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
156 |
|
$loaders[] = new FileClassMetadataLoader($path, $this->typeParser); |
94
|
78 |
|
} |
95
|
78 |
|
} |
96
|
|
|
|
97
|
156 |
|
if (!empty($loaders)) { |
98
|
156 |
|
$this->loader = count($loaders) > 1 ? new ChainClassMetadataLoader($loaders) : array_shift($loaders); |
99
|
78 |
|
} |
100
|
|
|
|
101
|
156 |
|
$this->initialized = true; |
102
|
78 |
|
} |
103
|
|
|
|
104
|
156 |
|
return $this->loader !== null && $this->loader->loadClassMetadata($classMetadata); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|