|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Concerns\Viewable; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsParent; |
|
6
|
|
|
use Thinktomorrow\Chief\Sets\Set; |
|
7
|
|
|
|
|
8
|
|
|
class ViewPath |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var ViewableContract */ |
|
11
|
|
|
private $viewable; |
|
12
|
|
|
|
|
13
|
|
|
/** @var ActsAsParent */ |
|
14
|
|
|
private $parent; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $viewBasePath; |
|
18
|
|
|
|
|
19
|
12 |
|
private function __construct(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null) |
|
20
|
|
|
{ |
|
21
|
12 |
|
$this->viewable = $viewable; |
|
22
|
12 |
|
$this->parent = $parent; |
|
23
|
12 |
|
$this->viewBasePath = $viewBasePath; |
|
24
|
12 |
|
} |
|
25
|
|
|
|
|
26
|
12 |
|
public static function make(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null) |
|
27
|
|
|
{ |
|
28
|
12 |
|
return new static($viewable, $parent, $viewBasePath); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
* @throws NotFoundView |
|
34
|
|
|
*/ |
|
35
|
12 |
|
public function get(): string |
|
36
|
|
|
{ |
|
37
|
12 |
|
$basePath = $this->viewBasePath ? $this->viewBasePath . '.' : ''; |
|
38
|
12 |
|
$guessedParentViewName = $this->parent ? $this->parent->viewKey() : ''; |
|
|
|
|
|
|
39
|
12 |
|
$guessedViewName = $this->viewable->viewKey(); |
|
40
|
|
|
|
|
41
|
|
|
$viewPaths = [ |
|
42
|
12 |
|
$basePath.$guessedParentViewName.'.'.$guessedViewName, |
|
43
|
12 |
|
$basePath.$guessedViewName, |
|
44
|
12 |
|
$basePath.$guessedViewName.'.show', |
|
45
|
12 |
|
$basePath.'show', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
// In case the collection set is made out of pages, we'll also allow to use the |
|
49
|
|
|
// generic collection page view for these sets as well as a fallback view |
|
50
|
12 |
|
if ($this->viewable instanceof Set && $this->viewable->first() instanceof ViewableContract) { |
|
51
|
6 |
|
$viewPaths[] = $basePath.$guessedParentViewName.'.'.$this->viewable->first()->viewKey(); |
|
52
|
6 |
|
$viewPaths[] = $basePath.$this->viewable->first()->viewKey(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
12 |
|
foreach ($viewPaths as $viewPath) { |
|
56
|
12 |
|
if (! view()->exists($viewPath)) { |
|
57
|
12 |
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $viewPath; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
12 |
|
throw new NotFoundView('Viewfile not found for ['.get_class($this->viewable).']. Try adding one with path: '.$basePath.$guessedViewName); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|