Passed
Branch master (4048f5)
by Caen
03:01
created

HydeKernel::touch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework;
4
5
use Composer\InstalledVersions;
6
use Hyde\Framework\Concerns\JsonSerializesArrayable;
7
use Hyde\Framework\Contracts\HydeKernelContract;
8
use Hyde\Framework\Contracts\RouteContract;
9
use Hyde\Framework\Foundation\Filesystem;
10
use Hyde\Framework\Foundation\Hyperlinks;
11
use Hyde\Framework\Helpers\Features;
12
use Illuminate\Contracts\Support\Arrayable;
13
use Illuminate\Support\Facades\View;
14
use Illuminate\Support\Str;
15
use Illuminate\Support\Traits\Macroable;
16
17
/**
18
 * Encapsulates a HydePHP project, providing helpful methods for interacting with it.
19
 *
20
 * @see \Hyde\Framework\Hyde for the facade commonly used to access this class.
21
 *
22
 * @author  Caen De Silva <[email protected]>
23
 * @copyright 2022 Caen De Silva
24
 * @license MIT License
25
 *
26
 * @link https://hydephp.com/
27
 */
28
class HydeKernel implements HydeKernelContract, Arrayable, \JsonSerializable
29
{
30
    use Macroable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Hyde\Framework\HydeKernel.
Loading history...
31
    use JsonSerializesArrayable;
32
33
    protected string $basePath;
34
    protected Filesystem $filesystem;
35
    protected Hyperlinks $hyperlinks;
36
    protected PageCollection $pages;
37
    protected RouteCollection $routes;
38
39
    protected bool $booted = false;
40
41
    public function __construct(?string $basePath = null)
42
    {
43
        $this->setBasePath($basePath ?? getcwd());
44
        $this->filesystem = new Filesystem($this);
45
        $this->hyperlinks = new Hyperlinks($this);
46
    }
47
48
    protected function bootKernel(): void
49
    {
50
        $this->booted = true;
51
        $this->pages = PageCollection::boot();
52
        $this->routes = RouteCollection::boot($this);
53
    }
54
55
    public static function boot(): void
56
    {
57
        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

57
        static::getInstance()->/** @scrutinizer ignore-call */ bootKernel();
Loading history...
58
    }
59
60
    public static function getInstance(): HydeKernelContract
61
    {
62
        return app(HydeKernelContract::class);
63
    }
64
65
    public static function version(): string
66
    {
67
        return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased';
68
    }
69
70
    public function getBasePath(): string
71
    {
72
        return $this->basePath;
73
    }
74
75
    public function setBasePath(string $basePath)
76
    {
77
        $this->basePath = rtrim($basePath, '/\\');
78
    }
79
80
    public function features(): Features
81
    {
82
        return new Features;
83
    }
84
85
    public function hasFeature(string $feature): bool
86
    {
87
        return Features::enabled($feature);
88
    }
89
90
    public function currentPage(): string
91
    {
92
        return View::shared('currentPage', '');
93
    }
94
95
    public function currentRoute(): ?RouteContract
96
    {
97
        return View::shared('currentRoute');
98
    }
99
100
    public function pages(): PageCollection
101
    {
102
        if (! $this->booted) {
103
            $this->bootKernel();
104
        }
105
106
        return $this->pages;
107
    }
108
109
    public function routes(): RouteCollection
110
    {
111
        if (! $this->booted) {
112
            $this->bootKernel();
113
        }
114
115
        return $this->routes;
116
    }
117
118
    public function makeTitle(string $slug): string
119
    {
120
        $alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but'];
121
122
        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

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