|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Console\Helpers; |
|
6
|
|
|
|
|
7
|
|
|
use Hyde\Facades\Filesystem; |
|
8
|
|
|
use Hyde\Foundation\Providers\ViewServiceProvider; |
|
9
|
|
|
use Hyde\Hyde; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
|
|
12
|
|
|
use function Hyde\path_join; |
|
13
|
|
|
use function Hyde\unslash; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @internal Helper object for publishable view groups. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ViewPublishGroup |
|
19
|
|
|
{ |
|
20
|
|
|
public readonly string $group; |
|
21
|
|
|
|
|
22
|
|
|
public readonly string $name; |
|
23
|
|
|
public readonly string $description; |
|
24
|
|
|
|
|
25
|
|
|
public readonly string $source; |
|
26
|
|
|
public readonly string $target; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array<string> The filenames relative to the source directory. */ |
|
29
|
|
|
public readonly array $files; |
|
30
|
|
|
|
|
31
|
|
|
/** @var class-string<\Hyde\Foundation\Providers\ViewServiceProvider> */ |
|
|
|
|
|
|
32
|
|
|
protected static string $provider = ViewServiceProvider::class; |
|
33
|
|
|
|
|
34
|
|
|
protected function __construct(string $group, string $source, string $target, array $files, ?string $name = null, ?string $description = null) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->group = $group; |
|
|
|
|
|
|
37
|
|
|
$this->source = $source; |
|
|
|
|
|
|
38
|
|
|
$this->target = $target; |
|
|
|
|
|
|
39
|
|
|
$this->files = $files; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$this->name = $name ?? Hyde::makeTitle($group); |
|
|
|
|
|
|
42
|
|
|
$this->description = $description ?? "Publish the '$group' files for customization."; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function fromGroup(string $group, ?string $name = null, ?string $description = null): static |
|
46
|
|
|
{ |
|
47
|
|
|
[$source, $target] = static::keyedArrayToTuple(static::$provider::pathsToPublish(static::$provider, $group)); |
|
48
|
|
|
[$source, $target] = [static::normalizePath($source), static::normalizePath($target)]; |
|
49
|
|
|
|
|
50
|
|
|
$files = static::findFiles($source); |
|
51
|
|
|
|
|
52
|
|
|
return new static($group, $source, $target, $files, $name, $description); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @return array<string, string> The source file paths mapped to their target file paths. */ |
|
56
|
|
|
public function publishableFilesMap(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return collect($this->files)->mapWithKeys(fn (string $file): array => [ |
|
|
|
|
|
|
59
|
|
|
path_join($this->source, $file) => path_join($this->target, $file), |
|
60
|
|
|
])->all(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array<string, string> $array |
|
65
|
|
|
* @return list<string> |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
|
|
protected static function keyedArrayToTuple(array $array): array |
|
68
|
|
|
{ |
|
69
|
|
|
return [key($array), current($array)]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @return array<string> */ |
|
73
|
|
|
protected static function findFiles(string $source): array |
|
74
|
|
|
{ |
|
75
|
|
|
return Filesystem::findFiles($source, recursive: true) |
|
76
|
|
|
->map(fn (string $file) => static::normalizePath($file)) |
|
77
|
|
|
->map(fn (string $file) => unslash(Str::after($file, $source))) |
|
78
|
|
|
->sort(fn (string $a, string $b): int => substr_count($a, '/') <=> substr_count($b, '/') ?: strcmp($a, $b)) |
|
79
|
|
|
->all(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected static function normalizePath(string $path): string |
|
83
|
|
|
{ |
|
84
|
|
|
return Hyde::pathToRelative( |
|
|
|
|
|
|
85
|
|
|
Filesystem::exists($path) ? realpath($path) : $path |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|