Skip to content

Commit 3e05c8f

Browse files
authored
fix: problem with custom view finder and numbers (#15)
1 parent 4f74767 commit 3e05c8f

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/CommonMark/View/FileViewFinder.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,59 @@
55
namespace ARKEcosystem\Foundation\CommonMark\View;
66

77
use Illuminate\View\FileViewFinder as Finder;
8+
use InvalidArgumentException;
89
use Spatie\Regex\Regex;
910

1011
final class FileViewFinder extends Finder
1112
{
1213
/**
13-
* Get an array of possible view files.
14+
* Find the given view in the list of paths.
1415
*
15-
* @param string $name
16+
* @param string $name
17+
* @param array $paths
18+
* @return string
1619
*
17-
* @return array
20+
* @throws \InvalidArgumentException
1821
*/
19-
protected function getPossibleViewFiles($name)
22+
protected function findInPaths($name, $paths)
2023
{
21-
return array_map(function ($extension) use ($name) : string {
22-
$regex = Regex::match('/\d.\d/', $name);
24+
$regex = Regex::match('/\d.\d/', $name);
2325

26+
foreach ((array) $paths as $path) {
2427
if ($regex->hasMatch()) {
25-
$name = rtrim(explode($regex->result(), $name)[0], '.');
28+
$possibleViewFiles = $this->getPossibleViewFilesConsideringNumbersWithDecimals($name, $path);
29+
} else {
30+
$possibleViewFiles = $this->getPossibleViewFiles($name);
31+
}
32+
33+
foreach ($possibleViewFiles as $file) {
34+
if ($this->files->exists($viewPath = $path.'/'.$file)) {
35+
return $viewPath;
36+
}
37+
}
38+
}
39+
40+
throw new InvalidArgumentException("View [{$name}] not found.");
41+
}
42+
43+
protected function getPossibleViewFilesConsideringNumbersWithDecimals(string $name, string $path): array
44+
{
45+
$regex = Regex::match('/\d.\d/', $name);
46+
47+
return array_map(function ($extension) use ($path, $name, $regex) : string {
48+
$number = $regex->result();
49+
$nameWithoutNumber = rtrim(explode($number, $name)[0], '.');
50+
51+
$file = str_replace('.', '/', $nameWithoutNumber).'/'.$number.'.'.$extension;
2652

27-
return str_replace('.', '/', $name).'/'.$regex->result().'.'.$extension;
53+
// Only return the file if it exists, that prevents applying this
54+
// custom path to numbers returned from custom render functions
55+
// like `src/UserInterface/Components/Number.php`
56+
if ($this->files->exists($path.'/'.$file)) {
57+
return $file;
2858
}
2959

60+
// If file doesnt exists, return the original path
3061
return str_replace('.', '/', $name).'.'.$extension;
3162
}, $this->extensions);
3263
}

0 commit comments

Comments
 (0)