1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Metadata\Driver; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File locator service for locating metadata files for use in creating EntityMetadata instances. |
7
|
|
|
* |
8
|
|
|
* @author Jacob Bare <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
final class FileLocator implements FileLocatorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Directories to search in. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $directories = [ |
18
|
|
|
'model' => [], |
19
|
|
|
'mixin' => [], |
20
|
|
|
'embed' => [], |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor. |
25
|
|
|
* |
26
|
|
|
* @param string|array $modelDirs |
27
|
|
|
* @param string|array $mixinDirs |
28
|
|
|
* @param string|array $embedDirs |
29
|
|
|
*/ |
30
|
|
|
public function __construct($modelDirs, $mixinDirs = [], $embedDirs = []) |
31
|
|
|
{ |
32
|
|
|
$this->directories['model'] = (Array) $modelDirs; |
33
|
|
|
$this->directories['mixin'] = (Array) $mixinDirs; |
34
|
|
|
$this->directories['embed'] = (Array) $embedDirs; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets the directories to search in. |
39
|
|
|
* |
40
|
|
|
* @param string $type The directory type, either model or mixin. |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
protected function getDirectories($type) |
44
|
|
|
{ |
45
|
|
|
return $this->directories[$type]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function findFileForEmbed($embedName, $extension) |
52
|
|
|
{ |
53
|
|
|
return $this->findFile('embed', $embedName, $extension); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function findFileForMixin($mixinName, $extension) |
60
|
|
|
{ |
61
|
|
|
return $this->findFile('mixin', $mixinName, $extension); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function findFileForType($modelType, $extension) |
68
|
|
|
{ |
69
|
|
|
return $this->findFile('model', $modelType, $extension); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Finds a file based on a directory type (model or mixin) and a key. |
74
|
|
|
* |
75
|
|
|
* @param string $dirType |
76
|
|
|
* @param string $key |
77
|
|
|
* @param string $extension |
78
|
|
|
* @return string|null |
79
|
|
|
*/ |
80
|
|
|
protected function findFile($dirType, $key, $extension) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->getDirectories($dirType) as $dir) { |
83
|
|
|
$path = sprintf('%s/%s', $dir, $this->getFilename($key, $extension)); |
84
|
|
|
if (file_exists($path)) { |
85
|
|
|
return $path; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
|
|
public function findAllTypes($extension) |
95
|
|
|
{ |
96
|
|
|
$types = []; |
97
|
|
|
$extension = sprintf('.%s', $extension); |
98
|
|
|
|
99
|
|
|
foreach ($this->getDirectories('model') as $dir) { |
100
|
|
|
$iterator = new \RecursiveIteratorIterator( |
101
|
|
|
new \RecursiveDirectoryIterator($dir), |
102
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY |
103
|
|
|
); |
104
|
|
|
foreach ($iterator as $file) { |
105
|
|
|
if (($fileName = $file->getBasename($extension)) == $file->getBasename()) { |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
$types[] = str_replace($extension, '', $fileName); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $types; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the filename for a metadata entity or mixin. |
116
|
|
|
* |
117
|
|
|
* @param string $key |
118
|
|
|
* @param string $extension |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function getFilename($key, $extension) |
122
|
|
|
{ |
123
|
|
|
return sprintf('%s.%s', $key, $extension); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|