1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Facades; |
6
|
|
|
|
7
|
|
|
use Hyde\Hyde; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Hyde\Pages\MarkdownPost; |
10
|
|
|
use Hyde\Pages\DocumentationPage; |
11
|
|
|
use Hyde\Enums\Feature; |
12
|
|
|
use Hyde\Support\Concerns\Serializable; |
13
|
|
|
use Hyde\Support\Contracts\SerializableContract; |
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
|
16
|
|
|
use function collect; |
17
|
|
|
use function array_filter; |
18
|
|
|
use function extension_loaded; |
19
|
|
|
use function in_array; |
20
|
|
|
use function count; |
21
|
|
|
use function app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Allows features to be enabled and disabled in a simple object-oriented manner. |
25
|
|
|
*/ |
26
|
|
|
class Features implements SerializableContract |
27
|
|
|
{ |
28
|
|
|
use Serializable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The features that are enabled. |
32
|
|
|
* |
33
|
|
|
* @var array<\Hyde\Enums\Feature> |
34
|
|
|
*/ |
35
|
|
|
protected array $features = []; |
36
|
|
|
|
37
|
|
|
public function __construct() |
38
|
|
|
{ |
39
|
|
|
$this->features = Config::getArray('hyde.features', Feature::cases()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the given specified is enabled. |
44
|
|
|
*/ |
45
|
|
|
public static function has(Feature $feature): bool |
46
|
|
|
{ |
47
|
|
|
return in_array($feature, Hyde::features()->features); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get all enabled features. |
52
|
|
|
* |
53
|
|
|
* @return array<string> |
54
|
|
|
*/ |
55
|
|
|
public static function enabled(): array |
56
|
|
|
{ |
57
|
|
|
return Arr::map(Hyde::features()->features, fn (Feature $feature): string => Str::kebab($feature->name)); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function hasHtmlPages(): bool |
61
|
|
|
{ |
62
|
|
|
return static::has(Feature::HtmlPages); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function hasBladePages(): bool |
66
|
|
|
{ |
67
|
|
|
return static::has(Feature::BladePages); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function hasMarkdownPages(): bool |
71
|
|
|
{ |
72
|
|
|
return static::has(Feature::MarkdownPages); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function hasMarkdownPosts(): bool |
76
|
|
|
{ |
77
|
|
|
return static::has(Feature::MarkdownPosts); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public static function hasDocumentationPages(): bool |
81
|
|
|
{ |
82
|
|
|
return static::has(Feature::DocumentationPages); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function hasDarkmode(): bool |
86
|
|
|
{ |
87
|
|
|
return static::has(Feature::Darkmode); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function hasThemeToggleButtons(): bool |
91
|
|
|
{ |
92
|
|
|
return static::hasDarkmode() && Config::getBool('hyde.theme_toggle_buttons', true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Can a sitemap be generated? |
97
|
|
|
*/ |
98
|
|
|
public static function hasSitemap(): bool |
99
|
|
|
{ |
100
|
|
|
return Hyde::hasSiteUrl() |
|
|
|
|
101
|
|
|
&& Config::getBool('hyde.generate_sitemap', true) |
102
|
|
|
&& extension_loaded('simplexml'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Can an RSS feed be generated? |
107
|
|
|
*/ |
108
|
|
|
public static function hasRss(): bool |
109
|
|
|
{ |
110
|
|
|
return Hyde::hasSiteUrl() |
111
|
|
|
&& static::hasMarkdownPosts() |
112
|
|
|
&& Config::getBool('hyde.rss.enabled', true) |
113
|
|
|
&& extension_loaded('simplexml') |
114
|
|
|
&& count(MarkdownPost::files()) > 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Should documentation search be enabled? |
119
|
|
|
*/ |
120
|
|
|
public static function hasDocumentationSearch(): bool |
121
|
|
|
{ |
122
|
|
|
return static::has(Feature::DocumentationSearch) |
123
|
|
|
&& static::hasDocumentationPages() |
124
|
|
|
&& count(DocumentationPage::files()) > 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Torchlight is by default enabled automatically when an API token |
129
|
|
|
* is set in the `.env` file but is disabled when running tests. |
130
|
|
|
*/ |
131
|
|
|
public static function hasTorchlight(): bool |
132
|
|
|
{ |
133
|
|
|
return static::has(Feature::Torchlight) |
134
|
|
|
&& (Config::getNullableString('torchlight.token') !== null) |
135
|
|
|
&& (app('env') !== 'testing'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get an array representation of the features and their status. |
140
|
|
|
* |
141
|
|
|
* @return array<string, bool> |
142
|
|
|
* |
143
|
|
|
* @example ['html-pages' => true, 'markdown-pages' => false, ...] |
144
|
|
|
*/ |
145
|
|
|
public function toArray(): array |
146
|
|
|
{ |
147
|
|
|
return Arr::mapWithKeys(Feature::cases(), fn (Feature $feature): array => [ |
148
|
|
|
Str::kebab($feature->name) => static::has($feature), |
|
|
|
|
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @internal This method is not covered by the backward compatibility promise. */ |
153
|
|
|
public static function mock(string $feature, ?bool $enabled = null): void |
154
|
|
|
{ |
155
|
|
|
if ($enabled === true) { |
156
|
|
|
// Add the feature if it doesn't already exist. |
157
|
|
|
Hyde::features()->features[] = collect(Feature::cases())->firstOrFail(fn (Feature $search): bool => Str::kebab($search->name) === $feature); |
|
|
|
|
158
|
|
|
} else { |
159
|
|
|
// Remove the feature if it exists. |
160
|
|
|
Hyde::features()->features = array_filter(Hyde::features()->features, fn (Feature $search): bool => Str::kebab($search->name) !== $feature); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|