Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| BreadcrumbsComponent | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeBreadcrumbs | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Views\Components; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Hyde\Foundation\Facades\Routes; |
| 9 | use Illuminate\Support\Facades\View; |
| 10 | use Illuminate\Contracts\View\View as ViewContract; |
| 11 | use Illuminate\View\Component; |
| 12 | |
| 13 | use function count; |
| 14 | use function explode; |
| 15 | |
| 16 | class BreadcrumbsComponent extends Component |
| 17 | { |
| 18 | public readonly array $breadcrumbs; |
| 19 | |
| 20 | public function __construct() |
| 21 | { |
| 22 | $this->breadcrumbs = $this->makeBreadcrumbs(); |
| 23 | } |
| 24 | |
| 25 | /** @interitDoc */ |
| 26 | public function render(): ViewContract |
| 27 | { |
| 28 | return View::make('hyde::components.breadcrumbs'); |
| 29 | } |
| 30 | |
| 31 | protected function makeBreadcrumbs(): array |
| 32 | { |
| 33 | $identifier = Hyde::currentRoute()->getPage()->getIdentifier(); |
| 34 | $breadcrumbs = [(Routes::get('index')?->getLink() ?? '/') => 'Home']; |
| 35 | |
| 36 | if ($identifier === 'index') { |
| 37 | return $breadcrumbs; |
| 38 | } |
| 39 | |
| 40 | $previous = ''; |
| 41 | $fields = explode('/', $identifier); |
| 42 | foreach ($fields as $index => $basename) { |
| 43 | if ($basename === 'index') { |
| 44 | break; |
| 45 | } |
| 46 | |
| 47 | // if it's not the last basename, add index.html (since it must be a directory) otherwise add .html |
| 48 | $path = $previous.$basename.($index < count($fields) - 1 ? '/index.html' : '.html'); |
| 49 | |
| 50 | $breadcrumbs[Hyde::relativeLink($path)] = Hyde::makeTitle($basename); |
| 51 | |
| 52 | $previous .= $basename.'/'; |
| 53 | } |
| 54 | |
| 55 | return $breadcrumbs; |
| 56 | } |
| 57 | } |