breadcrumbs = new Collection; $this->callbacks = $callbacks; foreach ($before as $callback) { $callback($this); } $this->call($name, $params); foreach ($after as $callback) { $callback($this); } return $this->breadcrumbs; } /** * Call the closure to generate breadcrumbs for a page. * * @param string $name The name of the page. * @param array $params The parameters to pass to the closure. * @throws InvalidBreadcrumbException if the name is not registered. */ protected function call(string $name, array $params): void { if (! isset($this->callbacks[ $name ])) { throw new InvalidBreadcrumbException($name); } $this->callbacks[$name]($this, ...$params); } /** * Add breadcrumbs for a parent page. * * Should be called from the closure for a page, before `push()` is called. * * @param string $name The name of the parent page. * @param array ...$params The parameters to pass to the closure. * @throws InvalidBreadcrumbException */ public function parent(string $name, ...$params): void { $this->call($name, $params); } /** * Add a breadcrumb. * * Should be called from the closure for each page. May be called more than once. * * @param string $title The title of the page. * @param string|null $url The URL of the page. * @param array $data Optional associative array of additional data to pass to the view. */ public function push(string $title, string $url = null, array $data = []): void { $this->breadcrumbs->push((object) array_merge($data, compact('title', 'url'))); } }