Passed
Push — master ( 8759a1...d1e6bd )
by Caen
03:57 queued 13s
created

Features::bladePages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Facades;
6
7
use function count;
8
use Hyde\Framework\Concerns\Internal\MockableFeatures;
9
use Hyde\Framework\Services\DiscoveryService;
10
use Hyde\Hyde;
11
use Hyde\Support\Concerns\Serializable;
12
use Hyde\Support\Contracts\SerializableContract;
13
use Illuminate\Support\Str;
14
use function str_starts_with;
15
16
/**
17
 * Allows features to be enabled and disabled in a simple object-oriented manner.
18
 *
19
 * @todo Split facade logic to service/manager class.
20
 *
21
 * @see \Hyde\Framework\Testing\Feature\ConfigurableFeaturesTest
22
 *
23
 * Based entirely on Laravel Jetstream (License MIT)
24
 * @see https://jetstream.laravel.com/
25
 */
26
class Features implements SerializableContract
27
{
28
    use Serializable;
29
    use MockableFeatures;
30
31
    /**
32
     * Determine if the given specified is enabled.
33
     */
34
    public static function enabled(string $feature): bool
35
    {
36
        return static::resolveMockedInstance($feature) ?? in_array(
37
            $feature, config('hyde.features', static::getDefaultOptions())
38
        );
39
    }
40
41
    // ================================================
42
    // Determine if a given feature is enabled.
43
    // ================================================
44
45
    public static function hasHtmlPages(): bool
46
    {
47
        return static::enabled(static::htmlPages());
48
    }
49
50
    public static function hasBladePages(): bool
51
    {
52
        return static::enabled(static::bladePages());
53
    }
54
55
    public static function hasMarkdownPages(): bool
56
    {
57
        return static::enabled(static::markdownPages());
58
    }
59
60
    public static function hasMarkdownPosts(): bool
61
    {
62
        return static::enabled(static::markdownPosts());
63
    }
64
65
    public static function hasDocumentationPages(): bool
66
    {
67
        return static::enabled(static::documentationPages());
68
    }
69
70
    public static function hasDocumentationSearch(): bool
71
    {
72
        return static::enabled(static::documentationSearch())
73
            && static::hasDocumentationPages()
74
            && count(DiscoveryService::getDocumentationPageFiles()) > 0;
75
    }
76
77
    public static function hasDarkmode(): bool
78
    {
79
        return static::enabled(static::darkmode());
80
    }
81
82
    /**
83
     * Torchlight is by default enabled automatically when an API token
84
     * is set in the .env file but is disabled when running tests.
85
     */
86
    public static function hasTorchlight(): bool
87
    {
88
        return static::enabled(static::torchlight())
89
            && (config('torchlight.token') !== null)
90
            && (app('env') !== 'testing');
91
    }
92
93
    // =================================================
94
    // Configure features to be used in the config file.
95
    // =================================================
96
97
    public static function htmlPages(): string
98
    {
99
        return 'html-pages';
100
    }
101
102
    public static function bladePages(): string
103
    {
104
        return 'blade-pages';
105
    }
106
107
    public static function markdownPages(): string
108
    {
109
        return 'markdown-pages';
110
    }
111
112
    public static function markdownPosts(): string
113
    {
114
        return 'markdown-posts';
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 darkmode(): string
128
    {
129
        return 'darkmode';
130
    }
131
132
    public static function torchlight(): string
133
    {
134
        return 'torchlight';
135
    }
136
137
    // ====================================================
138
    // Dynamic features that in addition to being enabled
139
    // in the config file, require preconditions to be met.
140
    // ====================================================
141
142
    /** Can a sitemap be generated? */
143
    public static function sitemap(): bool
144
    {
145
        return static::resolveMockedInstance('sitemap') ?? Hyde::hasSiteUrl()
146
            && config('hyde.generate_sitemap', true)
147
            && extension_loaded('simplexml');
148
    }
149
150
    /** Can an RSS feed be generated? */
151
    public static function rss(): bool
152
    {
153
        return static::resolveMockedInstance('rss') ?? Hyde::hasSiteUrl()
154
            && static::hasMarkdownPosts()
155
            && config('hyde.generate_rss_feed', true)
156
            && extension_loaded('simplexml')
157
            && count(DiscoveryService::getMarkdownPostFiles()) > 0;
158
    }
159
160
    /**
161
     * Get an array representation of the features and their status.
162
     *
163
     * @return array<string, bool>
164
     *
165
     * @example ['html-pages' => true, 'markdown-pages' => false, ...]
166
     */
167
    public function toArray(): array
168
    {
169
        return collect(get_class_methods(static::class))
0 ignored issues
show
Bug introduced by
get_class_methods(static::class) of type string[] is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        return collect(/** @scrutinizer ignore-type */ get_class_methods(static::class))
Loading history...
170
            ->filter(fn (string $method): bool => str_starts_with($method, 'has'))
171
            ->mapWithKeys(fn (string $method): array => [
172
                Str::kebab(substr($method, 3)) => (static::{$method}()),
173
            ])->toArray();
174
    }
175
176
    protected static function getDefaultOptions(): array
177
    {
178
        return [
179
            // Page Modules
180
            static::htmlPages(),
181
            static::markdownPosts(),
182
            static::bladePages(),
183
            static::markdownPages(),
184
            static::documentationPages(),
185
186
            // Frontend Features
187
            static::darkmode(),
188
            static::documentationSearch(),
189
190
            // Integrations
191
            static::torchlight(),
192
        ];
193
    }
194
}
195