1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JumpGate\ViewResolution\Resolvers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
6
|
|
|
use Illuminate\View\Factory; |
7
|
|
|
use Illuminate\View\View; |
8
|
|
|
use JumpGate\ViewResolution\Models\View as ViewModel; |
9
|
|
|
|
10
|
|
|
class Path |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \JumpGate\ViewResolution\Models\View |
14
|
|
|
*/ |
15
|
|
|
public $viewModel; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \JumpGate\ViewResolution\Resolvers\Path |
19
|
|
|
*/ |
20
|
|
|
public $path; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \JumpGate\ViewResolution\Resolvers\Layout|\Illuminate\View\View |
24
|
|
|
*/ |
25
|
|
|
public $layout; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Illuminate\Routing\Router |
29
|
|
|
*/ |
30
|
|
|
protected $route; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Illuminate\View\Factory |
34
|
|
|
*/ |
35
|
|
|
protected $view; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param \Illuminate\Routing\Router $route |
39
|
|
|
* @param \Illuminate\View\Factory $view |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Router $route, Factory $view) |
42
|
|
|
{ |
43
|
|
|
$this->route = $route; |
44
|
|
|
$this->view = $view; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set up the needed details for the view. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\View\View $layout |
51
|
|
|
* @param null|string $view |
52
|
|
|
* |
53
|
|
|
* @return \Illuminate\View\View |
54
|
|
|
*/ |
55
|
|
|
public function setUp(View $layout, $view = null) |
56
|
|
|
{ |
57
|
|
|
$this->layout = $layout; |
58
|
|
|
$this->setPath($view); |
59
|
|
|
$this->setContent(); |
60
|
|
|
|
61
|
|
|
return $this->layout; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a valid view path save it. |
66
|
|
|
* |
67
|
|
|
* @param $view |
68
|
|
|
*/ |
69
|
|
|
protected function setPath($view) |
70
|
|
|
{ |
71
|
|
|
$this->viewModel = new ViewModel([], $this->layout->getName()); |
|
|
|
|
72
|
|
|
|
73
|
|
|
if ($view == null) { |
74
|
|
|
$view = $this->findView(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->viewModel->view = $view; |
78
|
|
|
|
79
|
|
|
viewResolver()->collectDetails($this->viewModel); |
80
|
|
|
|
81
|
|
|
$this->path = $view; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Put the found view inside the layout. |
86
|
|
|
*/ |
87
|
|
|
protected function setContent() |
88
|
|
|
{ |
89
|
|
|
if (stripos($this->path, 'missingmethod') === false && $this->view->exists($this->path)) { |
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$this->layout->content = $this->view->make($this->path); |
|
|
|
|
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
$this->layout->content = null; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Try to figure out a view based on the called action. |
100
|
|
|
* |
101
|
|
|
* @param $layout |
102
|
|
|
* @param $parameters |
103
|
|
|
* |
104
|
|
|
* @return \Illuminate\View\View |
105
|
|
|
*/ |
106
|
|
|
public function missingMethod($layout, $parameters) |
107
|
|
|
{ |
108
|
|
|
$view = $this->findView(); |
109
|
|
|
|
110
|
|
|
if (count($parameters) == 1) { |
111
|
|
|
$view = str_ireplace('missingMethod', $parameters[0], $view); |
112
|
|
|
} elseif ($parameters[0] == null && $parameters[1] == null) { |
113
|
|
|
$view = str_ireplace('missingMethod', 'index', $view); |
114
|
|
|
} else { |
115
|
|
|
$view = implode('.', $parameters); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this->setUp($layout, $view); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Find a view based on the available details. |
123
|
|
|
* |
124
|
|
|
* This will look in the config and the route details to |
125
|
|
|
* find a sensible place a view may be. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
protected function findView() |
130
|
|
|
{ |
131
|
|
|
// Get the overall route name (SomeController@someMethod) |
132
|
|
|
// Break it up into it's component parts |
133
|
|
|
$route = $this->route->currentRouteAction(); |
134
|
|
|
$routeParts = explode('@', $route); |
135
|
|
|
|
136
|
|
|
if (count(array_filter($routeParts)) > 0) { |
137
|
|
|
$this->viewModel = new ViewModel($routeParts, $this->layout->getName()); |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Check for a configured view route. |
140
|
|
|
if (! is_null($configView = $this->viewModel->checkConfig())) { |
141
|
|
|
$this->viewModel->type = 'config'; |
142
|
|
|
|
143
|
|
|
return $configView; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->viewModel->type = 'auto'; |
147
|
|
|
|
148
|
|
|
return $this->viewModel->getView(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: