Skip to content

Commit e65e066

Browse files
committed
Refactor the way that a pattern path is found
1 parent 50cf2c6 commit e65e066

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/FileSystemPatternProvider.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(array $paths, $fileExtension, DataParser $dataParser
2929
*
3030
* @return array
3131
*/
32-
public function allPatternIds() : array
32+
public function allPatternIds(): array
3333
{
3434
if (empty($this->paths)) {
3535
return [];
@@ -51,7 +51,7 @@ public function allPatternIds() : array
5151
* @param string $id The pattern ID
5252
* @return array
5353
*/
54-
protected function allPatternStates(string $id) : array
54+
protected function allPatternStates(string $id): array
5555
{
5656
$path = $this->getPathForPattern($id);
5757

@@ -77,7 +77,7 @@ protected function allPatternStates(string $id) : array
7777
* @param string $state The state name
7878
* @return Rareloop\Primer\Pattern
7979
*/
80-
public function getPattern(string $id, string $state = 'default') : Pattern
80+
public function getPattern(string $id, string $state = 'default'): Pattern
8181
{
8282
if (empty($this->paths)) {
8383
throw new PatternNotFoundException;
@@ -102,14 +102,14 @@ public function getPattern(string $id, string $state = 'default') : Pattern
102102
* @param string $id The pattern ID
103103
* @return string
104104
*/
105-
public function getPatternTemplate(string $id) : string
105+
public function getPatternTemplate(string $id): string
106106
{
107107
$path = $this->getPathForPattern($id);
108108

109109
return file_get_contents($path . '/template.' . $this->fileExtension);
110110
}
111111

112-
protected function convertIdToPathRegex(string $id) : string
112+
protected function convertIdToPathRegex(string $id): string
113113
{
114114
$parts = array_map(function ($part) {
115115
return str_replace('-', '\-', $part);
@@ -126,17 +126,16 @@ protected function getPathForPattern($id)
126126
return $this->patternPaths[$id];
127127
}
128128

129-
$finder = $this->createFinderForPattern($id)->name('template.' . $this->fileExtension);
129+
foreach ($this->paths as $path) {
130+
$filePath = $path . '/' . $id . '/template.' . $this->fileExtension;
130131

131-
$files = array_values(iterator_to_array($finder));
132-
133-
if (count($files) === 0) {
134-
throw new PatternNotFoundException;
132+
if (file_exists($filePath)) {
133+
$this->patternPaths[$id] = $path . '/' . $id . '/';
134+
return $path . '/' . $id . '/';
135+
}
135136
}
136137

137-
$this->patternPaths[$id] = $files[0]->getPath();
138-
139-
return $this->patternPaths[$id];
138+
throw new PatternNotFoundException();
140139
}
141140

142141
/**
@@ -145,7 +144,7 @@ protected function getPathForPattern($id)
145144
* @param string $id The pattern ID
146145
* @return bool
147146
*/
148-
public function patternExists(string $id) : bool
147+
public function patternExists(string $id): bool
149148
{
150149
if (empty($this->paths)) {
151150
return false;
@@ -169,7 +168,7 @@ public function patternExists(string $id) : bool
169168
* @param string $state The state name
170169
* @return bool
171170
*/
172-
public function patternHasState(string $id, string $state = 'default') : bool
171+
public function patternHasState(string $id, string $state = 'default'): bool
173172
{
174173
if (empty($this->paths)) {
175174
return false;
@@ -191,7 +190,7 @@ public function patternHasState(string $id, string $state = 'default') : bool
191190
return count($files) > 0;
192191
}
193192

194-
protected function supportedFormats() : array
193+
protected function supportedFormats(): array
195194
{
196195
return array_merge(['php'], $this->dataParser ? $this->dataParser->supportedFormats() : []);
197196
}
@@ -203,7 +202,7 @@ protected function supportedFormats() : array
203202
* @param string $state [description]
204203
* @return [type] [description]
205204
*/
206-
public function getPatternStateData(string $id, string $state = 'default') : array
205+
public function getPatternStateData(string $id, string $state = 'default'): array
207206
{
208207
if (!$this->patternHasState($id, $state)) {
209208
return [];
@@ -250,7 +249,7 @@ protected function createFinderForPattern($id)
250249
* @param string $id The pattern ID
251250
* @return int Unix timestamp of when last modified
252251
*/
253-
public function getPatternTemplateLastModified(string $id) : int
252+
public function getPatternTemplateLastModified(string $id): int
254253
{
255254
$path = $this->getPathForPattern($id);
256255

0 commit comments

Comments
 (0)