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; |
|
|
|
|
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(); |
|
|
|
|
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( |
|
|
|
|
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
|
|
|
|