Passed
Push — master ( 1e39e8...4523d8 )
by Caen
03:01 queued 12s
created

AbstractPage::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Contracts;
4
5
use Hyde\Framework\Actions\SourceFileParser;
6
use Hyde\Framework\Concerns\CanBeInNavigation;
7
use Hyde\Framework\Concerns\HasPageMetadata;
8
use Hyde\Framework\Models\FrontMatter;
9
use Hyde\Framework\Models\Route;
10
use Hyde\Framework\Services\DiscoveryService;
11
use Illuminate\Support\Collection;
12
13
/**
14
 * To ensure compatibility with the Hyde Framework, all Page Models should extend this class.
15
 *
16
 * Markdown-based Pages can extend the AbstractMarkdownPage class to get relevant helpers.
17
 *
18
 * To learn about what the methods do, see the PHPDocs in the PageContract.
19
 *
20
 * @see \Hyde\Framework\Contracts\PageContract
21
 * @see \Hyde\Framework\Contracts\AbstractMarkdownPage
22
 * @test \Hyde\Framework\Testing\Feature\AbstractPageTest
23
 */
24
abstract class AbstractPage implements PageContract, CompilableContract
25
{
26
    use HasPageMetadata;
0 ignored issues
show
Bug introduced by
The trait Hyde\Framework\Concerns\HasPageMetadata requires the property $title which is not provided by Hyde\Framework\Contracts\AbstractPage.
Loading history...
27
    use CanBeInNavigation;
0 ignored issues
show
Bug introduced by
The trait Hyde\Framework\Concerns\CanBeInNavigation requires the property $title which is not provided by Hyde\Framework\Contracts\AbstractPage.
Loading history...
28
29
    public static string $sourceDirectory;
30
    public static string $outputDirectory;
31
    public static string $fileExtension;
32
    public static string $template;
33
34
    public string $identifier;
35
    public FrontMatter $matter;
36
37
    /** @inheritDoc */
38
    final public static function getSourceDirectory(): string
39
    {
40
        return unslash(static::$sourceDirectory);
41
    }
42
43
    /** @inheritDoc */
44
    final public static function getOutputDirectory(): string
45
    {
46
        return unslash(static::$outputDirectory);
47
    }
48
49
    /** @inheritDoc */
50
    final public static function getFileExtension(): string
51
    {
52
        return '.'.ltrim(static::$fileExtension, '.');
53
    }
54
55
    /** @inheritDoc */
56
    public static function parse(string $slug): PageContract
57
    {
58
        return (new SourceFileParser(static::class, $slug))->get();
59
    }
60
61
    /** @inheritDoc */
62
    public static function files(): array|false
63
    {
64
        return DiscoveryService::getSourceFileListForModel(static::class);
65
    }
66
67
    /** @inheritDoc */
68
    public static function all(): Collection
69
    {
70
        $collection = new Collection();
71
72
        foreach (static::files() as $basename) {
73
            $collection->push(static::parse($basename));
74
        }
75
76
        return $collection;
77
    }
78
79
    /** @inheritDoc */
80
    public static function qualifyBasename(string $basename): string
81
    {
82
        return static::getSourceDirectory().'/'.unslash($basename).static::getFileExtension();
83
    }
84
85
    /** @inheritDoc */
86
    public static function getOutputLocation(string $basename): string
87
    {
88
        // Using the trim function we ensure we don't have a leading slash when the output directory is the root directory.
89
        return trim(
90
            static::getOutputDirectory().'/'.unslash($basename),
91
            '/'
92
        ).'.html';
93
    }
94
95
    public function __construct(string $identifier = '', FrontMatter|array $matter = [])
96
    {
97
        $this->identifier = $identifier;
98
        $this->matter = $matter instanceof FrontMatter ? $matter : new FrontMatter($matter);
0 ignored issues
show
introduced by
$matter is never a sub-type of Hyde\Framework\Models\FrontMatter.
Loading history...
99
    }
100
101
    /** @interitDoc */
102
    public function __get(string $name)
103
    {
104
        return $this->matter->get($name);
105
    }
106
107
    /** @inheritDoc */
108
    public function __set(string $name, $value): void
109
    {
110
        $this->matter->set($name, $value);
111
    }
112
113
    /** @inheritDoc */
114
    public function matter(string $key = null, mixed $default = null): mixed
115
    {
116
        return $this->matter->get($key, $default);
117
    }
118
119
    /** @inheritDoc */
120
    public function getSourcePath(): string
121
    {
122
        return static::qualifyBasename($this->identifier);
123
    }
124
125
    /** @inheritDoc */
126
    public function getOutputPath(): string
127
    {
128
        return static::getCurrentPagePath().'.html';
0 ignored issues
show
Bug Best Practice introduced by
The method Hyde\Framework\Contracts...e::getCurrentPagePath() is not static, but was called statically. ( Ignorable by Annotation )

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

128
        return static::/** @scrutinizer ignore-call */ getCurrentPagePath().'.html';
Loading history...
129
    }
130
131
    /** @inheritDoc */
132
    public function getCurrentPagePath(): string
133
    {
134
        return trim(static::getOutputDirectory().'/'.$this->identifier, '/');
135
    }
136
137
    /** @inheritDoc */
138
    public function getRoute(): Route
139
    {
140
        return new Route($this);
141
    }
142
143
    /** @inheritDoc */
144
    public function htmlTitle(?string $title = null): string
145
    {
146
        $pageTitle = $title ?? $this->title ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The property title does not exist on Hyde\Framework\Contracts\AbstractPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
147
148
        return $pageTitle
149
            ? config('site.name', 'HydePHP').' - '.$pageTitle
150
            : config('site.name', 'HydePHP');
151
    }
152
153
    /** @inheritDoc */
154
    public function getBladeView(): string
155
    {
156
        return static::$template;
157
    }
158
159
    /** @inheritDoc */
160
    abstract public function compile(): string;
161
}
162