FileSystem.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace FD3\DevOps;
  3. use \FilesystemIterator;
  4. class FileSystem {
  5. protected $path;
  6. function __construct ($path = "."){
  7. $this->path = $path;
  8. }
  9. function dirExists($mode = 0777, $recursive = true ){
  10. if(!is_dir($this->path)){
  11. mkdir($this->path, $mode, $recursive);
  12. }
  13. return $this;
  14. }
  15. function realPath(){
  16. return new FileSystem(realpath($this->path));
  17. }
  18. function file($name){
  19. $newpath = $this->path . "/". $name;
  20. return new FileObject(basename($newpath), new FileSystem(dirname($newpath)));
  21. }
  22. function getPath(){ return $this->path; }
  23. }
  24. use WriteiniFile\WriteiniFile;
  25. class FileObject{
  26. protected $fileName;
  27. protected $context;
  28. function __construct($fileName, FileSystem $fs = null){
  29. $this->fileName = $fileName;
  30. if(!$fs){
  31. $fs = new FileSystem(getcwd());
  32. }
  33. $this->context = $fs;
  34. }
  35. function getPath(){
  36. return $this->context->dirExists()->realpath()->getPath() . "/".$this->fileName;
  37. }
  38. function content($content){
  39. $full_path = $this->getPath();
  40. file_put_contents($full_path, $content);
  41. return $this;
  42. }
  43. function writeIniConfig($config){
  44. $f = new WriteiniFile($this->getPath());
  45. $f->create($config);
  46. $f->write();
  47. return $this;
  48. }
  49. }