|
5 | 5 | namespace ARKEcosystem\Foundation\CommonMark\View; |
6 | 6 |
|
7 | 7 | use Illuminate\View\FileViewFinder as Finder; |
| 8 | +use InvalidArgumentException; |
8 | 9 | use Spatie\Regex\Regex; |
9 | 10 |
|
10 | 11 | final class FileViewFinder extends Finder |
11 | 12 | { |
12 | 13 | /** |
13 | | - * Get an array of possible view files. |
| 14 | + * Find the given view in the list of paths. |
14 | 15 | * |
15 | | - * @param string $name |
| 16 | + * @param string $name |
| 17 | + * @param array $paths |
| 18 | + * @return string |
16 | 19 | * |
17 | | - * @return array |
| 20 | + * @throws \InvalidArgumentException |
18 | 21 | */ |
19 | | - protected function getPossibleViewFiles($name) |
| 22 | + protected function findInPaths($name, $paths) |
20 | 23 | { |
21 | | - return array_map(function ($extension) use ($name) : string { |
22 | | - $regex = Regex::match('/\d.\d/', $name); |
| 24 | + $regex = Regex::match('/\d.\d/', $name); |
23 | 25 |
|
| 26 | + foreach ((array) $paths as $path) { |
24 | 27 | 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; |
26 | 52 |
|
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; |
28 | 58 | } |
29 | 59 |
|
| 60 | + // If file doesnt exists, return the original path |
30 | 61 | return str_replace('.', '/', $name).'.'.$extension; |
31 | 62 | }, $this->extensions); |
32 | 63 | } |
|
0 commit comments