PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Storage/Storage.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 382 lines | 333 code | 45 blank | 4 comment | 51 complexity | 0a780ebc1772563b922ac760a544bdf3 MD5 | raw file
  1. <?php
  2. /**
  3. * Storage class
  4. */
  5. class Storage
  6. {
  7. public $disk;
  8. protected $storagePath;
  9. protected $basePath;
  10. function __construct($disk=null)
  11. {
  12. if(empty($disk))
  13. {
  14. if($this->checkDiskExiste(Config::get('storage.default')))
  15. {
  16. $this->disk=Config::get('storage.default');
  17. $this->basePath=Sys::$app."/storage/file";
  18. $this->storagePath=$this->basePath."/".Config::get('storage.default');
  19. }
  20. else throw new invalidArgumentException("There is no disk call's ".Config::get('storage.default'));
  21. }
  22. else
  23. {
  24. if($this->checkDiskExiste($disk))
  25. {
  26. $this->disk=$disk;
  27. $this->basePath=Sys::$app."/storage/file";
  28. $this->storagePath=$this->basePath."/".$disk;
  29. }
  30. else throw new invalidArgumentException("There is no disk call's ".$disk);
  31. }
  32. }
  33. protected function checkDiskExiste($value)
  34. {
  35. return is_dir(Sys::$app."/storage/file/".$value);
  36. }
  37. protected function isDir($value)
  38. {
  39. return is_dir($this->storagePath."/".$value);
  40. }
  41. protected function path($value)
  42. {
  43. return Sys::$app."/storage/file/".$this->disk."/".$value;
  44. }
  45. public static function disk($name)
  46. {
  47. $self=new self($name);
  48. return $self;
  49. }
  50. public static function createDisk($name)
  51. {
  52. $self=new self();
  53. if(!$self->checkDiskExiste($name)) mkdir($self->basePath."/".$name, 0777, true);
  54. $self=new self($name);
  55. return $self;
  56. }
  57. protected function putNonStatic($name,$content)
  58. {
  59. $myfile = fopen($this->storagePath."/".$name, "w");
  60. fwrite($myfile, $content);
  61. fclose($myfile);
  62. }
  63. protected static function putStatic($name,$content)
  64. {
  65. $self=new self();
  66. $myfile = fopen($self->storagePath."/".$name, "w");
  67. fwrite($myfile, $content);
  68. fclose($myfile);
  69. }
  70. protected function existsNonStatic($name)
  71. {
  72. return file_exists($this->storagePath."/".$name);
  73. }
  74. protected static function existsStatic($name,$content)
  75. {
  76. $self=new self();
  77. return file_exists($self->storagePath."/".$name);
  78. }
  79. protected function getNonStatic($name)
  80. {
  81. return file_get_contents($this->storagePath."/".$name);
  82. }
  83. protected static function getStatic($name)
  84. {
  85. $self=new self();
  86. return file_get_contents($self->storagePath."/".$name);
  87. }
  88. protected function prependNonStatic($name,$content)
  89. {
  90. if($this->exists($name))
  91. {
  92. $oldContent=$this->get($name);
  93. $this->put($name,($content.$oldContent));
  94. }
  95. else throw new InvalidArgumentException("There is no file calls $name to prepend");
  96. }
  97. protected static function prependStatic($name,$content)
  98. {
  99. $self=new self();
  100. if($self->exists($name))
  101. {
  102. $oldContent=$self->get($name);
  103. $self->put($name,($content.$oldContent));
  104. }
  105. else throw new InvalidArgumentException("There is no file calls $name");
  106. }
  107. protected function appendNonStatic($name,$content)
  108. {
  109. if($this->exists($name))
  110. {
  111. $oldContent=$this->get($name);
  112. $this->put($name,($oldContent.$content));
  113. }
  114. else throw new InvalidArgumentException("There is no file calls $name");
  115. }
  116. protected static function appendStatic($name,$content)
  117. {
  118. $self=new self();
  119. if($self->exists($name))
  120. {
  121. $oldContent=$self->get($name);
  122. $self->put($name,($oldContent.$content));
  123. }
  124. else throw new InvalidArgumentException("There is no file calls $name");
  125. }
  126. protected function sizeNonStatic($name)
  127. {
  128. if($this->exists($name))
  129. {
  130. return filesize($this->path($name));
  131. }
  132. else throw new InvalidArgumentException("There is no file calls $name");
  133. }
  134. protected static function sizeStatic($name)
  135. {
  136. $self=new self();
  137. if($self->exists($name))
  138. {
  139. return filesize($self->path($name));
  140. }
  141. else throw new InvalidArgumentException("There is no file calls $name");
  142. }
  143. protected function deleteNonStatic($name)
  144. {
  145. if($this->exists($name))
  146. {
  147. return unlink($this->path($name));
  148. }
  149. else throw new InvalidArgumentException("There is no file calls $name");
  150. }
  151. protected static function deleteStatic($name)
  152. {
  153. $self=new self();
  154. //
  155. if($self->exists($name))
  156. {
  157. return unlink ($self->path($name));
  158. }
  159. else throw new InvalidArgumentException("There is no file calls $name");
  160. }
  161. protected function copyNonStatic($name1,$name2)
  162. {
  163. if($this->exists($name))
  164. {
  165. $content=$this->get($name1);
  166. return $this->put($name2,$content);
  167. }
  168. else throw new InvalidArgumentException("There is no file calls $name");
  169. }
  170. protected static function copyStatic($name1,$name2)
  171. {
  172. $self=new self();
  173. if($self->exists($name))
  174. {
  175. $content=$self->get($name1);
  176. return $self->put($name2,$content);
  177. }
  178. else throw new InvalidArgumentException("There is no file calls $name");
  179. }
  180. protected function moveNonStatic($name1,$name2)
  181. {
  182. if($this->exists($name1))
  183. {
  184. $content=$this->get($name1);
  185. $this->put($name2,$content);
  186. $this->delete($name1);
  187. }
  188. else throw new InvalidArgumentException("There is no file calls $name");
  189. }
  190. protected static function moveStatic($name1,$name2)
  191. {
  192. $self=new self();
  193. if($self->exists($name))
  194. {
  195. $content=$self->get($name1);
  196. $self->put($name2,$content);
  197. $self->delete($name1);
  198. }
  199. else throw new InvalidArgumentException("There is no file calls $name");
  200. }
  201. protected function lasteditNonStatic($name)
  202. {
  203. if($this->exists($name))
  204. {
  205. return filemtime($this->path($name));
  206. }
  207. else throw new InvalidArgumentException("There is no file calls $name");
  208. }
  209. protected static function lasteditStatic($name)
  210. {
  211. $self=new self();
  212. if($self->exists($name))
  213. {
  214. return filemtime($self->path($name));
  215. }
  216. else throw new InvalidArgumentException("There is no file calls $name");
  217. }
  218. protected function filesNonStatic($name="")
  219. {
  220. if($this->isDir($name))
  221. {
  222. $files=array();
  223. $r=scandir($this->path($name));
  224. foreach ($r as $key => $value) {
  225. if(is_file($this->storagePath."/".$value)) $files[]=$value;
  226. }
  227. return $files;
  228. }
  229. else throw new InvalidArgumentException("There is no directory calls $name");
  230. }
  231. protected static function filesStatic($name="")
  232. {
  233. $self=new self();
  234. if($self->isDir($name))
  235. {
  236. $files=array();
  237. $r=scandir($self->path($name));
  238. foreach ($r as $key => $value) {
  239. if(is_file($self->storagePath."/".$value)) $files[]=$value;
  240. }
  241. return $files;
  242. }
  243. else throw new InvalidArgumentException("There is no directory calls $name");
  244. }
  245. protected function directorysNonStatic($name="")
  246. {
  247. if($this->isDir($name))
  248. {
  249. $files=array();
  250. $r=scandir($this->path($name));
  251. foreach ($r as $key => $value) {
  252. if(is_dir($this->storagePath."/".$value)) $files[]=$value;
  253. }
  254. return $files;
  255. }
  256. else throw new InvalidArgumentException("There is no directory calls $name");
  257. }
  258. protected static function directorysStatic($name="")
  259. {
  260. $self=new self();
  261. if($self->isDir($name))
  262. {
  263. $files=array();
  264. $r=scandir($self->path($name));
  265. foreach ($r as $key => $value) {
  266. if(is_dir($self->storagePath."/".$value)) $files[]=$value;
  267. }
  268. return $files;
  269. }
  270. else throw new InvalidArgumentException("There is no directory calls $name");
  271. }
  272. protected function makeDirNonStatic($name)
  273. {
  274. if($this->isDir($name))
  275. return mkdir($this->storagePath."/".$name);
  276. }
  277. protected static function makeDirStatic($name="")
  278. {
  279. $self=new self();
  280. return mkdir($self->storagePath."/".$name);
  281. }
  282. protected function deleteDirNonStatic($name="")
  283. {
  284. if($this->isDir($name))
  285. {
  286. return rmdir($self->storagePath."/".$name);
  287. }
  288. else throw new InvalidArgumentException("There is no directory calls $name");
  289. }
  290. protected static function deleteDirStatic($name="")
  291. {
  292. $self=new self();
  293. if($self->isDir($name))
  294. {
  295. return rmdir($self->storagePath."/".$name);
  296. }
  297. else throw new InvalidArgumentException("There is no directory calls $name");
  298. }
  299. public function __call($name, $args) {
  300. switch ($name) {
  301. case 'put': return call_user_func_array(array($this, "putNonStatic"), $args); break;
  302. case 'exists': return call_user_func_array(array($this, "existsNonStatic"), $args); break;
  303. case 'get': return call_user_func_array(array($this, "getNonStatic"), $args); break;
  304. case 'prepend': return call_user_func_array(array($this, "prependNonStatic"), $args); break;
  305. case 'append': return call_user_func_array(array($this, "appendNonStatic"), $args); break;
  306. case 'size': return call_user_func_array(array($this, "sizeNonStatic"), $args); break;
  307. case 'delete': return call_user_func_array(array($this, "deleteNonStatic"), $args); break;
  308. case 'copy': return call_user_func_array(array($this, "copyNonStatic"), $args); break;
  309. case 'move': return call_user_func_array(array($this, "moveNonStatic"), $args); break;
  310. case 'lastModified': return call_user_func_array(array($this, "lasteditNonStatic"), $args); break;
  311. case 'files': return call_user_func_array(array($this, "filesNonStatic"), $args); break;
  312. case 'directories': return call_user_func_array(array($this, "directorysNonStatic"), $args); break;
  313. case 'makeDirectory': return call_user_func_array(array($this, "makeDirNonStatic"), $args); break;
  314. case 'deleteDirectory': return call_user_func_array(array($this, "deleteDirNonStatic"), $args); break;
  315. default : throw new Whoops\Exception\ErrorException("Call to undefined method Storage::$name()");
  316. }
  317. }
  318. public static function __callStatic($name, $args) {
  319. switch ($name) {
  320. case 'put': return call_user_func_array(array("Storage", "putStatic"), $args); break;
  321. case 'exists': return call_user_func_array(array("Storage", "existsStatic"), $args); break;
  322. case 'get': return call_user_func_array(array("Storage", "getStatic"), $args); break;
  323. case 'prepend': return call_user_func_array(array("Storage", "prependStatic"), $args); break;
  324. case 'append': return call_user_func_array(array("Storage", "appendStatic"), $args); break;
  325. case 'size': return call_user_func_array(array("Storage", "sizeStatic"), $args); break;
  326. case 'delete': return call_user_func_array(array("Storage", "deleteStatic"), $args); break;
  327. case 'copy': return call_user_func_array(array("Storage", "copyStatic"), $args); break;
  328. case 'move': return call_user_func_array(array("Storage", "moveStatic"), $args); break;
  329. case 'lastModified': return call_user_func_array(array("Storage", "lasteditStatic"), $args); break;
  330. case 'files': return call_user_func_array(array("Storage", "filesStatic"), $args); break;
  331. case 'directories': return call_user_func_array(array("Storage", "directorysStatic"), $args); break;
  332. case 'makeDirectory': return call_user_func_array(array("Storage", "makeDirStatic"), $args); break;
  333. case 'deleteDirectory': return call_user_func_array(array("Storage", "deleteDirStatic"), $args); break;
  334. default : throw new Whoops\Exception\ErrorException("Call to undefined method Storage::$name()");
  335. }
  336. }
  337. }