|
1 <?php |
|
2 /** |
|
3 * LICENSE |
|
4 * |
|
5 * This source file is subject to the new BSD license that is bundled |
|
6 * with this package in the file LICENSE.txt. |
|
7 * It is also available through the world-wide-web at this URL: |
|
8 * http://framework.zend.com/license/new-bsd |
|
9 * If you did not receive a copy of the license and are unable to |
|
10 * obtain it through the world-wide-web, please send an email |
|
11 * to license@zend.com so we can send you a copy immediately. |
|
12 * |
|
13 * @category Zend |
|
14 * @package Zend_Cloud |
|
15 * @subpackage StorageService |
|
16 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
17 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
18 */ |
|
19 |
|
20 require_once 'Zend/Cloud/StorageService/Adapter.php'; |
|
21 require_once 'Zend/Cloud/StorageService/Exception.php'; |
|
22 |
|
23 /** |
|
24 * FileSystem adapter for unstructured cloud storage. |
|
25 * |
|
26 * @category Zend |
|
27 * @package Zend_Cloud |
|
28 * @subpackage StorageService |
|
29 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
30 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
31 */ |
|
32 class Zend_Cloud_StorageService_Adapter_FileSystem implements Zend_Cloud_StorageService_Adapter |
|
33 { |
|
34 |
|
35 /** |
|
36 * Options array keys for the file system adapter. |
|
37 */ |
|
38 const LOCAL_DIRECTORY = 'local_directory'; |
|
39 |
|
40 /** |
|
41 * The directory for the data |
|
42 * @var string |
|
43 */ |
|
44 protected $_directory = null; |
|
45 |
|
46 /** |
|
47 * Constructor |
|
48 * |
|
49 * @param array|Zend_Config $options |
|
50 * @return void |
|
51 */ |
|
52 public function __construct($options = array()) |
|
53 { |
|
54 if ($options instanceof Zend_Config) { |
|
55 $options = $options->toArray(); |
|
56 } |
|
57 |
|
58 if (!is_array($options)) { |
|
59 throw new Zend_Cloud_StorageService_Exception('Invalid options provided'); |
|
60 } |
|
61 |
|
62 if (isset($options[self::LOCAL_DIRECTORY])) { |
|
63 $this->_directory = $options[self::LOCAL_DIRECTORY]; |
|
64 } else { |
|
65 $this->_directory = realpath(sys_get_temp_dir()); |
|
66 } |
|
67 } |
|
68 |
|
69 /** |
|
70 * Get an item from the storage service. |
|
71 * |
|
72 * TODO: Support streaming |
|
73 * |
|
74 * @param string $path |
|
75 * @param array $options |
|
76 * @return false|string |
|
77 */ |
|
78 public function fetchItem($path, $options = array()) |
|
79 { |
|
80 $filepath = $this->_getFullPath($path); |
|
81 $path = realpath($filepath); |
|
82 |
|
83 if (!$path) { |
|
84 return false; |
|
85 } |
|
86 |
|
87 return file_get_contents($path); |
|
88 } |
|
89 |
|
90 /** |
|
91 * Store an item in the storage service. |
|
92 * |
|
93 * WARNING: This operation overwrites any item that is located at |
|
94 * $destinationPath. |
|
95 * |
|
96 * @TODO Support streams |
|
97 * |
|
98 * @param string $destinationPath |
|
99 * @param mixed $data |
|
100 * @param array $options |
|
101 * @return void |
|
102 */ |
|
103 public function storeItem($destinationPath, $data, $options = array()) |
|
104 { |
|
105 $path = $this->_getFullPath($destinationPath); |
|
106 file_put_contents($path, $data); |
|
107 chmod($path, 0777); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Delete an item in the storage service. |
|
112 * |
|
113 * @param string $path |
|
114 * @param array $options |
|
115 * @return void |
|
116 */ |
|
117 public function deleteItem($path, $options = array()) |
|
118 { |
|
119 if (!isset($path)) { |
|
120 return; |
|
121 } |
|
122 |
|
123 $filepath = $this->_getFullPath($path); |
|
124 if (file_exists($filepath)) { |
|
125 unlink($filepath); |
|
126 } |
|
127 } |
|
128 |
|
129 /** |
|
130 * Copy an item in the storage service to a given path. |
|
131 * |
|
132 * WARNING: This operation is *very* expensive for services that do not |
|
133 * support copying an item natively. |
|
134 * |
|
135 * @TODO Support streams for those services that don't support natively |
|
136 * |
|
137 * @param string $sourcePath |
|
138 * @param string $destination path |
|
139 * @param array $options |
|
140 * @return void |
|
141 */ |
|
142 public function copyItem($sourcePath, $destinationPath, $options = array()) |
|
143 { |
|
144 copy($this->_getFullPath($sourcePath), $this->_getFullPath($destinationPath)); |
|
145 } |
|
146 |
|
147 /** |
|
148 * Move an item in the storage service to a given path. |
|
149 * |
|
150 * WARNING: This operation is *very* expensive for services that do not |
|
151 * support moving an item natively. |
|
152 * |
|
153 * @TODO Support streams for those services that don't support natively |
|
154 * |
|
155 * @param string $sourcePath |
|
156 * @param string $destination path |
|
157 * @param array $options |
|
158 * @return void |
|
159 */ |
|
160 public function moveItem($sourcePath, $destinationPath, $options = array()) |
|
161 { |
|
162 rename($this->_getFullPath($sourcePath), $this->_getFullPath($destinationPath)); |
|
163 } |
|
164 |
|
165 /** |
|
166 * Rename an item in the storage service to a given name. |
|
167 * |
|
168 * |
|
169 * @param string $path |
|
170 * @param string $name |
|
171 * @param array $options |
|
172 * @return void |
|
173 */ |
|
174 public function renameItem($path, $name, $options = null) |
|
175 { |
|
176 rename( |
|
177 $this->_getFullPath($path), |
|
178 dirname($this->_getFullPath($path)) . DIRECTORY_SEPARATOR . $name |
|
179 ); |
|
180 } |
|
181 |
|
182 /** |
|
183 * List items in the given directory in the storage service |
|
184 * |
|
185 * The $path must be a directory |
|
186 * |
|
187 * |
|
188 * @param string $path Must be a directory |
|
189 * @param array $options |
|
190 * @return array A list of item names |
|
191 */ |
|
192 public function listItems($path, $options = null) |
|
193 { |
|
194 $listing = scandir($this->_getFullPath($path)); |
|
195 |
|
196 // Remove the hidden navigation directories |
|
197 $listing = array_diff($listing, array('.', '..')); |
|
198 |
|
199 return $listing; |
|
200 } |
|
201 |
|
202 /** |
|
203 * Get a key/value array of metadata for the given path. |
|
204 * |
|
205 * @param string $path |
|
206 * @param array $options |
|
207 * @return array |
|
208 */ |
|
209 public function fetchMetadata($path, $options = array()) |
|
210 { |
|
211 $fullPath = $this->_getFullPath($path); |
|
212 $metadata = null; |
|
213 if (file_exists($fullPath)) { |
|
214 $metadata = stat(realpath($fullPath)); |
|
215 } |
|
216 |
|
217 return isset($metadata) ? $metadata : false; |
|
218 } |
|
219 |
|
220 /** |
|
221 * Store a key/value array of metadata at the given path. |
|
222 * WARNING: This operation overwrites any metadata that is located at |
|
223 * $destinationPath. |
|
224 * |
|
225 * @param string $destinationPath |
|
226 * @param array $options |
|
227 * @return void |
|
228 */ |
|
229 public function storeMetadata($destinationPath, $metadata, $options = array()) |
|
230 { |
|
231 require_once 'Zend/Cloud/OperationNotAvailableException.php'; |
|
232 throw new Zend_Cloud_OperationNotAvailableException('Storing metadata not implemented'); |
|
233 } |
|
234 |
|
235 /** |
|
236 * Delete a key/value array of metadata at the given path. |
|
237 * |
|
238 * @param string $path |
|
239 * @param array $options |
|
240 * @return void |
|
241 */ |
|
242 public function deleteMetadata($path) |
|
243 { |
|
244 require_once 'Zend/Cloud/OperationNotAvailableException.php'; |
|
245 throw new Zend_Cloud_OperationNotAvailableException('Deleting metadata not implemented'); |
|
246 } |
|
247 |
|
248 /** |
|
249 * Return the full path for the file. |
|
250 * |
|
251 * @param string $path |
|
252 * @return string |
|
253 */ |
|
254 private function _getFullPath($path) |
|
255 { |
|
256 return $this->_directory . DIRECTORY_SEPARATOR . $path; |
|
257 } |
|
258 |
|
259 /** |
|
260 * Get the concrete client. |
|
261 * @return strings |
|
262 */ |
|
263 public function getClient() |
|
264 { |
|
265 return $this->_directory; |
|
266 } |
|
267 } |