1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Hyde\Support\Filesystem; |
6
|
|
|
|
7
|
|
|
use function basename; |
8
|
|
|
|
9
|
|
|
use Hyde\Facades\Filesystem; |
10
|
|
|
use Hyde\Hyde; |
11
|
|
|
use Hyde\Support\Concerns\Serializable; |
12
|
|
|
use Hyde\Support\Contracts\SerializableContract; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Filesystem abstraction for a file stored in the project. |
16
|
|
|
*/ |
17
|
|
|
abstract class ProjectFile implements SerializableContract |
18
|
|
|
{ |
19
|
|
|
use Serializable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string The path relative to the project root. |
23
|
|
|
* |
24
|
|
|
* @example `_pages/index.blade.php` |
25
|
|
|
* @example `_media/logo.png` |
26
|
|
|
*/ |
27
|
|
|
public readonly string $path; |
28
|
|
|
|
29
|
|
|
public static function make(string $path): static |
30
|
|
|
{ |
31
|
|
|
return new static($path); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __construct(string $path) |
35
|
|
|
{ |
36
|
|
|
$this->path = Hyde::pathToRelative($path); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array{name: string, path: string} |
41
|
|
|
*/ |
42
|
|
|
public function toArray(): array |
43
|
|
|
{ |
44
|
|
|
return [ |
|
|
|
|
45
|
|
|
'name' => $this->getName(), |
46
|
|
|
'path' => $this->getPath(), |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getName(): string |
51
|
|
|
{ |
52
|
|
|
return basename($this->path); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getPath(): string |
56
|
|
|
{ |
57
|
|
|
return $this->path; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getAbsolutePath(): string |
61
|
|
|
{ |
62
|
|
|
return Hyde::path($this->path); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getContents(): string |
66
|
|
|
{ |
67
|
|
|
return Filesystem::getContents($this->path); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function getExtension(): string |
71
|
|
|
{ |
72
|
|
|
return Filesystem::extension($this->getPath()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|