This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 416
/
BreadcrumbsManager.php
315 lines (275 loc) · 10.7 KB
/
BreadcrumbsManager.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
namespace DaveJamesMiller\Breadcrumbs;
use DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException;
use DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException;
use DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException;
use DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Traits\Macroable;
/**
* The main Breadcrumbs singleton class, responsible for registering, generating and rendering breadcrumbs.
*/
class BreadcrumbsManager
{
use Macroable;
/**
* @var BreadcrumbsGenerator
*/
protected $generator;
/**
* @var Router
*/
protected $router;
/**
* @var ViewFactory
*/
protected $viewFactory;
/**
* @var array The registered breadcrumb-generating callbacks.
*/
protected $callbacks = [];
/**
* @var array Closures to call before generating breadcrumbs for the current page.
*/
protected $before = [];
/**
* @var array Closures to call after generating breadcrumbs for the current page.
*/
protected $after = [];
/**
* @var array|null The current route name and parameters.
*/
protected $route;
public function __construct(BreadcrumbsGenerator $generator, Router $router, ViewFactory $viewFactory)
{
$this->generator = $generator;
$this->router = $router;
$this->viewFactory = $viewFactory;
}
/**
* Register a breadcrumb-generating callback for a page.
*
* @param string $name The name of the page.
* @param callable $callback The callback, which should accept a Generator instance as the first parameter and may
* accept additional parameters.
* @return void
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException If the given name has already been
* used.
*/
public function for(string $name, callable $callback): void
{
if (isset($this->callbacks[$name])) {
throw new DuplicateBreadcrumbException($name);
}
$this->callbacks[$name] = $callback;
}
/**
* Register a breadcrumb-generating callback for a page.
*
* For backwards-compatibility with v5.0.0 and below.
*
* @param string $name The name of the page.
* @param callable $callback The callback, which should accept a Generator instance as the first parameter and may
* accept additional parameters.
* @return void
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException If the given name has already been
* used.
* @see self::for()
*/
public function register(string $name, callable $callback): void
{
$this->for($name, $callback);
}
/**
* Register a closure to call before generating breadcrumbs for the current page.
*
* For example, this can be used to always prepend the homepage without needing to manually add it to each page.
*
* @param callable $callback The callback, which should accept a Generator instance as the first and only parameter.
* @return void
*/
public function before(callable $callback): void
{
$this->before[] = $callback;
}
/**
* Register a closure to call after generating breadcrumbs for the current page.
*
* For example, this can be used to append the current page number when using pagination.
*
* @param callable $callback The callback, which should accept a Generator instance as the first and only parameter.
* @return void
*/
public function after(callable $callback): void
{
$this->after[] = $callback;
}
/**
* Check if a breadcrumb with the given name exists.
*
* If no name is given, defaults to the current route name.
*
* @param string|null $name The page name.
* @return bool Whether there is a registered callback with that name.
*/
public function exists(string $name = null): bool
{
if (is_null($name)) {
try {
[$name] = $this->getCurrentRoute();
} catch (UnnamedRouteException $e) {
return false;
}
}
return isset($this->callbacks[$name]);
}
/**
* Generate a set of breadcrumbs for a page.
*
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\Collection The generated breadcrumbs.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route
* doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names
* are) not registered.
*/
public function generate(string $name = null, ...$params): Collection
{
$origName = $name;
// Route-bound breadcrumbs
if ($name === null) {
try {
[$name, $params] = $this->getCurrentRoute();
} catch (UnnamedRouteException $e) {
if (config('breadcrumbs.unnamed-route-exception')) {
throw $e;
}
return new Collection;
}
}
// Generate breadcrumbs
try {
return $this->generator->generate($this->callbacks, $this->before, $this->after, $name, $params);
} catch (InvalidBreadcrumbException $e) {
if ($origName === null && config('breadcrumbs.missing-route-bound-breadcrumb-exception')) {
$e->setIsRouteBound();
throw $e;
}
if ($origName !== null && config('breadcrumbs.invalid-named-breadcrumb-exception')) {
throw $e;
}
return new Collection;
}
}
/**
* Render breadcrumbs for a page with the specified view.
*
* @param string $view The name of the view to render.
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\HtmlString The generated HTML.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException if no view has been set.
*/
public function view(string $view, string $name = null, ...$params): HtmlString
{
$breadcrumbs = $this->generate($name, ...$params);
// TODO: After dropping support for Laravel 5.8 and below, change this to return the view directly
// https://github.com/laravel/framework/pull/29600
$html = $this->viewFactory->make($view, compact('breadcrumbs'))->render();
return new HtmlString($html);
}
/**
* Render breadcrumbs for a page with the default view.
*
* @param string|null $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return \Illuminate\Support\HtmlString The generated HTML.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if no name is given and the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\ViewNotSetException if no view has been set.
*/
public function render(string $name = null, ...$params): HtmlString
{
$view = config('breadcrumbs.view');
if (!$view) {
throw new ViewNotSetException('Breadcrumbs view not specified (check config/breadcrumbs.php)');
}
return $this->view($view, $name, ...$params);
}
/**
* Get the last breadcrumb for the current page.
*
* Optionally pass a
*
* @return \stdClass|null The breadcrumb for the current page.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if the current route doesn't have an associated name.
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\InvalidBreadcrumbException if the name is (or any ancestor names are) not registered.
*/
public function current(): ?\stdClass
{
return $this->generate()->where('current', '!==', false)->last();
}
/**
* Get the current route name and parameters.
*
* This may be the route set manually with the setCurrentRoute() method, but normally is the route retrieved from
* the Laravel Router.
*
* #### Example
* ```php
* [$name, $params] = $this->getCurrentRoute();
* ```
*
* @return array A two-element array consisting of the route name (string) and any parameters (array).
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\UnnamedRouteException if the current route doesn't have an associated name.
*/
protected function getCurrentRoute()
{
// Manually set route
if ($this->route) {
return $this->route;
}
// Determine the current route
$route = $this->router->current();
// No current route - must be the 404 page
if ($route === null) {
return ['errors.404', []];
}
// Convert route to name
$name = $route->getName();
if ($name === null) {
throw new UnnamedRouteException($route);
}
// Get the current route parameters
$params = array_values($route->parameters());
return [$name, $params];
}
/**
* Set the current route name and parameters to use when calling render() or generate() with no parameters.
*
* @param string $name The name of the current page.
* @param mixed ...$params The parameters to pass to the closure for the current page.
* @return void
*/
public function setCurrentRoute(string $name, ...$params): void
{
$this->route = [$name, $params];
}
/**
* Clear the previously set route name and parameters to use when calling render() or generate() with no parameters.
*
* Next time it will revert to the default behaviour of using the current route from Laravel.
*
* @return void
*/
public function clearCurrentRoute(): void
{
$this->route = null;
}
}