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

DiscoveryService::getMarkdownPageFiles()   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\Contracts\PageParserContract;
7
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
8
use Hyde\Framework\Hyde;
9
use Hyde\Framework\Models\Pages\BladePage;
10
use Hyde\Framework\Models\Pages\DocumentationPage;
11
use Hyde\Framework\Models\Pages\MarkdownPage;
12
use Hyde\Framework\Models\Pages\MarkdownPost;
13
14
/**
15
 * The core service that powers all HydePHP file auto-discovery.
16
 *
17
 * Contains service methods to return helpful collections of arrays and lists,
18
 * and provides helper methods for source file auto-discovery used in the site
19
 * building process to determine where files are located and how to parse them.
20
 *
21
 * The CollectionService was in v0.53.0 merged into this class.
22
 */
23
class DiscoveryService
24
{
25
    public static function getParserClassForModel(string $model): string
26
    {
27
        /** @var AbstractPage $model */
28
        return $model::getParserClass();
29
    }
30
31
    /**
32
     * Create and get a constructed instance of a Model's Parser class.
33
     *
34
     * @param  string<AbstractPage>  $model  Class constant of the Model to get the Parser for.
35
     * @param  string  $slug  The slug of the source file to parse.
36
     *
37
     * @example getParserForModel(MarkdownPost::class, 'hello-world')
38
     *
39
     * @return PageParserContract The constructed Parser instance.
40
     */
41
    public static function getParserInstanceForModel(string $model, string $slug): PageParserContract
42
    {
43
        /** @var AbstractPage $model */
44
        return new $model::$parserClass($slug);
45
    }
46
47
    /**
48
     * Supply a model::class constant and get a list of all the existing source file base names.
49
     *
50
     * @param  string<AbstractPage>  $model
51
     * @return array
52
     *
53
     * @throws \Hyde\Framework\Exceptions\UnsupportedPageTypeException
54
     *
55
     * @example DiscoveryService::getSourceFileListForModel(BladePage::class)
56
     */
57
    public static function getSourceFileListForModel(string $model): array
58
    {
59
        if (! class_exists($model) || ! is_subclass_of($model, AbstractPage::class)) {
60
            throw new UnsupportedPageTypeException($model);
61
        }
62
63
        // Scan the source directory, and directories therein, for files that match the model's file extension.
64
65
        $files = [];
66
        foreach (glob(Hyde::path($model::qualifyBasename('{*,**/*}')), GLOB_BRACE) as $filepath) {
67
            if (! str_starts_with(basename($filepath), '_')) {
68
                $files[] = self::formatSlugForModel($model, $filepath);
69
            }
70
        }
71
72
        return $files;
73
    }
74
75
    public static function getModelFileExtension(string $model): string
76
    {
77
        /** @var AbstractPage $model */
78
        return $model::getFileExtension();
79
    }
80
81
    public static function getModelSourceDirectory(string $model): string
82
    {
83
        /** @var AbstractPage $model */
84
        return $model::getSourceDirectory();
85
    }
86
87
    public static function getBladePageFiles(): array
88
    {
89
        return self::getSourceFileListForModel(BladePage::class);
90
    }
91
92
    public static function getMarkdownPageFiles(): array
93
    {
94
        return self::getSourceFileListForModel(MarkdownPage::class);
95
    }
96
97
    public static function getMarkdownPostFiles(): array
98
    {
99
        return self::getSourceFileListForModel(MarkdownPost::class);
100
    }
101
102
    public static function getDocumentationPageFiles(): array
103
    {
104
        return self::getSourceFileListForModel(DocumentationPage::class);
105
    }
106
107
    /**
108
     * Get all the Media asset file paths.
109
     * Returns a full file path, unlike the other get*List methods.
110
     */
111
    public static function getMediaAssetFiles(): array
112
    {
113
        return glob(Hyde::path('_media/*.{'.str_replace(
114
                ' ',
115
                '',
116
                config('hyde.media_extensions', 'png,svg,jpg,jpeg,gif,ico,css,js')
117
            ).'}'), GLOB_BRACE);
118
    }
119
120
    /**
121
     * Create a filepath that can be opened in the browser from a terminal.
122
     *
123
     * @param  string<AbstractPage>  $filepath
124
     * @return string
125
     */
126
    public static function createClickableFilepath(string $filepath): string
127
    {
128
        if (realpath($filepath) === false) {
129
            return $filepath;
130
        }
131
132
        return 'file://'.str_replace(
133
            '\\',
134
            '/',
135
            realpath($filepath)
136
        );
137
    }
138
139
    public static function formatSlugForModel(string $model, string $filepath): string
140
    {
141
        /** @var AbstractPage $model */
142
        $slug = str_replace(Hyde::path($model::$sourceDirectory), '', $filepath);
143
144
        if (str_ends_with($slug, $model::$fileExtension)) {
145
            $slug = substr($slug, 0, -strlen($model::$fileExtension));
146
        }
147
148
        return unslash($slug);
149
    }
150
}
151