PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/core/Filesystem/Filesystem.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 242 lines | 178 code | 45 blank | 19 comment | 26 complexity | bc9b430aa0698fcd3a51407e5e270e60 MD5 | raw file
  1. <?php
  2. namespace Fiesta\Filesystem;
  3. /**
  4. * File system
  5. */
  6. class Filesystem
  7. {
  8. public function exists($path)
  9. {
  10. return file_exists($path);
  11. }
  12. public function get($path)
  13. {
  14. if($this->exists($path)) return file_get_contents($path);
  15. else throw new FileNotFoundException("File does not existe in ($path)");
  16. }
  17. public function getRequire($path)
  18. {
  19. if($this->exists($path)) return require $path;
  20. else throw new FileNotFoundException("File does not existe in ($path)");
  21. }
  22. public function getRequireOnce($path)
  23. {
  24. if($this->exists($path)) return require_once $path;
  25. else throw new FileNotFoundException("File does not existe in ($path)");
  26. }
  27. public function put($path,$content,$lock=false)
  28. {
  29. // $myfile = fopen($path, "w");
  30. // fwrite($myfile, $content);
  31. // fclose($myfile);
  32. //
  33. return file_put_contents($path, $content, $lock ? LOCK_EX : 0);
  34. }
  35. public function prepend($path,$content)
  36. {
  37. if($this->exists($path))
  38. {
  39. $oldContent=$this->get($path);
  40. $this->put($name,($content.$oldContent));
  41. }
  42. else throw new FileNotFoundException("File does not existe in ($path)");
  43. }
  44. public function append($path,$content)
  45. {
  46. if($this->exists($path))
  47. {
  48. $oldContent=$this->get($path);
  49. $this->put($name,($oldContent.$content));
  50. }
  51. else throw new FileNotFoundException("File does not existe in ($path)");
  52. }
  53. public function delete($path)
  54. {
  55. $paths=is_array($path) ? $path : func_get_args() ;
  56. //
  57. $ok=true;
  58. foreach ($paths as $value) if( !unlink($path)) $ok=false;
  59. return $ok;
  60. }
  61. public function copy($from,$to)
  62. {
  63. if($this->exists($from))
  64. {
  65. $content=$this->get($from);
  66. return $this->put($to,$content);
  67. }
  68. else throw new FileNotFoundException("File does not existe in ($path)");
  69. }
  70. public function move($from,$to)
  71. {
  72. if($this->exists($from))
  73. {
  74. $content=$this->get($from);
  75. $this->put($to,$content);
  76. $this->delete($from);
  77. }
  78. else throw new FileNotFoundException("File does not existe in ($path)");
  79. }
  80. public function name($path)
  81. {
  82. return pathinfo($path, PATHINFO_FILENAME);
  83. }
  84. public function type($path)
  85. {
  86. return filetype($path);
  87. }
  88. public function baseName($path)
  89. {
  90. return basename($path);
  91. }
  92. public function extension($path)
  93. {
  94. return pathinfo($path, PATHINFO_EXTENSION);
  95. }
  96. public function size($path)
  97. {
  98. return filesize($path);
  99. }
  100. public function lastEdit($path)
  101. {
  102. return filemtime($path);
  103. }
  104. public function isDirectory($directory)
  105. {
  106. return is_dir($directory);
  107. }
  108. // public function isWritable($path)
  109. // {
  110. // return is_writable($path);
  111. // }
  112. public function isFile($file)
  113. {
  114. return is_file($file);
  115. }
  116. public function glob($pattern, $flags=0)
  117. {
  118. return glob($pattern, $flags);
  119. }
  120. public function all($directory)
  121. {
  122. $glob = glob($directory.'/*');
  123. if ($glob === false) return array();
  124. return $glob;
  125. }
  126. public function files($directory)
  127. {
  128. $glob = glob($directory.'/*');
  129. if ($glob === false) return array();
  130. return array_filter($glob, function($file)
  131. {
  132. //return filetype($file) == 'file';
  133. return $this->isFile($file);
  134. });
  135. }
  136. public function directories($directory)
  137. {
  138. $glob = glob($directory.'/*');
  139. if ($glob === false) return array();
  140. return array_filter($glob, function($file)
  141. {
  142. //return filetype($file) != 'file';
  143. return $this->isDirectory($file);
  144. });
  145. }
  146. public function makeDirectory($path,$mode=0755,$recursive = false)
  147. {
  148. return mkdir ( $path, $mode, $recursive);
  149. }
  150. public function copyDirectory($from, $to, $options=null)
  151. {
  152. if(! $this->isDirectory($from)) { throw new DirectoryNotFoundException("Directory does not existe in ($from)"); return false; }
  153. //
  154. if(! $this->isDirectory($to)) $this->makeDirectory($to,0777,true);
  155. //
  156. $options = $options ?: \FilesystemIterator::SKIP_DOTS;
  157. $items = new \FilesystemIterator($from, $options);
  158. //
  159. //$items=$this->all($from);
  160. //
  161. foreach ($items as $item) {
  162. $target=$to."/".$item->getBasename();
  163. if($this->isDirectory($item))
  164. {
  165. $path=$item->getPathname();
  166. if ( ! $this->copyDirectory($path, $target, $options)) break; // return false;
  167. }
  168. else
  169. {
  170. if ( ! $this->copy($item->getPathname(), $target)) break; //return false;
  171. }
  172. }
  173. return true;
  174. }
  175. public function deleteDirectory($directory, $preserve = false)
  176. {
  177. if ( ! $this->isDirectory($directory)) { throw new DirectoryNotFoundException("Directory does not existe in ($directory)"); return false; }
  178. $items = new \FilesystemIterator($directory);
  179. foreach ($items as $item)
  180. {
  181. if ($item->isDir())
  182. {
  183. $this->deleteDirectory($item->getPathname());
  184. }
  185. else
  186. {
  187. $this->delete($item->getPathname());
  188. }
  189. }
  190. if ( ! $preserve) @rmdir($directory);
  191. return true;
  192. }
  193. public function clearDirectory($path)
  194. {
  195. return $this->deleteDirectory($path, true);
  196. }
  197. }