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\FileCollection; |
10
|
|
|
use Hyde\Framework\Foundation\Filesystem; |
11
|
|
|
use Hyde\Framework\Foundation\Hyperlinks; |
12
|
|
|
use Hyde\Framework\Foundation\PageCollection; |
13
|
|
|
use Hyde\Framework\Foundation\RouteCollection; |
14
|
|
|
use Hyde\Framework\Helpers\Features; |
15
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
16
|
|
|
use Illuminate\Support\Facades\View; |
17
|
|
|
use Illuminate\Support\Traits\Macroable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Encapsulates a HydePHP project, providing helpful methods for interacting with it. |
21
|
|
|
* |
22
|
|
|
* @see \Hyde\Framework\Hyde for the facade commonly used to access this class. |
23
|
|
|
* |
24
|
|
|
* @author Caen De Silva <[email protected]> |
25
|
|
|
* @copyright 2022 Caen De Silva |
26
|
|
|
* @license MIT License |
27
|
|
|
* |
28
|
|
|
* @link https://hydephp.com/ |
29
|
|
|
*/ |
30
|
|
|
class HydeKernel implements HydeKernelContract, Arrayable, \JsonSerializable |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
use Foundation\Concerns\ImplementsStringHelpers; |
33
|
|
|
use Foundation\Concerns\ForwardsHyperlinks; |
34
|
|
|
use Foundation\Concerns\ForwardsFilesystem; |
35
|
|
|
|
36
|
|
|
use JsonSerializesArrayable; |
37
|
|
|
use Macroable; |
|
|
|
|
38
|
|
|
|
39
|
|
|
protected static HydeKernel $instance; |
40
|
|
|
|
41
|
|
|
protected string $basePath; |
42
|
|
|
|
43
|
|
|
protected Filesystem $filesystem; |
44
|
|
|
protected Hyperlinks $hyperlinks; |
45
|
|
|
|
46
|
|
|
protected FileCollection $files; |
47
|
|
|
protected PageCollection $pages; |
48
|
|
|
protected RouteCollection $routes; |
49
|
|
|
|
50
|
|
|
protected bool $booted = false; |
51
|
|
|
|
52
|
|
|
public function __construct(?string $basePath = null) |
53
|
|
|
{ |
54
|
|
|
$this->setBasePath($basePath ?? getcwd()); |
55
|
|
|
$this->filesystem = new Filesystem($this); |
56
|
|
|
$this->hyperlinks = new Hyperlinks($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function boot(): void |
60
|
|
|
{ |
61
|
|
|
$this->booted = true; |
62
|
|
|
|
63
|
|
|
$this->files = FileCollection::boot($this); |
64
|
|
|
$this->pages = PageCollection::boot($this); |
65
|
|
|
$this->routes = RouteCollection::boot($this); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public static function setInstance(HydeKernel $instance): void |
69
|
|
|
{ |
70
|
|
|
static::$instance = $instance; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function getInstance(): HydeKernel |
74
|
|
|
{ |
75
|
|
|
return static::$instance; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function version(): string |
79
|
|
|
{ |
80
|
|
|
return InstalledVersions::getPrettyVersion('hyde/framework') ?: 'unreleased'; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getBasePath(): string |
84
|
|
|
{ |
85
|
|
|
return $this->basePath; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setBasePath(string $basePath): void |
89
|
|
|
{ |
90
|
|
|
$this->basePath = rtrim($basePath, '/\\'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function features(): Features |
94
|
|
|
{ |
95
|
|
|
return new Features; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function hasFeature(string $feature): bool |
99
|
|
|
{ |
100
|
|
|
return Features::enabled($feature); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function currentPage(): string |
104
|
|
|
{ |
105
|
|
|
return View::shared('currentPage', ''); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function currentRoute(): ?RouteContract |
109
|
|
|
{ |
110
|
|
|
return View::shared('currentRoute'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function files(): FileCollection |
114
|
|
|
{ |
115
|
|
|
$this->needsToBeBooted(); |
116
|
|
|
|
117
|
|
|
return $this->files; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function pages(): PageCollection |
121
|
|
|
{ |
122
|
|
|
$this->needsToBeBooted(); |
123
|
|
|
|
124
|
|
|
return $this->pages; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function routes(): RouteCollection |
128
|
|
|
{ |
129
|
|
|
$this->needsToBeBooted(); |
130
|
|
|
|
131
|
|
|
return $this->routes; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
* |
137
|
|
|
* @return array{basePath: string, features: \Hyde\Framework\Helpers\Features, pages: \Hyde\Framework\Foundation\PageCollection, routes: \Hyde\Framework\Foundation\RouteCollection} |
138
|
|
|
*/ |
139
|
|
|
public function toArray(): array |
140
|
|
|
{ |
141
|
|
|
return [ |
142
|
|
|
'basePath' => $this->basePath, |
143
|
|
|
'features' => $this->features(), |
144
|
|
|
'files' => $this->files(), |
145
|
|
|
'pages' => $this->pages(), |
146
|
|
|
'routes' => $this->routes(), |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function needsToBeBooted(): void |
151
|
|
|
{ |
152
|
|
|
if (! $this->booted) { |
153
|
|
|
$this->boot(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.