PageRenderTime 25ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

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

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