-
Notifications
You must be signed in to change notification settings - Fork 7
/
FileSystem.php
168 lines (149 loc) · 3.89 KB
/
FileSystem.php
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
<?php
/*
* This file is part of the php-vfs package.
*
* (c) Michael Donat <michael.donat@me.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace VirtualFileSystem;
use VirtualFileSystem\Structure\Directory;
use VirtualFileSystem\Structure\File;
use VirtualFileSystem\Structure\Link;
use VirtualFileSystem\Structure\Root;
/**
* Main 'access' class to vfs implementation. It will register new stream wrapper on instantiation.
*
* This class provides methods to get access to Container as well as file URI helper.
*
* @author Michael Donat <michael.donat@me.com>
* @package php-vfs
*/
class FileSystem
{
protected $scheme;
/**
* @var Container
*/
protected $container;
/**
* Class constructor. Will register both, the stream default options and wrapper handler.
*
* Note: Each FileSystem instance will create NEW stream wrapper/scheme.
*/
public function __construct()
{
$this->scheme = uniqid('phpvfs');
/* injecting components */
$this->container = $container = new Container(new Factory());
$this->container->root()->setScheme($this->scheme);
$this->registerContextOptions($container);
stream_wrapper_register($this->scheme, sprintf('\%s\%s', __NAMESPACE__, 'Wrapper'));
}
/**
* Returns wrapper scheme.
*
* @return string
*/
public function scheme()
{
return $this->scheme;
}
/**
* Registers Container object as default context option for scheme associated with FileSystem instance.
*
* @param Container $container
*/
protected function registerContextOptions(Container $container)
{
$defaultOptions = stream_context_get_options(stream_context_get_default());
stream_context_set_default(array_merge(
$defaultOptions,
array($this->scheme => array('Container' => $container))
));
}
/**
* Remoces wrapper registered for scheme associated with FileSystem instance.
*/
public function __destruct()
{
stream_wrapper_unregister($this->scheme);
}
/**
* Returns Container instance.
*
* @return Container
*/
public function container()
{
return $this->container;
}
/**
* Returns Root instance.
*
* @return Root
*/
public function root()
{
return $this->container()->root();
}
/**
* Returns absolute path to full URI path (with scheme)
*
* @param string $path - path without scheme
*
* @return string - path with scheme
*/
public function path($path)
{
$path = ltrim($path, '/');
return $this->scheme().'://'.$path;
}
/**
* Creates and returns a directory
*
* @param string $path
* @param bool $recursive
* @param integer|null $mode
*
* @return Directory
*/
public function createDirectory($path, $recursive = false, $mode = null)
{
return $this->container()->createDir($path, $recursive, $mode);
}
/**
* Creates and returns a file
*
* @param string $path
* @param string|null $data
*
* @return File
*/
public function createFile($path, $data = null)
{
return $this->container()->createFile($path, $data);
}
/**
* Creates fs structure
*
* @param array $structure
*/
public function createStructure(array $structure)
{
$this->container()->createStructure($structure);
}
/**
* Creates and returns a link
*
* @param string $path
* @param string $destinationPath
*
* @return Link
*/
public function createLink($path, $destinationPath)
{
return $this->container()->createLink($path, $destinationPath);
}
}