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
|
|
|
const EXTENSION_JSON = 'json'; |
24
|
|
|
const EXTENSION_XML = 'xml'; |
25
|
|
|
const EXTENSION_YAML = 'yml'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string[] |
29
|
|
|
*/ |
30
|
|
|
private $directories; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var TypeParserInterface |
34
|
|
|
*/ |
35
|
|
|
private $typeParser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ClassMetadataLoaderInterface|null |
39
|
|
|
*/ |
40
|
|
|
private $loader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $initialized = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string|string[] $directories |
49
|
|
|
* @param TypeParserInterface|null $typeParser |
50
|
|
|
*/ |
51
|
204 |
|
public function __construct($directories, TypeParserInterface $typeParser = null) |
52
|
|
|
{ |
53
|
204 |
|
$directories = is_array($directories) ? $directories : [$directories]; |
54
|
|
|
|
55
|
204 |
|
foreach ($directories as $directory) { |
56
|
204 |
|
if (!is_dir($directory)) { |
57
|
44 |
|
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $directory)); |
58
|
|
|
} |
59
|
|
|
|
60
|
204 |
|
if (!is_readable($directory)) { |
61
|
102 |
|
throw new \InvalidArgumentException(sprintf('The directory "%s" is not readable.', $directory)); |
62
|
|
|
} |
63
|
102 |
|
} |
64
|
|
|
|
65
|
204 |
|
$this->directories = $directories; |
66
|
204 |
|
$this->typeParser = $typeParser ?: new TypeParser(); |
67
|
204 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
156 |
|
public function loadClassMetadata(ClassMetadataInterface $classMetadata) |
73
|
|
|
{ |
74
|
156 |
|
if (!$this->initialized) { |
75
|
156 |
|
$loaders = []; |
76
|
|
|
|
77
|
156 |
|
foreach ($this->directories as $directory) { |
78
|
156 |
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); |
79
|
|
|
|
80
|
156 |
|
foreach ($iterator as $file) { |
81
|
156 |
|
if ($file->isDir()) { |
82
|
156 |
|
continue; |
83
|
|
|
} |
84
|
|
|
|
85
|
156 |
|
switch ($file->getExtension()) { |
86
|
156 |
|
case self::EXTENSION_JSON: |
87
|
156 |
|
$loaders[] = new JsonClassMetadataLoader($file->getRealPath(), $this->typeParser); |
88
|
156 |
|
break; |
89
|
|
|
|
90
|
156 |
|
case self::EXTENSION_XML: |
91
|
156 |
|
$loaders[] = new XmlClassMetadataLoader($file->getRealPath(), $this->typeParser); |
92
|
156 |
|
break; |
93
|
|
|
|
94
|
156 |
|
case self::EXTENSION_YAML: |
95
|
156 |
|
$loaders[] = new YamlClassMetadataLoader($file->getRealPath(), $this->typeParser); |
96
|
156 |
|
break; |
97
|
78 |
|
} |
98
|
78 |
|
} |
99
|
78 |
|
} |
100
|
|
|
|
101
|
156 |
|
if (!empty($loaders)) { |
102
|
156 |
|
$this->loader = count($loaders) > 1 ? new ChainClassMetadataLoader($loaders) : array_shift($loaders); |
103
|
78 |
|
} |
104
|
|
|
|
105
|
156 |
|
$this->initialized = true; |
106
|
78 |
|
} |
107
|
|
|
|
108
|
156 |
|
return $this->loader !== null && $this->loader->loadClassMetadata($classMetadata); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|