Passed
Push — master ( 0cdad4...4caa7d )
by Caen
03:31 queued 12s
created

PageCollection::addPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation;
6
7
use Hyde\Facades\Features;
8
use Hyde\Foundation\Concerns\BaseFoundationCollection;
9
use Hyde\Framework\Exceptions\FileNotFoundException;
10
use Hyde\Pages\BladePage;
11
use Hyde\Pages\Concerns\HydePage;
12
use Hyde\Pages\DocumentationPage;
13
use Hyde\Pages\HtmlPage;
14
use Hyde\Pages\MarkdownPage;
15
use Hyde\Pages\MarkdownPost;
16
use Illuminate\Support\Collection;
17
18
/**
19
 * The PageCollection contains all the instantiated pages.
20
 *
21
 * This class is stored as a singleton in the HydeKernel.
22
 * You would commonly access it via one of the facades:
23
 *
24
 * @todo We could improve this by catching exceptions and rethrowing them using a
25
 *       DiscoveryException to make it clear that the problem is with the discovery process.
26
 *
27
 * @see \Hyde\Foundation\Facades\PageCollection
28
 * @see \Hyde\Hyde::pages()
29
 */
30
final class PageCollection extends BaseFoundationCollection
31
{
32
    public function getPage(string $sourcePath): HydePage
33
    {
34
        return $this->items[$sourcePath] ?? throw new FileNotFoundException($sourcePath.' in page collection');
35
    }
36
37
    public function getPages(?string $pageClass = null): self
38
    {
39
        return ! $pageClass ? $this : $this->filter(function (HydePage $page) use ($pageClass): bool {
40
            return $page instanceof $pageClass;
41
        });
42
    }
43
44
    /**
45
     * This method adds the specified page to the page collection.
46
     * It can be used by package developers to add a page that will be compiled.
47
     *
48
     * Note that this method when used outside of this class is only intended to be used for adding on-off pages;
49
     * If you are registering multiple pages, you may instead want to register an entire custom page class,
50
     * as that will allow you to utilize the full power of the HydePHP autodiscovery.
51
     *
52
     * When using this method, take notice of the following things:
53
     * 1. Be sure to register the page before the HydeKernel boots,
54
     *    otherwise it might not be fully processed by Hyde.
55
     * 2. Note that all pages will have their routes added to the route index,
56
     *    and subsequently be compiled during the build process.
57
     */
58
    public function addPage(HydePage $page): self
59
    {
60
        $this->put($page->getSourcePath(), $page);
0 ignored issues
show
Bug introduced by
$page->getSourcePath() of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $this->put(/** @scrutinizer ignore-type */ $page->getSourcePath(), $page);
Loading history...
61
62
        return $this;
63
    }
64
65
    protected function runDiscovery(): self
66
    {
67
        if (Features::hasHtmlPages()) {
68
            $this->discoverPagesFor(HtmlPage::class);
69
        }
70
71
        if (Features::hasBladePages()) {
72
            $this->discoverPagesFor(BladePage::class);
73
        }
74
75
        if (Features::hasMarkdownPages()) {
76
            $this->discoverPagesFor(MarkdownPage::class);
77
        }
78
79
        if (Features::hasMarkdownPosts()) {
80
            $this->discoverPagesFor(MarkdownPost::class);
81
        }
82
83
        if (Features::hasDocumentationPages()) {
84
            $this->discoverPagesFor(DocumentationPage::class);
85
        }
86
87
        foreach ($this->kernel->getRegisteredPageClasses() as $pageClass) {
88
            $this->discoverPagesFor($pageClass);
89
        }
90
91
        return $this;
92
    }
93
94
    protected function discoverPagesFor(string $pageClass): void
95
    {
96
        $this->parsePagesFor($pageClass)->each(function (HydePage $page): void {
97
            $this->addPage($page);
98
        });
99
    }
100
101
    /**
102
     * @param  string<\Hyde\Pages\Concerns\HydePage>  $pageClass
103
     * @return \Illuminate\Support\Collection<\Hyde\Pages\Concerns\HydePage>
104
     */
105
    protected function parsePagesFor(string $pageClass): Collection
106
    {
107
        $collection = new Collection();
108
109
        /** @var HydePage $pageClass */
110
        foreach ($pageClass::files() as $basename) {
111
            $collection->push($pageClass::parse($basename));
112
        }
113
114
        return $collection;
115
    }
116
}
117