|
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 |
|
|
|
|
|
|
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
|
|
|
|