/library/Zend/Cloud/StorageService/Adapter/FileSystem.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 267 lines · 91 code · 25 blank · 151 comment · 8 complexity · f6f34dc7185828825117bd0a76ed9bf9 MD5 · raw file

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