Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
BreadcrumbsComponent
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeBreadcrumbs
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Views\Components;
6
7use Hyde\Hyde;
8use Hyde\Foundation\Facades\Routes;
9use Illuminate\Support\Facades\View;
10use Illuminate\Contracts\View\View as ViewContract;
11use Illuminate\View\Component;
12
13use function count;
14use function explode;
15
16class 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}