Passed
Push — master ( 529dea...6e1056 )
by Caen
03:45 queued 12s
created

DiscoveryService::getDocumentationPageFiles()   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\Services;
4
5
use Hyde\Framework\Contracts\AbstractPage;
6
use Hyde\Framework\Hyde;
7
use Hyde\Framework\Models\Pages\BladePage;
8
use Hyde\Framework\Models\Pages\DocumentationPage;
9
use Hyde\Framework\Models\Pages\MarkdownPage;
10
use Hyde\Framework\Models\Pages\MarkdownPost;
11
12
/**
13
 * The core service that powers all HydePHP file auto-discovery.
14
 *
15
 * Contains service methods to return helpful collections of arrays and lists,
16
 * and provides helper methods for source file auto-discovery used in the site
17
 * building process to determine where files are located and how to parse them.
18
 *
19
 * The CollectionService was in v0.53.0 merged into this class.
20
 */
21
class DiscoveryService
22
{
23
    public static function getParserClassForModel(string $model): string
24
    {
25
        /** @var AbstractPage $model */
26
        return $model::getParserClass();
27
    }
28
29
    /**
30
     * Create and get a constructed instance of a Model's Parser class.
31
     *
32
     * @param  string  $model  Class constant of the Model to get the Parser for.
33
     * @param  string  $slug  The slug of the source file to parse.
34
     *
35
     * @example getParserForModel(MarkdownPost::class, 'hello-world')
36
     *
37
     * @return object The constructed Parser instance.
38
     */
39
    public static function getParserInstanceForModel(string $model, string $slug): object
40
    {
41
        /** @var AbstractPage $model */
42
        return new $model::$parserClass($slug);
43
    }
44
45
    /**
46
     * Get the file extension for a models source files.
47
     */
48
    public static function getFileExtensionForModelFiles(string $model): string
49
    {
50
        /** @var AbstractPage $model */
51
        return $model::getFileExtension();
52
    }
53
54
    /**
55
     * Get the source directory path of a model.
56
     */
57
    public static function getFilePathForModelClassFiles(string $model): string
58
    {
59
        /** @var AbstractPage $model */
60
        return $model::getSourceDirectory();
61
    }
62
63
    /**
64
     * Create a filepath that can be opened in the browser from a terminal.
65
     *
66
     * @param  string  $filepath
67
     * @return string
68
     */
69
    public static function createClickableFilepath(string $filepath): string
70
    {
71
        if (realpath($filepath) === false) {
72
            return $filepath;
73
        }
74
75
        return 'file://'.str_replace(
76
            '\\',
77
            '/',
78
            realpath($filepath)
79
        );
80
    }
81
82
    /**
83
     *  Get all the Markdown files in the _docs directory.
84
     */
85
    public static function getDocumentationPageFiles(): array|false
86
    {
87
        return self::getSourceFileListForModel(DocumentationPage::class);
88
    }
89
90
    /**
91
     * Supply a model::class constant and get a list of all the existing source file base names.
92
     *
93
     * @param  string  $model
94
     * @return array|false array on success, false if the class was not found
95
     *
96
     * @example DiscoveryService::getSourceFileListForModel(BladePage::class)
97
     */
98
    public static function getSourceFileListForModel(string $model): array|false
99
    {
100
        if (! class_exists($model) || ! is_subclass_of($model, AbstractPage::class)) {
101
            return false;
102
        }
103
104
        // Scan the source directory, and directories therein, for files that match the model's file extension.
105
106
        $files = [];
107
        foreach (glob(Hyde::path($model::qualifyBasename('{*,**/*}')), GLOB_BRACE) as $filepath) {
108
            if (! str_starts_with(basename($filepath), '_')) {
109
                $files[] = self::formatSlugForModel($model, $filepath);
110
            }
111
        }
112
113
        return $files;
114
    }
115
116
    public static function formatSlugForModel(string $model, string $filepath): string
117
    {
118
        /** @var AbstractPage $model */
119
        $slug = str_replace(Hyde::path($model::$sourceDirectory), '', $filepath);
120
121
        if (str_ends_with($slug, $model::$fileExtension)) {
122
            $slug = substr($slug, 0, -strlen($model::$fileExtension));
123
        }
124
125
        $slug = unslash($slug);
126
127
        return $slug;
128
    }
129
130
    /**
131
     * @return array
132
     *               Get all the Markdown files in the _pages directory.
133
     */
134
    public static function getMarkdownPageFiles(): array|false
135
    {
136
        return self::getSourceFileListForModel(MarkdownPage::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getSourceFi...es\MarkdownPage::class) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
137
    }
138
139
    /**
140
     * Get all the Media asset file paths.
141
     * Returns a full file path, unlike the other get*List methods.
142
     */
143
    public static function getMediaAssetFiles(): array
144
    {
145
        return glob(Hyde::path('_media/*.{'.str_replace(
146
                ' ',
147
                '',
148
                config('hyde.media_extensions', 'png,svg,jpg,jpeg,gif,ico,css,js')
149
            ).'}'), GLOB_BRACE);
150
    }
151
152
    /**
153
     * @return array
154
     *               Get all the Blade files in the _pages directory.
155
     */
156
    public static function getBladePageFiles(): array|false
157
    {
158
        return self::getSourceFileListForModel(BladePage::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getSourceFi...Pages\BladePage::class) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
159
    }
160
161
    /**
162
     * @return array
163
     *               Get all the Markdown files in the _posts directory.
164
     */
165
    public static function getMarkdownPostFiles(): array|false
166
    {
167
        return self::getSourceFileListForModel(MarkdownPost::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getSourceFi...es\MarkdownPost::class) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
168
    }
169
}
170