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