-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFileSystemPatternProvider.php
More file actions
259 lines (208 loc) · 6.75 KB
/
FileSystemPatternProvider.php
File metadata and controls
259 lines (208 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace Rareloop\Primer;
use Rareloop\Primer\Pattern;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\Finder;
use Rareloop\Primer\Contracts\DataParser;
use Rareloop\Primer\Contracts\PatternProvider;
use Rareloop\Primer\Contracts\TemplateProvider;
use Rareloop\Primer\Exceptions\PatternNotFoundException;
class FileSystemPatternProvider implements PatternProvider, TemplateProvider
{
protected $paths;
protected $fileExtension;
protected ?DataParser $dataParser;
protected $patternPaths = [];
public function __construct(array $paths, $fileExtension, ?DataParser $dataParser = null)
{
$this->paths = $paths;
$this->fileExtension = $fileExtension;
$this->dataParser = $dataParser;
}
/**
* Get a list of all the known pattern id's
*
* @return array
*/
public function allPatternIds(): array
{
if (empty($this->paths)) {
return [];
}
$finder = new Finder();
$finder->files()->in($this->paths)->name('template.' . $this->fileExtension);
return (new Collection($finder))->map(function ($file) {
return $file->getRelativePath();
})->sort()->values()->toArray();
}
/**
* Get a list of all known states for a given pattern
*
* The array must *always* contain `default` whether or not data exists
*
* @param string $id The pattern ID
* @return array
*/
protected function allPatternStates(string $id): array
{
$path = $this->getPathForPattern($id);
$supportedFormatsRegex = implode('|', $this->supportedFormats());
$finder = new Finder();
$finder->files()->in($path)->name('/data~.*\.(' . $supportedFormatsRegex . ')/')->sortByName();
$states = ['default'];
foreach ($finder as $file) {
preg_match('/data~(.*)\.(' . $supportedFormatsRegex . ')/', $file->getFilename(), $matches);
$states[] = $matches[1];
}
return $states;
}
/**
* Retrieve a Pattern
*
* @param string $id The pattern ID
* @param string $state The state name
* @return Rareloop\Primer\Pattern
*/
public function getPattern(string $id, string $state = 'default'): Pattern
{
if (empty($this->paths)) {
throw new PatternNotFoundException;
}
if (!$this->patternHasState($id, $state)) {
$state = 'default';
}
return new Pattern(
$id,
$this->getPatternStateData($id, $state),
$this->getPatternTemplate($id),
$state,
$this->allPatternStates($id)
);
}
/**
* Get the contents of the template for a given pattern
*
* @param string $id The pattern ID
* @return string
*/
public function getPatternTemplate(string $id): string
{
$path = $this->getPathForPattern($id);
return file_get_contents($path . '/template.' . $this->fileExtension);
}
protected function convertIdToPathRegex(string $id): string
{
$parts = array_map(function ($part) {
return str_replace('-', '\-', $part);
}, explode('/', $id));
$id = '/^' . implode('\/', $parts) . '\//';
return $id;
}
protected function getPathForPattern($id)
{
if (isset($this->patternPaths[$id])) {
return $this->patternPaths[$id];
}
foreach ($this->paths as $path) {
$filePath = $path . '/' . $id . '/template.' . $this->fileExtension;
if (file_exists($filePath)) {
$this->patternPaths[$id] = $path . '/' . $id . '/';
return $path . '/' . $id . '/';
}
}
throw new PatternNotFoundException();
}
/**
* Does a given pattern exist?
*
* @param string $id The pattern ID
* @return bool
*/
public function patternExists(string $id): bool
{
if (empty($this->paths)) {
return false;
}
try {
$this->getPathForPattern($id);
return true;
} catch (PatternNotFoundException $e) {
return false;
}
}
/**
* Does a given state exists for a given pattern?
*
* All valid pattern's will return true for the `default` state
*
* @param string $id The pattern ID
* @param string $state The state name
* @return bool
*/
public function patternHasState(string $id, string $state = 'default'): bool
{
if (empty($this->paths)) {
return false;
}
$path = $this->getPathForPattern($id);
if ($state === 'default') {
return true;
}
$supportedFormatsRegex = implode('|', $this->supportedFormats());
$finder = new Finder;
$finder->files()->in($path)->name('/data\~' . $state . '\.(' . $supportedFormatsRegex . ')/');
$files = array_values(iterator_to_array($finder));
return count($files) > 0;
}
protected function supportedFormats(): array
{
return array_merge(['php'], $this->dataParser ? $this->dataParser->supportedFormats() : []);
}
/**
* Get the data for the given pattern and state
*
* @param string $id [description]
* @param string $state [description]
* @return [type] [description]
*/
public function getPatternStateData(string $id, string $state = 'default'): array
{
if (!$this->patternHasState($id, $state)) {
return [];
}
$path = $this->getPathForPattern($id);
$finder = new Finder;
$finder->in($path)->files();
$glob = $state !== 'default' ? 'data~' . $state . '.*' : 'data.*';
$finder->name($glob);
$files = array_values(iterator_to_array($finder));
$file = array_shift($files);
if (!$file) {
return [];
}
if ($file->getExtension() === 'php') {
return include $file->getPath() . '/' . $file->getFilename();
}
return $this->dataParser->parse($file->getContents(), $file->getExtension());
}
protected function createFinderForPattern($id)
{
$finder = new Finder();
$finder
->files()
->in($this->paths)
->path($this->convertIdToPathRegex($id));
return $finder;
}
/**
* Get when a pattern template was last modified
*
* @param string $id The pattern ID
* @return int Unix timestamp of when last modified
*/
public function getPatternTemplateLastModified(string $id): int
{
$path = $this->getPathForPattern($id);
return filemtime($path . '/template.' . $this->fileExtension);
}
}