-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Manager.php
194 lines (169 loc) · 4.52 KB
/
Manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
namespace Whitecube\NovaPage\Pages;
use Illuminate\Routing\Route;
use Illuminate\Http\Resources\ConditionallyLoadsAttributes;
class Manager
{
use ConditionallyLoadsAttributes,
Concerns\QueriesResources,
Concerns\ResolvesResourceCards;
/**
* The registered NovaPage Templates & Pages.
*
* @var Whitecube\NovaPage\Pages\TemplatesRepository
*/
protected $repository;
/**
* The default current Page Template
*
* @var Whitecube\NovaPage\Pages\Template
*/
protected $current;
/**
* Create the Main Service Singleton
*
* @return void
*/
public function __construct(TemplatesRepository $repository)
{
$this->repository = $repository;
}
/**
* Load the TemplateRepository with registered routes
*
* @return void
*/
public function booted()
{
$this->repository->registerRouteTemplates();
}
/**
* Register a Template into the TemplatesRepository
* and to the Laravel service container as well.
*
* @param string $type
* @param string $name
* @param string $template
* @return Whitecube\NovaPage\Pages\Template
*/
public function register($type, $name, $template)
{
app()->bind($template, function () use ($name) {
return $this->option($name);
});
return $this->repository->register($type, $name, $template);
}
/**
* Register an option Template into the TemplatesRepository
* and to the Laravel service container as well.
*
* @param string $type
* @param string $name
* @param string $template
* @return Whitecube\NovaPage\Pages\Template
*/
public function registerOption($name, $template)
{
return $this->register('option', $name, $template);
}
/**
* Load a new Page Template
*
* @param string $name
* @param string $type
* @param bool $current
* @param bool $throwOnMissing
* @return Whitecube\NovaPage\Pages\Template
*/
public function load($name, $type = 'route', $current = true, $throwOnMissing = false)
{
$template = $this->repository->load($type, $name, $throwOnMissing);
if ($type == 'route' && $current) {
$this->current = $template;
}
return $template;
}
/**
* Load Page Template for given route instance
*
* @param Illuminate\Routing\Route $route
* @param bool $current
* @param bool $throwOnMissing
* @return void
*/
public function loadForRoute(Route $route, $current = true, $throwOnMissing = false)
{
if(!$route->template()) return;
$this->load($route->getName(), 'route', $current, $throwOnMissing);
}
/**
* Get a loaded Template by its name
*
* @param string $name
* @param string $type
* @return null|Whitecube\NovaPage\Pages\Template
*/
public function find($name = null, $type = 'route')
{
if(is_null($name)) {
return $this->current;
}
return $this->repository->getLoaded($type, $name);
}
/**
* Get an option template by its name
*
* @param string $name
* @param bool $throwOnMissing
* @return mixed
*/
public function option($name, $throwOnMissing = false)
{
return $this->find($name, 'option') ??
$this->load($name, 'option', false, $throwOnMissing);
}
/**
* Get an attribute on the current Template
*
* @param string $attribute
* @return mixed
*/
public function __get($attribute)
{
if(!$this->current) {
return;
}
return $this->current->$attribute;
}
/**
* Forward a method call to the current Template
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
if(!$this->current) {
return;
}
return call_user_func_array([$this->current, $method], $arguments);
}
/**
* Mimic eloquent model method and return a fake Query builder
*
* @return Whitecube\NovaPage\Pages\Query
*/
public function newQueryWithoutScopes()
{
return new Query($this->getRepository());
}
/**
* Get the underlying template repository
* @return TemplatesRepository|Whitecube\NovaPage\Pages\TemplatesRepository
*/
public function getRepository()
{
return $this->repository;
}
}