1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-vfs package. |
4
|
|
|
* |
5
|
|
|
* (c) Michael Donat <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace VirtualFileSystem; |
12
|
|
|
|
13
|
|
|
use VirtualFileSystem\Structure\Directory; |
14
|
|
|
use VirtualFileSystem\Structure\File; |
15
|
|
|
use VirtualFileSystem\Structure\Link; |
16
|
|
|
use VirtualFileSystem\Structure\Root; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Main 'access' class to vfs implementation. It will register new stream wrapper on instantiation. |
20
|
|
|
* |
21
|
|
|
* This class provides methods to get access to Container as well as file URI helper. |
22
|
|
|
* |
23
|
|
|
* @author Michael Donat <[email protected]> |
24
|
|
|
* @package php-vfs |
25
|
|
|
*/ |
26
|
|
|
class FileSystem |
27
|
|
|
{ |
28
|
|
|
protected $scheme; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Container |
32
|
|
|
*/ |
33
|
|
|
protected $container; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Class constructor. Will register both, the stream default options and wrapper handler. |
37
|
|
|
* |
38
|
|
|
* Note: Each FileSystem instance will create NEW stream wrapper/scheme. |
39
|
|
|
*/ |
40
|
|
|
public function __construct() |
41
|
|
|
{ |
42
|
|
|
$this->scheme = uniqid('phpvfs'); |
43
|
|
|
|
44
|
|
|
/* injecting components */ |
45
|
|
|
$this->container = $container = new Container(new Factory()); |
46
|
|
|
$this->container->root()->setScheme($this->scheme); |
47
|
|
|
|
48
|
|
|
$this->registerContextOptions($container); |
49
|
|
|
|
50
|
|
|
stream_wrapper_register($this->scheme, sprintf('\%s\%s', __NAMESPACE__, 'Wrapper')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns wrapper scheme. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function scheme() |
59
|
|
|
{ |
60
|
|
|
return $this->scheme; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Registers Container object as default context option for scheme associated with FileSystem instance. |
65
|
|
|
* |
66
|
|
|
* @param Container $container |
67
|
|
|
*/ |
68
|
|
|
protected function registerContextOptions(Container $container) |
69
|
|
|
{ |
70
|
|
|
$defaultOptions = stream_context_get_options(stream_context_get_default()); |
71
|
|
|
stream_context_set_default(array_merge( |
72
|
|
|
$defaultOptions, |
73
|
|
|
array($this->scheme => array('Container' => $container)) |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Remoces wrapper registered for scheme associated with FileSystem instance. |
79
|
|
|
*/ |
80
|
|
|
public function __destruct() |
81
|
|
|
{ |
82
|
|
|
stream_wrapper_unregister($this->scheme); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns Container instance. |
87
|
|
|
* |
88
|
|
|
* @return Container |
89
|
|
|
*/ |
90
|
|
|
public function container() |
91
|
|
|
{ |
92
|
|
|
return $this->container; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns Root instance. |
97
|
|
|
* |
98
|
|
|
* @return Root |
99
|
|
|
*/ |
100
|
|
|
public function root() |
101
|
|
|
{ |
102
|
|
|
return $this->container()->root(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns absolute path to full URI path (with scheme) |
107
|
|
|
* |
108
|
|
|
* @param string $path - path without scheme |
109
|
|
|
* |
110
|
|
|
* @return string - path with scheme |
111
|
|
|
*/ |
112
|
|
|
public function path($path) |
113
|
|
|
{ |
114
|
|
|
$path = ltrim($path, '/'); |
115
|
|
|
|
116
|
|
|
return $this->scheme().'://'.$path; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates and returns a directory |
121
|
|
|
* |
122
|
|
|
* @param string $path |
123
|
|
|
* @param bool $recursive |
124
|
|
|
* @param integer|null $mode |
125
|
|
|
* |
126
|
|
|
* @return Directory |
127
|
|
|
*/ |
128
|
|
|
public function createDirectory($path, $recursive = false, $mode = null) |
129
|
|
|
{ |
130
|
|
|
return $this->container()->createDir($path, $recursive, $mode); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Creates and returns a file |
135
|
|
|
* |
136
|
|
|
* @param string $path |
137
|
|
|
* @param string|null $data |
138
|
|
|
* |
139
|
|
|
* @return File |
140
|
|
|
*/ |
141
|
|
|
public function createFile($path, $data = null) |
142
|
|
|
{ |
143
|
|
|
return $this->container()->createFile($path, $data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Creates fs structure |
148
|
|
|
* |
149
|
|
|
* @param array $structure |
150
|
|
|
*/ |
151
|
|
|
public function createStructure(array $structure) |
152
|
|
|
{ |
153
|
|
|
$this->container()->createStructure($structure); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Creates and returns a link |
158
|
|
|
* |
159
|
|
|
* @param string $path |
160
|
|
|
* @param string $destinationPath |
161
|
|
|
* |
162
|
|
|
* @return Link |
163
|
|
|
*/ |
164
|
|
|
public function createLink($path, $destinationPath) |
165
|
|
|
{ |
166
|
|
|
return $this->container()->createLink($path, $destinationPath); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|