Passed
Push — master ( 798d58...a9db05 )
by Caen
03:32 queued 12s
created

DiscoveryService::getMediaGlobPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Services;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Str;
9
use Hyde\Facades\Config;
10
use function glob;
11
use function implode;
12
use function sprintf;
13
use function unslash;
14
15
/**
16
 * General Discovery Helpers for HydePHP Auto-Discovery.
17
 *
18
 * Offloads FoundationCollection logic and provides helpers for common code.
19
 *
20
 * @see \Hyde\Framework\Testing\Feature\DiscoveryServiceTest
21
 */
22
class DiscoveryService
23
{
24
    final public const DEFAULT_MEDIA_EXTENSIONS = ['png', 'svg', 'jpg', 'jpeg', 'gif', 'ico', 'css', 'js'];
25
26
    /**
27
     * Format a filename to an identifier for a given model. Unlike the basename function, any nested paths
28
     * within the source directory are retained in order to satisfy the page identifier definition.
29
     *
30
     * @param  class-string<\Hyde\Pages\Concerns\HydePage>  $pageClass
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...
31
     * @param  string  $filepath  Example: index.blade.php
32
     * @return string Example: index
33
     */
34
    public static function pathToIdentifier(string $pageClass, string $filepath): string
35
    {
36
        return unslash(Str::between(Hyde::pathToRelative($filepath),
37
            $pageClass::sourceDirectory().'/',
38
            $pageClass::fileExtension())
39
        );
40
    }
41
42
    /**
43
     * Get all the Media asset file paths.
44
     * Returns a full file path, unlike the other get*List methods.
45
     *
46
     * @return array<string> An array of absolute file paths.
47
     */
48
    public static function getMediaAssetFiles(): array
49
    {
50
        return glob(Hyde::path(static::getMediaGlobPattern()), GLOB_BRACE) ?: [];
51
    }
52
53
    protected static function getMediaGlobPattern(): string
54
    {
55
        return sprintf(Hyde::getMediaDirectory().'/{*,**/*,**/*/*}.{%s}', implode(',',
56
            Config::getArray('hyde.media_extensions', self::DEFAULT_MEDIA_EXTENSIONS)
57
        ));
58
    }
59
}
60