3 namespace wcf\system\breadcrumb
;
5 use wcf\data\page\PageCache
;
6 use wcf\system\page\PageLocationManager
;
7 use wcf\system\SingletonFactory
;
11 * Manages breadcrumbs.
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 final class Breadcrumbs
extends SingletonFactory
implements \Countable
, \Iterator
26 * Current iterator-index
28 protected int $index = 0;
31 * Returns the list of breadcrumbs.
33 * @return Breadcrumb[]
35 public function get(): array
37 if ($this->items
=== null) {
38 $this->loadBreadcrumbs();
45 * Loads the list of breadcrumbs.
47 protected function loadBreadcrumbs(): void
50 $locations = PageLocationManager
::getInstance()->getLocations();
52 // locations are provided starting with the current location followed
53 // by all relevant ancestors, but breadcrumbs appear in the reverse order
54 $locations = \array_reverse
($locations);
56 // add the landing page as first location, unless it is already included
57 $landingPage = PageCache
::getInstance()->getLandingPage();
58 $addLandingPage = true;
59 for ($i = 0, $length = \
count($locations); $i < $length; $i++
) {
60 if ($locations[$i]['pageID'] == $landingPage->pageID
) {
61 $addLandingPage = false;
66 if ($addLandingPage) {
67 \array_unshift
($locations, [
68 'link' => $landingPage->getLink(),
69 'title' => BREADCRUMBS_HOME_USE_PAGE_TITLE ? WCF
::getLanguage()->get(PAGE_TITLE
) : $landingPage->getTitle(),
73 // ignore the last location as it represents the current page
74 \array_pop
($locations);
76 for ($i = 0, $length = \
count($locations); $i < $length; $i++
) {
77 $this->items
[] = new Breadcrumb($locations[$i]['title'], $locations[$i]['link']);
84 public function count(): int
86 if ($this->items
=== null) {
87 $this->loadBreadcrumbs();
90 return \
count($this->items
);
96 public function current(): Breadcrumb
98 return $this->items
[$this->index
];
104 public function key(): int
112 public function valid(): bool
114 return isset($this->items
[$this->index
]);
120 public function rewind(): void
128 public function next(): void