Passed
Push — master ( 64fca5...69e1b4 )
by Caen
03:06 queued 12s
created

Features::hasBladePages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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