1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Filesystem; |
6
|
|
|
|
7
|
|
|
use drupol\phpvfs\Commands\Cd; |
8
|
|
|
use drupol\phpvfs\Commands\Exist; |
9
|
|
|
use drupol\phpvfs\Commands\Get; |
10
|
|
|
use drupol\phpvfs\Commands\Inspect; |
11
|
|
|
use drupol\phpvfs\Commands\Touch; |
12
|
|
|
use drupol\phpvfs\Node\Directory; |
13
|
|
|
use drupol\phpvfs\Node\DirectoryInterface; |
14
|
|
|
use drupol\phpvfs\Node\File; |
15
|
|
|
use drupol\phpvfs\Node\VfsInterface; |
16
|
|
|
|
17
|
|
|
class Filesystem implements FilesystemInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var \drupol\phpvfs\Node\DirectoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $cwd; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Filesystem constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $id |
28
|
|
|
* @param array $attributes |
29
|
|
|
* |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
1 |
|
public function __construct( |
33
|
|
|
string $id, |
34
|
|
|
array $attributes = [] |
35
|
|
|
) { |
36
|
|
|
$attributes = [ |
37
|
1 |
|
'id' => $id, |
38
|
1 |
|
'vfs' => $this, |
39
|
1 |
|
] + $attributes; |
40
|
|
|
|
41
|
1 |
|
$this->cwd = Directory::create($id, $attributes); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function cd(string $id) |
48
|
|
|
{ |
49
|
|
|
Cd::exec($this, $id); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public static function create(string $id, array $attributes = []) |
58
|
|
|
{ |
59
|
|
|
return new self($id, $attributes); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public static function directory(string $id, array $attributes = []): DirectoryInterface |
66
|
|
|
{ |
67
|
|
|
return Directory::create($id, $attributes); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function exist(string $id): bool |
74
|
|
|
{ |
75
|
1 |
|
return Exist::exec($this, $id); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public static function file(string $id, string $content = '', array $attributes = []): File |
82
|
|
|
{ |
83
|
|
|
return File::create($id, $content, $attributes); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
1 |
|
public function get(string $id): ?VfsInterface |
90
|
|
|
{ |
91
|
1 |
|
return Get::exec($this, $id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function getCwd(): DirectoryInterface |
98
|
|
|
{ |
99
|
1 |
|
return $this->cwd; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function inspect(string $id): string |
106
|
|
|
{ |
107
|
1 |
|
return Inspect::exec($this, $id); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function root(): VfsInterface |
114
|
|
|
{ |
115
|
|
|
return $this->cwd->root(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
1 |
|
public function setCwd(DirectoryInterface $dir) |
122
|
|
|
{ |
123
|
1 |
|
$this->cwd = $dir; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function touch(string $id) |
132
|
|
|
{ |
133
|
|
|
Touch::exec($this, $id); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|