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
|
11 |
|
private function __construct(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null) |
20
|
|
|
{ |
21
|
11 |
|
$this->viewable = $viewable; |
22
|
11 |
|
$this->parent = $parent; |
23
|
11 |
|
$this->viewBasePath = $viewBasePath; |
24
|
11 |
|
} |
25
|
|
|
|
26
|
11 |
|
public static function make(ViewableContract $viewable, ActsAsParent $parent = null, string $viewBasePath = null) |
27
|
|
|
{ |
28
|
11 |
|
return new static($viewable, $parent, $viewBasePath); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
* @throws NotFoundView |
34
|
|
|
*/ |
35
|
11 |
|
public function get(): string |
36
|
|
|
{ |
37
|
11 |
|
$basePath = $this->viewBasePath ? $this->viewBasePath . '.' : ''; |
38
|
11 |
|
$guessedParentViewName = $this->parent ? $this->parent->viewKey() : ''; |
|
|
|
|
39
|
11 |
|
$guessedViewName = $this->viewable->viewKey(); |
40
|
|
|
|
41
|
|
|
$viewPaths = [ |
42
|
11 |
|
$basePath.$guessedParentViewName.'.'.$guessedViewName, |
43
|
11 |
|
$basePath.$guessedViewName, |
44
|
11 |
|
$basePath.$guessedViewName.'.show', |
45
|
11 |
|
$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
|
11 |
|
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
|
11 |
|
foreach ($viewPaths as $viewPath) { |
56
|
11 |
|
if (! view()->exists($viewPath)) { |
57
|
11 |
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $viewPath; |
61
|
|
|
} |
62
|
|
|
|
63
|
11 |
|
if (! view()->exists($basePath.'show')) { |
64
|
11 |
|
throw new NotFoundView('Viewfile not found for ['.get_class($this->viewable).']. Make sure the view ['.$basePath.$guessedViewName.'] or the default view ['.$basePath.'show] exists.'); |
65
|
|
|
} |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|