forked from phpbb/phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.php
More file actions
219 lines (198 loc) · 4.52 KB
/
Copy pathstorage.php
File metadata and controls
219 lines (198 loc) · 4.52 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
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\storage;
/**
* @internal Experimental
*/
class storage
{
/**
* @var string
*/
protected $storage_name;
/**
* @var \phpbb\storage\adapter_factory
*/
protected $factory;
/**
* @var \phpbb\storage\adapter\adapter_interface
*/
protected $adapter;
/**
* Constructor
*
* @param \phpbb\storage\adapter_factory $factory
* @param string $storage_name
*/
public function __construct(adapter_factory $factory, $storage_name)
{
$this->factory = $factory;
$this->storage_name = $storage_name;
}
/**
* Returns storage name
*
* @return string
*/
public function get_name()
{
return $this->storage_name;
}
/**
* Returns an adapter instance
*
* @return \phpbb\storage\adapter\adapter_interface
*/
protected function get_adapter()
{
if ($this->adapter === null)
{
$this->adapter = $this->factory->get($this->storage_name);
}
return $this->adapter;
}
/**
* Dumps content into a file.
*
* @param string path The file to be written to.
* @param string content The data to write into the file.
*
* @throws \phpbb\storage\exception\exception When the file already exists
* When the file cannot be written
*/
public function put_contents($path, $content)
{
$this->get_adapter()->put_contents($path, $content);
}
/**
* Read the contents of a file
*
* @param string $path The file to read
*
* @throws \phpbb\storage\exception\exception When the file doesn't exist
* When cannot read file contents
*
* @return string Returns file contents
*
*/
public function get_contents($path)
{
return $this->get_adapter()->get_contents($path);
}
/**
* Checks the existence of files or directories.
*
* @param string $path file/directory to check
*
* @return bool Returns true if the file/directory exist, false otherwise.
*/
public function exists($path)
{
return $this->get_adapter()->exists($path);
}
/**
* Removes files or directories.
*
* @param string $path file/directory to remove
*
* @throws \phpbb\storage\exception\exception When removal fails.
*/
public function delete($path)
{
$this->get_adapter()->delete($path);
}
/**
* Rename a file or a directory.
*
* @param string $path_orig The original file/direcotry
* @param string $path_dest The target file/directory
*
* @throws \phpbb\storage\exception\exception When target exists
* When file/directory cannot be renamed
*/
public function rename($path_orig, $path_dest)
{
$this->get_adapter()->rename($path_orig, $path_dest);
}
/**
* Copies a file.
*
* @param string $path_orig The original filename
* @param string $path_dest The target filename
*
* @throws \phpbb\storage\exception\exception When target exists
* When the file cannot be copied
*/
public function copy($path_orig, $path_dest)
{
$this->get_adapter()->copy($path_orig, $path_dest);
}
/**
* Reads a file as a stream.
*
* @param string $path File to read
*
* @throws \phpbb\storage\exception\exception When unable to open file
* @return resource Returns a file pointer
*/
public function read_stream($path)
{
$stream = null;
$adapter = $this->get_adapter();
if ($adapter instanceof stream_interface)
{
$stream = $adapter->read_stream($path);
}
else
{
// Simulate the stream
$stream = fopen('php://temp', 'w+b');
fwrite($stream, $adapter->get_contents($path));
rewind($stream);
}
return $stream;
}
/**
* Writes a new file using a stream.
*
* @param string $path The target file
* @param resource $resource The resource
* When target file cannot be created
*/
public function write_stream($path, $resource)
{
$adapter = $this->get_adapter();
if ($adapter instanceof stream_interface)
{
$adapter->write_stream($path, $resource);
}
else
{
// Simulate the stream
$adapter->put_contents($path, stream_get_contents($resource));
}
}
/**
* Get file info.
*
* @param string $path The file
*
* @throws \phpbb\storage\exception\not_implemented When the adapter doesnt implement the method
*
* @return \phpbb\storage\file_info Returns file_info object
*/
public function file_info($path)
{
return new file_info($this->adapter, $path);
}
}