Passed
Push — master ( 511c7b...f04524 )
by Caen
03:20 queued 14s
created

Features::hasDataCollections()   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($feature, config('hyde.features', [
37
            // Page Modules
38
            static::htmlPages(),
39
            static::markdownPosts(),
40
            static::bladePages(),
41
            static::markdownPages(),
42
            static::documentationPages(),
43
            // static::dataCollections(),
44
45
            // Frontend Features
46
            static::darkmode(),
47
            static::documentationSearch(),
48
49
            // Integrations
50
            static::torchlight(),
51
        ]));
52
    }
53
54
    // ================================================
55
    // Determine if a given feature is enabled.
56
    // ================================================
57
58
    public static function hasHtmlPages(): bool
59
    {
60
        return static::enabled(static::htmlPages());
61
    }
62
63
    public static function hasBladePages(): bool
64
    {
65
        return static::enabled(static::bladePages());
66
    }
67
68
    public static function hasMarkdownPages(): bool
69
    {
70
        return static::enabled(static::markdownPages());
71
    }
72
73
    public static function hasMarkdownPosts(): bool
74
    {
75
        return static::enabled(static::markdownPosts());
76
    }
77
78
    public static function hasDocumentationPages(): bool
79
    {
80
        return static::enabled(static::documentationPages());
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
    // Configure features to be used in the config file.
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 darkmode(): string
141
    {
142
        return 'darkmode';
143
    }
144
145
    public static function torchlight(): string
146
    {
147
        return 'torchlight';
148
    }
149
150
    // ====================================================
151
    // Dynamic features that in addition to being enabled
152
    // in the config file, require preconditions to be met.
153
    // ====================================================
154
155
    /** Can a sitemap be generated? */
156
    public static function sitemap(): bool
157
    {
158
        return static::resolveMockedInstance('sitemap') ?? Hyde::hasSiteUrl()
159
            && config('site.generate_sitemap', true)
160
            && extension_loaded('simplexml');
161
    }
162
163
    /** Can an RSS feed be generated? */
164
    public static function rss(): bool
165
    {
166
        return static::resolveMockedInstance('rss') ?? Hyde::hasSiteUrl()
167
            && static::hasMarkdownPosts()
168
            && config('hyde.generate_rss_feed', true)
169
            && extension_loaded('simplexml')
170
            && count(DiscoveryService::getMarkdownPostFiles()) > 0;
171
    }
172
173
    /**
174
     * Get an array representation of the features and their status.
175
     *
176
     * @return array<string, bool>
177
     *
178
     * @example ['html-pages' => true, 'markdown-pages' => false, ...]
179
     */
180
    public function toArray(): array
181
    {
182
        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

182
        return collect(/** @scrutinizer ignore-type */ get_class_methods(static::class))
Loading history...
183
            ->filter(fn (string $method): bool => str_starts_with($method, 'has'))
184
            ->mapWithKeys(fn (string $method): array => [
185
                Str::kebab(substr($method, 3)) => (static::{$method}()),
186
            ])->toArray();
187
    }
188
}
189