File indexing completed on 2025-01-26 05:24:52

0001 <?php
0002 /**
0003  * LICENSE
0004  *
0005  * This source file is subject to the new BSD license that is bundled
0006  * with this package in the file LICENSE.txt.
0007  * It is also available through the world-wide-web at this URL:
0008  * http://framework.zend.com/license/new-bsd
0009  * If you did not receive a copy of the license and are unable to
0010  * obtain it through the world-wide-web, please send an email
0011  * to license@zend.com so we can send you a copy immediately.
0012  *
0013  * @category   Zend
0014  * @package    Zend_Cloud
0015  * @subpackage StorageService
0016  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0017  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0018  */
0019 
0020 // require_once 'Zend/Cloud/StorageService/Adapter.php';
0021 // require_once 'Zend/Cloud/StorageService/Exception.php';
0022 
0023 /**
0024  * FileSystem adapter for unstructured cloud storage.
0025  *
0026  * @category   Zend
0027  * @package    Zend_Cloud
0028  * @subpackage StorageService
0029  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0030  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0031  */
0032 class Zend_Cloud_StorageService_Adapter_FileSystem implements Zend_Cloud_StorageService_Adapter
0033 {
0034 
0035     /**
0036      * Options array keys for the file system adapter.
0037      */
0038     const LOCAL_DIRECTORY = 'local_directory';
0039 
0040     /**
0041      * The directory for the data
0042      * @var string
0043      */
0044     protected $_directory = null;
0045 
0046     /**
0047      * Constructor
0048      *
0049      * @param  array|Zend_Config $options
0050      * @return void
0051      */
0052     public function __construct($options = array())
0053     {
0054         if ($options instanceof Zend_Config) {
0055             $options = $options->toArray();
0056         }
0057 
0058         if (!is_array($options)) {
0059             throw new Zend_Cloud_StorageService_Exception('Invalid options provided');
0060         }
0061 
0062         if (isset($options[self::LOCAL_DIRECTORY])) {
0063             $this->_directory = $options[self::LOCAL_DIRECTORY];
0064         } else {
0065             $this->_directory = realpath(sys_get_temp_dir());
0066         }
0067     }
0068 
0069     /**
0070      * Get an item from the storage service.
0071      *
0072      * TODO: Support streaming
0073      *
0074      * @param  string $path
0075      * @param  array $options
0076      * @return false|string
0077      */
0078     public function fetchItem($path, $options = array())
0079     {
0080         $filepath = $this->_getFullPath($path);
0081         $path     = realpath($filepath);
0082 
0083         if (!$path || !file_exists($path)) {
0084             return false;
0085         }
0086 
0087         return file_get_contents($path);
0088     }
0089 
0090     /**
0091      * Store an item in the storage service.
0092      *
0093      * WARNING: This operation overwrites any item that is located at
0094      * $destinationPath.
0095      *
0096      * @TODO Support streams
0097      *
0098      * @param  string $destinationPath
0099      * @param  mixed $data
0100      * @param  array $options
0101      * @return void
0102      */
0103     public function storeItem($destinationPath, $data, $options = array())
0104     {
0105         $path = $this->_getFullPath($destinationPath);
0106         file_put_contents($path, $data);
0107         chmod($path, 0775);
0108     }
0109 
0110     /**
0111      * Delete an item in the storage service.
0112      *
0113      * @param  string $path
0114      * @param  array $options
0115      * @return void
0116      */
0117     public function deleteItem($path, $options = array())
0118     {
0119         if (!isset($path)) {
0120             return;
0121         }
0122 
0123         $filepath = $this->_getFullPath($path);
0124         if (file_exists($filepath)) {
0125             unlink($filepath);
0126         }
0127     }
0128 
0129     /**
0130      * Copy an item in the storage service to a given path.
0131      *
0132      * WARNING: This operation is *very* expensive for services that do not
0133      * support copying an item natively.
0134      *
0135      * @TODO Support streams for those services that don't support natively
0136      *
0137      * @param  string $sourcePath
0138      * @param  string $destination path
0139      * @param  array $options
0140      * @return void
0141      */
0142     public function copyItem($sourcePath, $destinationPath, $options = array())
0143     {
0144         copy($this->_getFullPath($sourcePath), $this->_getFullPath($destinationPath));
0145     }
0146 
0147     /**
0148      * Move an item in the storage service to a given path.
0149      *
0150      * WARNING: This operation is *very* expensive for services that do not
0151      * support moving an item natively.
0152      *
0153      * @TODO Support streams for those services that don't support natively
0154      *
0155      * @param  string $sourcePath
0156      * @param  string $destination path
0157      * @param  array $options
0158      * @return void
0159      */
0160     public function moveItem($sourcePath, $destinationPath, $options = array())
0161     {
0162         rename($this->_getFullPath($sourcePath), $this->_getFullPath($destinationPath));
0163     }
0164 
0165         /**
0166      * Rename an item in the storage service to a given name.
0167      *
0168      *
0169      * @param  string $path
0170      * @param  string $name
0171      * @param  array $options
0172      * @return void
0173      */
0174     public function renameItem($path, $name, $options = null)
0175     {
0176         rename(
0177             $this->_getFullPath($path),
0178             dirname($this->_getFullPath($path)) . DIRECTORY_SEPARATOR . $name
0179         );
0180     }
0181 
0182     /**
0183      * List items in the given directory in the storage service
0184      *
0185      * The $path must be a directory
0186      *
0187      *
0188      * @param  string $path Must be a directory
0189      * @param  array $options
0190      * @return array A list of item names
0191      */
0192     public function listItems($path, $options = null)
0193     {
0194         $listing = scandir($this->_getFullPath($path));
0195 
0196         // Remove the hidden navigation directories
0197         $listing = array_diff($listing, array('.', '..'));
0198 
0199         return $listing;
0200     }
0201 
0202     /**
0203      * Get a key/value array of metadata for the given path.
0204      *
0205      * @param  string $path
0206      * @param  array $options
0207      * @return array
0208      */
0209     public function fetchMetadata($path, $options = array())
0210     {
0211         $fullPath = $this->_getFullPath($path);
0212         $metadata = null;
0213         if (file_exists($fullPath)) {
0214             $metadata = stat(realpath($fullPath));
0215         }
0216 
0217         return isset($metadata) ? $metadata : false;
0218     }
0219 
0220     /**
0221      * Store a key/value array of metadata at the given path.
0222      * WARNING: This operation overwrites any metadata that is located at
0223      * $destinationPath.
0224      *
0225      * @param  string $destinationPath
0226      * @param  array $options
0227      * @return void
0228      */
0229     public function storeMetadata($destinationPath, $metadata, $options = array())
0230     {
0231         // require_once 'Zend/Cloud/OperationNotAvailableException.php';
0232         throw new Zend_Cloud_OperationNotAvailableException('Storing metadata not implemented');
0233     }
0234 
0235     /**
0236      * Delete a key/value array of metadata at the given path.
0237      *
0238      * @param  string $path
0239      * @param  array $options
0240      * @return void
0241      */
0242     public function deleteMetadata($path)
0243     {
0244         // require_once 'Zend/Cloud/OperationNotAvailableException.php';
0245         throw new Zend_Cloud_OperationNotAvailableException('Deleting metadata not implemented');
0246     }
0247 
0248     /**
0249      * Return the full path for the file.
0250      *
0251      * @param string $path
0252      * @return string
0253      */
0254     private function _getFullPath($path)
0255     {
0256         return $this->_directory . DIRECTORY_SEPARATOR . $path;
0257     }
0258 
0259     /**
0260      * Get the concrete client.
0261      * @return strings
0262      */
0263     public function getClient()
0264     {
0265          return $this->_directory;
0266     }
0267 }