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 static HydeKernelContract $instance; |
34
|
|
|
|
35
|
|
|
protected string $basePath; |
36
|
|
|
protected Filesystem $filesystem; |
37
|
|
|
protected Hyperlinks $hyperlinks; |
38
|
|
|
protected PageCollection $pages; |
39
|
|
|
protected RouteCollection $routes; |
40
|
|
|
|
41
|
|
|
protected bool $booted = false; |
42
|
|
|
|
43
|
|
|
public function __construct(?string $basePath = null) |
44
|
|
|
{ |
45
|
|
|
$this->setBasePath($basePath ?? getcwd()); |
46
|
|
|
$this->filesystem = new Filesystem($this); |
47
|
|
|
$this->hyperlinks = new Hyperlinks($this); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function boot(): void |
51
|
|
|
{ |
52
|
|
|
$this->booted = true; |
53
|
|
|
$this->pages = PageCollection::boot(); |
54
|
|
|
$this->routes = RouteCollection::boot($this); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function setInstance(HydeKernelContract $instance): void |
58
|
|
|
{ |
59
|
|
|
static::$instance = $instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function getInstance(): HydeKernelContract |
63
|
|
|
{ |
64
|
|
|
return static::$instance; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function version(): string |
68
|
|
|
{ |
69
|
|
|
return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getBasePath(): string |
73
|
|
|
{ |
74
|
|
|
return $this->basePath; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function setBasePath(string $basePath) |
78
|
|
|
{ |
79
|
|
|
$this->basePath = rtrim($basePath, '/\\'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function features(): Features |
83
|
|
|
{ |
84
|
|
|
return new Features; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function hasFeature(string $feature): bool |
88
|
|
|
{ |
89
|
|
|
return Features::enabled($feature); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function currentPage(): string |
93
|
|
|
{ |
94
|
|
|
return View::shared('currentPage', ''); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function currentRoute(): ?RouteContract |
98
|
|
|
{ |
99
|
|
|
return View::shared('currentRoute'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function pages(): PageCollection |
103
|
|
|
{ |
104
|
|
|
if (! $this->booted) { |
105
|
|
|
$this->boot(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->pages; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function routes(): RouteCollection |
112
|
|
|
{ |
113
|
|
|
if (! $this->booted) { |
114
|
|
|
$this->boot(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->routes; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function makeTitle(string $slug): string |
121
|
|
|
{ |
122
|
|
|
$alwaysLowercase = ['a', 'an', 'the', 'in', 'on', 'by', 'with', 'of', 'and', 'or', 'but']; |
123
|
|
|
|
124
|
|
|
return ucfirst(str_ireplace( |
|
|
|
|
125
|
|
|
$alwaysLowercase, |
126
|
|
|
$alwaysLowercase, |
127
|
|
|
Str::headline($slug) |
128
|
|
|
)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function formatHtmlPath(string $destination): string |
132
|
|
|
{ |
133
|
|
|
return $this->hyperlinks->formatHtmlPath($destination); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function relativeLink(string $destination): string |
137
|
|
|
{ |
138
|
|
|
return $this->hyperlinks->relativeLink($destination); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function image(string $name, bool $preferQualifiedUrl = false): string |
142
|
|
|
{ |
143
|
|
|
return $this->hyperlinks->image($name, $preferQualifiedUrl); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function hasSiteUrl(): bool |
147
|
|
|
{ |
148
|
|
|
return $this->hyperlinks->hasSiteUrl(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function url(string $path = '', ?string $default = null): string |
152
|
|
|
{ |
153
|
|
|
return $this->hyperlinks->url($path, $default); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function path(string $path = ''): string |
157
|
|
|
{ |
158
|
|
|
return $this->filesystem->path($path); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function vendorPath(string $path = ''): string |
162
|
|
|
{ |
163
|
|
|
return $this->filesystem->vendorPath($path); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function copy(string $from, string $to): bool |
167
|
|
|
{ |
168
|
|
|
return $this->filesystem->copy($from, $to); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function touch(string|array $path): bool |
172
|
|
|
{ |
173
|
|
|
return $this->filesystem->touch($path); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function unlink(string|array $path): bool |
177
|
|
|
{ |
178
|
|
|
return $this->filesystem->unlink($path); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function getModelSourcePath(string $model, string $path = ''): string |
182
|
|
|
{ |
183
|
|
|
return $this->filesystem->getModelSourcePath($model, $path); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getBladePagePath(string $path = ''): string |
187
|
|
|
{ |
188
|
|
|
return $this->filesystem->getBladePagePath($path); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getMarkdownPagePath(string $path = ''): string |
192
|
|
|
{ |
193
|
|
|
return $this->filesystem->getMarkdownPagePath($path); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getMarkdownPostPath(string $path = ''): string |
197
|
|
|
{ |
198
|
|
|
return $this->filesystem->getMarkdownPostPath($path); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getDocumentationPagePath(string $path = ''): string |
202
|
|
|
{ |
203
|
|
|
return $this->filesystem->getDocumentationPagePath($path); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function getSiteOutputPath(string $path = ''): string |
207
|
|
|
{ |
208
|
|
|
return $this->filesystem->getSiteOutputPath($path); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function pathToRelative(string $path): string |
212
|
|
|
{ |
213
|
|
|
return $this->filesystem->pathToRelative($path); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** @inheritDoc */ |
217
|
|
|
public function toArray() |
218
|
|
|
{ |
219
|
|
|
return [ |
220
|
|
|
'basePath' => $this->basePath, |
221
|
|
|
'features' => $this->features(), |
222
|
|
|
'pages' => $this->pages(), |
223
|
|
|
'routes' => $this->routes(), |
224
|
|
|
]; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|