PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/File/Storage/File.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 275 lines | 117 code | 30 blank | 128 comment | 12 complexity | f9848bbaa206f092e9797dff0a481592 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Abstract model class
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_File_Storage_File extends Mage_Core_Model_File_Storage_Abstract
  34. {
  35. /**
  36. * Prefix of model events names
  37. *
  38. * @var string
  39. */
  40. protected $_eventPrefix = 'core_file_storage_file';
  41. /**
  42. * Data at storage
  43. *
  44. * @var array
  45. */
  46. protected $_data = null;
  47. /**
  48. * Collect errors during sync process
  49. *
  50. * @var array
  51. */
  52. protected $_errors = array();
  53. /**
  54. * Class construct
  55. */
  56. public function __construct()
  57. {
  58. $this->_init('core/file_storage_file');
  59. }
  60. /**
  61. * Initialization
  62. *
  63. * @return Mage_Core_Model_File_Storage_File
  64. */
  65. public function init()
  66. {
  67. return $this;
  68. }
  69. /**
  70. * Return storage name
  71. *
  72. * @return string
  73. */
  74. public function getStorageName()
  75. {
  76. return Mage::helper('core')->__('File system');
  77. }
  78. /**
  79. * Get files and directories from storage
  80. *
  81. * @return array
  82. */
  83. public function getStorageData()
  84. {
  85. return $this->_getResource()->getStorageData();
  86. }
  87. /**
  88. * Check if there was errors during sync process
  89. *
  90. * @return bool
  91. */
  92. public function hasErrors()
  93. {
  94. return !empty($this->_errors);
  95. }
  96. /**
  97. * Clear files and directories in storage
  98. *
  99. * @return Mage_Core_Model_File_Storage_File
  100. */
  101. public function clear()
  102. {
  103. $this->_getResource()->clear();
  104. return $this;
  105. }
  106. /**
  107. * Collect files and directories from storage
  108. *
  109. * @param int $offset
  110. * @param int $count
  111. * @param string $type
  112. * @return array|bool
  113. */
  114. public function collectData($offset = 0, $count = 100, $type = 'files')
  115. {
  116. if (!in_array($type, array('files', 'directories'))) {
  117. return false;
  118. }
  119. $offset = ((int) $offset >= 0) ? (int) $offset : 0;
  120. $count = ((int) $count >= 1) ? (int) $count : 1;
  121. if (is_null($this->_data)) {
  122. $this->_data = $this->getStorageData();
  123. }
  124. $slice = array_slice($this->_data[$type], $offset, $count);
  125. if (empty($slice)) {
  126. return false;
  127. }
  128. return $slice;
  129. }
  130. /**
  131. * Export directories list from storage
  132. *
  133. * @param int $offset
  134. * @param int $count
  135. * @return array|bool
  136. */
  137. public function exportDirectories($offset = 0, $count = 100)
  138. {
  139. return $this->collectData($offset, $count, 'directories');
  140. }
  141. /**
  142. * Export files list in defined range
  143. *
  144. * @param int $offset
  145. * @param int $count
  146. * @return array|bool
  147. */
  148. public function exportFiles($offset = 0, $count = 1)
  149. {
  150. $slice = $this->collectData($offset, $count, 'files');
  151. if (!$slice) {
  152. return false;
  153. }
  154. $result = array();
  155. foreach ($slice as $fileName) {
  156. try {
  157. $fileInfo = $this->collectFileInfo($fileName);
  158. } catch (Exception $e) {
  159. Mage::logException($e);
  160. continue;
  161. }
  162. $result[] = $fileInfo;
  163. }
  164. return $result;
  165. }
  166. /**
  167. * Import entities to storage
  168. *
  169. * @param array $data
  170. * @param string $callback
  171. * @return Mage_Core_Model_File_Storage_File
  172. */
  173. public function import($data, $callback)
  174. {
  175. if (!is_array($data) || !method_exists($this, $callback)) {
  176. return $this;
  177. }
  178. foreach ($data as $part) {
  179. try {
  180. $this->$callback($part);
  181. } catch (Exception $e) {
  182. $this->_errors[] = $e->getMessage();
  183. Mage::logException($e);
  184. }
  185. }
  186. return $this;
  187. }
  188. /**
  189. * Import directories to storage
  190. *
  191. * @param array $dirs
  192. * @return Mage_Core_Model_File_Storage_File
  193. */
  194. public function importDirectories($dirs)
  195. {
  196. return $this->import($dirs, 'saveDir');
  197. }
  198. /**
  199. * Import files list
  200. *
  201. * @param array $files
  202. * @return Mage_Core_Model_File_Storage_File
  203. */
  204. public function importFiles($files)
  205. {
  206. return $this->import($files, 'saveFile');
  207. }
  208. /**
  209. * Save directory to storage
  210. *
  211. * @param array $dir
  212. * @return bool
  213. */
  214. public function saveDir($dir)
  215. {
  216. return $this->_getResource()->saveDir($dir);
  217. }
  218. /**
  219. * Save file to storage
  220. *
  221. * @param array|Mage_Core_Model_File_Storage_Database $file
  222. * @param bool $overwrite
  223. * @return bool|int
  224. */
  225. public function saveFile($file, $overwrite = true)
  226. {
  227. if (isset($file['filename']) && !empty($file['filename'])
  228. && isset($file['content']) && !empty($file['content'])
  229. ) {
  230. try {
  231. $filename = (isset($file['directory']) && !empty($file['directory']))
  232. ? $file['directory'] . DS . $file['filename']
  233. : $file['filename'];
  234. return $this->_getResource()
  235. ->saveFile($filename, $file['content'], $overwrite);
  236. } catch (Exception $e) {
  237. Mage::logException($e);
  238. Mage::throwException(Mage::helper('core')->__('Unable to save file "%s" at "%s"', $file['filename'], $file['directory']));
  239. }
  240. } else {
  241. Mage::throwException(Mage::helper('core')->__('Wrong file info format'));
  242. }
  243. return false;
  244. }
  245. }