Passed
Push — master ( d3f7f1...90b48b )
by Caen
03:27 queued 12s
created

HydeKernel   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 51
c 9
b 0
f 0
dl 0
loc 183
rs 9.68
wmc 34

31 Methods

Rating   Name   Duplication   Size   Complexity  
A unlink() 0 3 1
A getBladePagePath() 0 3 1
A hasSiteUrl() 0 3 1
A routes() 0 7 2
A getBasePath() 0 3 1
A getMarkdownPagePath() 0 3 1
A currentPage() 0 3 1
A relativeLink() 0 3 1
A touch() 0 3 1
A makeTitle() 0 8 1
A version() 0 3 2
A currentRoute() 0 3 1
A formatHtmlPath() 0 3 1
A pathToRelative() 0 3 1
A url() 0 3 1
A pages() 0 7 2
A getModelSourcePath() 0 3 1
A hasFeature() 0 3 1
A vendorPath() 0 3 1
A getMarkdownPostPath() 0 3 1
A features() 0 3 1
A __construct() 0 5 1
A copy() 0 3 1
A bootKernel() 0 5 1
A boot() 0 3 1
A setBasePath() 0 3 1
A image() 0 3 1
A path() 0 3 1
A getSiteOutputPath() 0 3 1
A getInstance() 0 3 1
A getDocumentationPagePath() 0 3 1
1
<?php
2
3
namespace Hyde\Framework;
4
5
use Composer\InstalledVersions;
6
use Hyde\Framework\Contracts\HydeKernelContract;
7
use Hyde\Framework\Contracts\RouteContract;
8
use Hyde\Framework\Foundation\Filesystem;
9
use Hyde\Framework\Foundation\Hyperlinks;
10
use Hyde\Framework\Helpers\Features;
11
use Illuminate\Support\Facades\View;
12
use Illuminate\Support\Str;
13
use Illuminate\Support\Traits\Macroable;
14
15
/**
16
 * Encapsulates a HydePHP project, providing helpful methods for interacting with it.
17
 *
18
 * @see \Hyde\Framework\Hyde for the facade commonly used to access this class.
19
 *
20
 * @author  Caen De Silva <[email protected]>
21
 * @copyright 2022 Caen De Silva
22
 * @license MIT License
23
 *
24
 * @link https://hydephp.com/
25
 */
