-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVFSWrapper.php
More file actions
219 lines (191 loc) · 4.82 KB
/
VFSWrapper.php
File metadata and controls
219 lines (191 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace org\jecat\framework\fs\vfs ;
class VFSWrapper
{
public $context ;
private $resource ;
private $aFileSystem ;
/**
* 根据 stream 协议 取得一个虚拟文件系统
*/
static public function vfs($sProtocol='vfs')
{
if( !isset(self::$arrProtocols[$sProtocol]) )
{
self::$arrProtocols[$sProtocol] = new VirtualFileSystem() ;
//stream_wrapper_unregister($sProtocol) ;
stream_wrapper_register($sProtocol,__CLASS__) ;
}
return self::$arrProtocols[$sProtocol] ;
}
static protected function findFileSystem($sUrl)
{
$arrUrlInfo = parse_url($sUrl) ;
if( !$aVfs=self::vfs($arrUrlInfo['scheme']) )
{
return null ;
}
return $aVfs->fileSystem($arrUrlInfo['host'].$arrUrlInfo['path']) ;
}
static protected function localeFileSystem($sUrl)
{
$arrUrlInfo = parse_url($sUrl) ;
if( !$aVfs=self::vfs($arrUrlInfo['scheme']) )
{
return null ;
}
return $aVfs->localeFileSystemPath($arrUrlInfo['host'].$arrUrlInfo['path']) ;
}
// -------------------------------------------
// stream 的操作
/**
* 仅仅做为 stream 操作的构造函数
* 非 stream 操作不会触发这个方法
*/
public function __construct()
{}
public function stream_open($sUrl,$sMode,$options,&$opened_path)
{
if( !list($this->aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return ($this->resource=&$this->aFileSystem->openFile($sPath,$sMode,$options,$opened_path)) ? true: false ;
}
public function stream_close()
{
return $this->aFileSystem->closeFile($this->resource) ;
}
public function stream_eof()
{
return $this->aFileSystem->endOfFile($this->resource) ;
}
public function stream_lock($operation)
{
return $this->aFileSystem->lockFile($this->resource,$operation) ;
}
public function stream_flush()
{
return $this->aFileSystem->flushFile($this->resource) ;
}
public function stream_read($nCount)
{
return $this->aFileSystem->readFile($this->resource,$nCount) ;
}
public function stream_seek($offset,$whence=SEEK_SET)
{
return $this->aFileSystem->seekFile($this->resource,$offset,$whence) ;
}
public function stream_tell()
{
return $this->aFileSystem->tellFile($this->resource) ;
}
public function stream_write($data)
{
return $this->aFileSystem->writeFile($this->resource,$data) ;
}
// public function stream_cast ( $cast_as )
// {
// }
// public function stream_metadata ( $path , $option , $var )
// {
// }
// public function stream_set_option ( $option , $arg1 , $arg2 )
// {
// }
public function stream_stat ( )
{
return false;
}
// public function stream_truncate ( $new_size )
// {
// }
public function unlink ( $sUrl )
{
if( !list($this->aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return $this->aFileSystem->unlinkFile($sPath) ;
}
public function url_stat ($sUrl,$flags)
{
if( !list($this->aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return $this->aFileSystem->stat($sPath,$flags) ;
}
// -------------------------------------------
// 非 stream 的操作(针对目录的操作)
/**
* 目录操作的实际构造函数
*/
public function dir_opendir($sUrl,$options)
{
if( !list($this->aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return ($this->resource=$this->aFileSystem->opendir($sPath,$options)) ? true: false ;
}
public function dir_closedir()
{
return $this->aFileSystem->closedir($this->resource) ;
}
public function dir_readdir()
{
return $this->aFileSystem->readdir($this->resource) ;
}
public function dir_rewinddir ()
{
return $this->aFileSystem->rewinddir($this->resource) ;
}
public function mkdir($sUrl,$nMode,$options)
{
if( !list($aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return $aFileSystem->mkdir($sPath,$nMode,$options) ;
}
/**
* 移动文件
*/
public function rename($sUrlFrom,$sUrlTo)
{
if( !list($aFromFs,$sFromPath)=self::localeFileSystem($sUrlFrom) )
{
return false ;
}
list($aToFs,$sToPath)=self::localeFileSystem($sUrlTo) ;
// 同一文件系统内
if( $aFromFs===$aToFs and $aToFs )
{
return $aFromFs->rename($sFromPath,$sToPath) ;
}
// 都是 本地文件系统
else if( ($aFromFs instanceof LocalFileSystem) and ($aToFs instanceof LocalFileSystem) )
{
return rename( $aFromFs->url($sFromPath), $aToFs->url($sToPath) ) ;
}
// 暴力移动(对大文件会有性能问题)
else
{
if( !file_put_contents($sUrlTo,file_get_contents($sUrlFrom)) )
{
return false ;
}
return $this->unlink($sUrlFrom) ;
}
}
public function rmdir($sUrl,$options)
{
if( !list($aFileSystem,$sPath)=self::localeFileSystem($sUrl) )
{
return false ;
}
return $aFileSystem->rmdir($sPath,$options) ;
}
static private $arrProtocols = array() ;
}