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

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