26
class HydeKernel implements HydeKernelContract
27
{
28
    use Macroable;
29
30
    protected string $basePath;
31
    protected Filesystem $filesystem;
32
    protected Hyperlinks $hyperlinks;
33
    protected PageCollection $pages;
34
    protected RouteCollection $routes;
35
36
    protected bool $booted = false;
37
38
    public function __construct(?string $basePath = null)
39
    {
40
        $this->setBasePath($basePath ?? getcwd());
41
        $this->filesystem = new Filesystem($this);
42
        $this->hyperlinks = new Hyperlinks($this);
43
    }
44
45
    protected function bootKernel(): void
46
    {
47
        $this->booted = true;
48
        $this->pages = PageCollection::boot();
49
        $this->routes = RouteCollection::boot($this);
50
    }
51
52
    public static function boot(): void
53
    {
54
        static::getInstance()->bootKernel();
0 ignored issues
show
Bug introduced by
The method bootKernel() does not exist on Hyde\Framework\Contracts\HydeKernelContract. Since it exists in all sub-types, consider adding an abstract or default implementation to Hyde\Framework\Contracts\HydeKernelContract. ( Ignorable by Annotation )

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

54
        static::getInstance()->/** @scrutinizer ignore-call */ bootKernel();
Loading history...
55
    }
56
57
    public static function getInstance(): HydeKernelContract
58
    {
59
        return app(HydeKernelContract::class);
60
    }
61
62
    public static function version(): string
63
    {
64
        return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased';
65
    }
66
67
    public function getBasePath(): string
68
    {
69
        return $this->basePath;
70
    }
71
72
    public function setBasePath(string $basePath)
73
    {
74
        $this->basePath = rtrim($basePath, '/\\');
75
    }
76
77
    public function features(): Features
78
    {
79
        return new Features;
80
    }
81
82
    public function hasFeature(string $feature): bool
83
    {
84
        return Features::enabled($feature);
85
    }
86
87
    public function currentPage(): string
88
    {
89
        return View::shared('currentPage', '');
90
    }
91
92
    public function currentRoute(): ?RouteContract
93
    {
94
        return View::shared('currentRoute');
95
    }
96
97
    public function pages(): PageCollection
98
    {
99
        if (! $this->booted) {
100
            $this->bootKernel();
101
        }
102
103
        return $this->pages;
104
    }
105
106
    public function routes(): RouteCollection
107
    {
108
        if (! $this->booted) {
109
            $this->bootKernel();
110
        }
111
112
        return $this->routes;
113
    }
114
115
    public function makeTitle(string $slug): string
116
    {
117
        $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but'];
118
119
        return ucfirst(str_ireplace(
0 ignored issues
show
Bug introduced by
It seems like str_ireplace($alwaysLowe...t\Str::headline($slug)) can also be of type array; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

119
        return ucfirst(/** @scrutinizer ignore-type */ str_ireplace(
Loading history...
120
            $alwaysLowercase,
121
            $alwaysLowercase,
122
            Str::headline($slug)
123
        ));
124
    }
125
126
    public function formatHtmlPath(string $destination): string
127
    {
128
        return $this->hyperlinks->formatHtmlPath($destination);
129
    }
130
131
    public function relativeLink(string $destination): string
132
    {
133
        return $this->hyperlinks->relativeLink($destination);
134
    }
135
136
    public function image(string $name, bool $preferQualifiedUrl = false): string
137
    {
138
        return $this->hyperlinks->image($name, $preferQualifiedUrl);
139
    }
140
141
    public function hasSiteUrl(): bool
142
    {
143
        return $this->hyperlinks->hasSiteUrl();
144
    }
145
146
    public function url(string $path = '', ?string $default = null): string
147
    {
148
        return $this->hyperlinks->url($path, $default);
149
    }
150
151
    public function path(string $path = ''): string
152
    {
153
        return $this->filesystem->path($path);
154
    }
155
156
    public function vendorPath(string $path = ''): string
157
    {
158
        return $this->filesystem->vendorPath($path);
159
    }
160
161
    public function copy(string $from, string $to): bool
162
    {
163
        return $this->filesystem->copy($from, $to);
164
    }
165
166
    public function touch(string|array $path): bool
167
    {
168
        return $this->filesystem->touch($path);
169
    }
170
171
    public function unlink(string|array $path): bool
172
    {
173
        return $this->filesystem->unlink($path);
174
    }
175
176
    public function getModelSourcePath(string $model, string $path = ''): string
177
    {
178
        return $this->filesystem->getModelSourcePath($model, $path);
179
    }
180
181
    public function getBladePagePath(string $path = ''): string
182
    {
183
        return $this->filesystem->getBladePagePath($path);
184
    }
185
186
    public function getMarkdownPagePath(string $path = ''): string
187
    {
188
        return $this->filesystem->getMarkdownPagePath($path);
189
    }
190
191
    public function getMarkdownPostPath(string $path = ''): string
192
    {
193
        return $this->filesystem->getMarkdownPostPath($path);
194
    }
195
196
    public function getDocumentationPagePath(string $path = ''): string
197
    {
198
        return $this->filesystem->getDocumentationPagePath($path);
199
    }
200
201
    public function getSiteOutputPath(string $path = ''): string
202
    {
203
        return $this->filesystem->getSiteOutputPath($path);
204
    }
205
206
    public function pathToRelative(string $path): string
207
    {
208
        return $this->filesystem->pathToRelative($path);
209
    }
210
}
211