-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFeatures.php
195 lines (163 loc) · 5.15 KB
/
Features.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace Hyde\Facades;
use function count;
use Hyde\Framework\Concerns\Internal\MockableFeatures;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Hyde;
use Hyde\Support\Concerns\JsonSerializesArrayable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Str;
use JsonSerializable;
use function str_starts_with;
/**
* Allows features to be enabled and disabled in a simple object-oriented manner.
*
* @todo Split facade logic to service/manager class.
*
* @see \Hyde\Framework\Testing\Feature\ConfigurableFeaturesTest
*
* Based entirely on Laravel Jetstream (License MIT)
* @see https://jetstream.laravel.com/
*/
class Features implements Arrayable, JsonSerializable
{
use JsonSerializesArrayable;
use MockableFeatures;
/**
* Determine if the given specified is enabled.
*/
public static function enabled(string $feature): bool
{
return static::resolveMockedInstance($feature) ?? in_array($feature, config('hyde.features', [
// Page Modules
static::htmlPages(),
static::markdownPosts(),
static::bladePages(),
static::markdownPages(),
static::documentationPages(),
// static::dataCollections(),
// Frontend Features
static::darkmode(),
static::documentationSearch(),
// Integrations
static::torchlight(),
]));
}
// ================================================
// Determine if a given feature is enabled.
// ================================================
public static function hasHtmlPages(): bool
{
return static::enabled(static::htmlPages());
}
public static function hasBladePages(): bool
{
return static::enabled(static::bladePages());
}
public static function hasMarkdownPages(): bool
{
return static::enabled(static::markdownPages());
}
public static function hasMarkdownPosts(): bool
{
return static::enabled(static::markdownPosts());
}
public static function hasDocumentationPages(): bool
{
return static::enabled(static::documentationPages());
}
public static function hasDataCollections(): bool
{
return static::enabled(static::dataCollections());
}
public static function hasDocumentationSearch(): bool
{
return static::enabled(static::documentationSearch())
&& static::hasDocumentationPages()
&& count(DiscoveryService::getDocumentationPageFiles()) > 0;
}
public static function hasDarkmode(): bool
{
return static::enabled(static::darkmode());
}
/**
* Torchlight is by default enabled automatically when an API token
* is set in the .env file but is disabled when running tests.
*/
public static function hasTorchlight(): bool
{
return static::enabled(static::torchlight())
&& (config('torchlight.token') !== null)
&& (app('env') !== 'testing');
}
// ================================================
// Enable a given feature to be used in the config.
// ================================================
public static function htmlPages(): string
{
return 'html-pages';
}
public static function bladePages(): string
{
return 'blade-pages';
}
public static function markdownPages(): string
{
return 'markdown-pages';
}
public static function markdownPosts(): string
{
return 'markdown-posts';
}
public static function documentationPages(): string
{
return 'documentation-pages';
}
public static function documentationSearch(): string
{
return 'documentation-search';
}
public static function dataCollections(): string
{
return 'data-collections';
}
public static function darkmode(): string
{
return 'darkmode';
}
public static function torchlight(): string
{
return 'torchlight';
}
// ================================================
// Dynamic features.
// ================================================
/** Can a sitemap be generated? */
public static function sitemap(): bool
{
return static::resolveMockedInstance('sitemap') ?? Hyde::hasSiteUrl()
&& config('site.generate_sitemap', true)
&& extension_loaded('simplexml');
}
/** Can an RSS feed be generated? */
public static function rss(): bool
{
return static::resolveMockedInstance('rss') ?? Hyde::hasSiteUrl()
&& static::hasMarkdownPosts()
&& config('hyde.generate_rss_feed', true)
&& extension_loaded('simplexml')
&& count(DiscoveryService::getMarkdownPostFiles()) > 0;
}
/** @inheritDoc */
public function toArray(): array
{
$array = [];
foreach (get_class_methods(static::class) as $method) {
if (str_starts_with((string) $method, 'has')) {
$array[Str::kebab(substr((string) $method, 3))] = static::{$method}();
}
}
return $array;
}
}