Passed
Push — master ( 486f33...203560 )
by Caen
03:17 queued 13s
created

DiscoveryService::createClickableFilepath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use function class_exists;
8
use function config;
9
use function glob;
10
use Hyde\Foundation\Facades\FileCollection;
11
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
12
use Hyde\Hyde;
13
use Hyde\Pages\BladePage;
14
use Hyde\Pages\Concerns\HydePage;
15
use Hyde\Pages\DocumentationPage;
16
use Hyde\Pages\MarkdownPage;
17
use Hyde\Pages\MarkdownPost;
18
use Hyde\Support\Filesystem\SourceFile;
19
use Illuminate\Support\Str;
20
use function implode;
21
use function is_array;
22
use function is_subclass_of;
23
use function sprintf;
24
use function str_replace;
25
use function unslash;
26
27
/**
28
 * The core service that powers all HydePHP file auto-discovery.
29
 *
30
 * Contains service methods to return helpful collections of arrays and lists,
31
 * and provides helper methods for source file auto-discovery used in the site
32
 * building process to determine where files are located and how to parse them.
33
 *
34
 * @see \Hyde\Framework\Testing\Feature\DiscoveryServiceTest
35
 */
36
class DiscoveryService
37
{
38
    final public const DEFAULT_MEDIA_EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
39
40
    public static function getBladePageFiles(): array
41
    {
42
        return static::getSourceFileListForModel(BladePage::class);
43
    }
44
45
    public static function getMarkdownPageFiles(): array
46
    {
47
        return static::getSourceFileListForModel(MarkdownPage::class);
48
    }
49
50
    public static function getMarkdownPostFiles(): array
51
    {
52
        return static::getSourceFileListForModel(MarkdownPost::class);
53
    }
54
55
    public static function getDocumentationPageFiles(): array
56
    {
57
        return static::getSourceFileListForModel(DocumentationPage::class);
58
    }
59
60
    /**
61
     * Supply a model::class constant and get a list of all the existing source file base names.
62
     *
63
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $model
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
64
     * @return array<string>
65
     *
66
     * @throws \Hyde\Framework\Exceptions\UnsupportedPageTypeException
67
     *
68
     * @example Usage: DiscoveryService::getSourceFileListForModel(BladePage::class)
69
     * @example Returns: ['index', 'about', 'contact']
70
     */
71
    public static function getSourceFileListForModel(string $model): array
72
    {
73
        if (! class_exists($model) || ! is_subclass_of($model, HydePage::class)) {
74
            throw new UnsupportedPageTypeException($model);
75
        }
76
77
        return FileCollection::getSourceFiles($model)->flatten()->map(function (SourceFile $file) use ($model): string {
0 ignored issues
show
Bug introduced by
The method getSourceFiles() does not exist on Hyde\Foundation\Facades\FileCollection. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

77
        return FileCollection::/** @scrutinizer ignore-call */ getSourceFiles($model)->flatten()->map(function (SourceFile $file) use ($model): string {
Loading history...
78
            return static::pathToIdentifier($model, $file->withoutDirectoryPrefix());
79
        })->toArray();
80
    }
81
82
    /** @param class-string<\Hyde\Pages\Concerns\HydePage> $model */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
83
    public static function getModelFileExtension(string $model): string
84
    {
85
        return $model::fileExtension();
86
    }
87
88
    /** @param class-string<\Hyde\Pages\Concerns\HydePage> $model */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
89
    public static function getModelSourceDirectory(string $model): string
90
    {
91
        return $model::sourceDirectory();
92
    }
93
94
    /**
95
     * Format a filename to an identifier for a given model. Unlike the basename function, any nested paths
96
     * within the source directory are retained in order to satisfy the page identifier definition.
97
     *
98
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $model
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\Hyde\Pages\Concerns\HydePage> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\Hyde\Pages\Concerns\HydePage>.
Loading history...
99
     * @param  string  $filepath  Example: index.blade.php
100
     * @return string Example: index
101
     */
102
    public static function pathToIdentifier(string $model, string $filepath): string
103
    {
104
        return unslash(Str::between(Hyde::pathToRelative($filepath),
105
            $model::$sourceDirectory.'/',
0 ignored issues
show
Bug introduced by
The property sourceDirectory does not exist on string.
Loading history...
106
            $model::$fileExtension)
0 ignored issues
show
Bug introduced by
The property fileExtension does not exist on string.
Loading history...
107
        );
108
    }
109
110
    /**
111
     * Get all the Media asset file paths.
112
     * Returns a full file path, unlike the other get*List methods.
113
     *
114
     * @return array<string> An array of absolute file paths.
115
     */
116
    public static function getMediaAssetFiles(): array
117
    {
118
        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
119
    }
120
121
    protected static function getMediaGlobPattern(): string
122
    {
123
        return sprintf('_media/{*,**/*,**/*/*}.{%s}', static::parseConfiguredMediaExtensions(
124
            config('hyde.media_extensions', self::DEFAULT_MEDIA_EXTENSIONS)
125
        ));
126
    }
127
128
    protected static function parseConfiguredMediaExtensions(string|array|null $extensions): string
129
    {
130
        return is_array($extensions)
0 ignored issues
show
introduced by
The condition is_array($extensions) is always true.
Loading history...
131
            ? implode(',', $extensions)
132
            : static::removeSpaces((string) $extensions);
133
    }
134
135
    protected static function removeSpaces(string $string): string
136
    {
137
        return str_replace(' ', '', $string);
138
    }
139
}
140