Passed
Push — master ( eb8cff...c3a046 )
by Caen
03:04 queued 14s
created

DiscoveryService::getMediaAssetFiles()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hyde\Framework\Services;
4
5
use Hyde\Framework\Contracts\AbstractPage;
6
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;
7
use Hyde\Framework\Hyde;
8
use Hyde\Framework\Models\File;
0 ignored issues
show
Bug introduced by
The type Hyde\Framework\Models\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
 * @see \Hyde\Framework\Testing\Feature\DiscoveryServiceTest
24
 */
25
class DiscoveryService
26
{
27
    /**
28
     * Supply a model::class constant and get a list of all the existing source file base names.
29
     *
30
     * @param  string<AbstractPage>  $model
31
     * @return array
32
     *
33
     * @throws \Hyde\Framework\Exceptions\UnsupportedPageTypeException
34
     *
35
     * @example DiscoveryService::getSourceFileListForModel(BladePage::class)
36
     */
37
    public static function getSourceFileListForModel(string $model): array
38
    {
39
        if (! class_exists($model) || ! is_subclass_of($model, AbstractPage::class)) {
40
            throw new UnsupportedPageTypeException($model);
41
        }
42
43
        $files = [];
44
        Hyde::files()->getSourceFiles($model)->each(function (File $file) use (&$files, $model) {
45
            $files[] = self::formatSlugForModel($model, $file->withoutDirectoryPrefix());
46
        });
47
48
        return $files;
49
    }
50
51
    public static function getModelFileExtension(string $model): string
52
    {
53
        /** @var AbstractPage $model */
54
        return $model::getFileExtension();
55
    }
56
57
    public static function getModelSourceDirectory(string $model): string
58
    {
59
        /** @var AbstractPage $model */
60
        return $model::getSourceDirectory();
61
    }
62
63
    public static function getBladePageFiles(): array
64
    {
65
        return self::getSourceFileListForModel(BladePage::class);
66
    }
67
68
    public static function getMarkdownPageFiles(): array
69
    {
70
        return self::getSourceFileListForModel(MarkdownPage::class);
71
    }
72
73
    public static function getMarkdownPostFiles(): array
74
    {
75
        return self::getSourceFileListForModel(MarkdownPost::class);
76
    }
77
78
    public static function getDocumentationPageFiles(): array
79
    {
80
        return self::getSourceFileListForModel(DocumentationPage::class);
81
    }
82
83
    /**
84
     * Get all the Media asset file paths.
85
     * Returns a full file path, unlike the other get*List methods.
86
     */
87
    public static function getMediaAssetFiles(): array
88
    {
89
        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
90
    }
91
92
    /**
93
     * Create a filepath that can be opened in the browser from a terminal.
94
     *
95
     * @param  string<AbstractPage>  $filepath
96
     * @return string
97
     */
98
    public static function createClickableFilepath(string $filepath): string
99
    {
100
        if (realpath($filepath) === false) {
101
            return $filepath;
102
        }
103
104
        return 'file://'.str_replace(
105
            '\\',
106
            '/',
107
            realpath($filepath)
108
        );
109
    }
110
111
    public static function formatSlugForModel(string $model, string $filepath): string
112
    {
113
        /** @var AbstractPage $model */
114
        $slug = str_replace(Hyde::path($model::$sourceDirectory), '', $filepath);
115
116
        if (str_ends_with($slug, $model::$fileExtension)) {
117
            $slug = substr($slug, 0, -strlen($model::$fileExtension));
118
        }
119
120
        return unslash($slug);
121
    }
122
123
    protected static function getMediaGlobPattern(): string
124
    {
125
        return sprintf('_media/*.{%s}', str_replace(' ', '',
126
            config('hyde.media_extensions', 'png,svg,jpg,jpeg,gif,ico,css,js')
127
        ));
128
    }
129
}
130