]> _ Git - cubist_util.git/commitdiff
wip #4666 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 13 Sep 2021 15:58:30 +0000 (17:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 13 Sep 2021 15:58:30 +0000 (17:58 +0200)
src/Files/Files.php
src/Files/VirtualDirectory.php

index 8335387f21b201de534a81748f4cd6f127d212a4..764b07b269e670e4c6126d1206b889a75962a75a 100644 (file)
@@ -28,6 +28,7 @@ class Files
             return;
         }
         mkdir($path, $mode, $recursive);
+        return realpath($path);
     }
 
     public static function humanReadableSize($size, $precision = 2)
@@ -35,7 +36,7 @@ class Files
         $base = log($size) / log(1024);
         $suffixes = array('', 'k', 'M', 'G', 'T');
 
-        return round(pow(1024, $base - floor($base)), $precision) . $suffixes[intval(floor($base))];
+        return round(1024 ** ($base - floor($base)), $precision) . $suffixes[(int)floor($base)];
     }
 
     /**
index 35d8de2bbd1eca6f16f537ce8d450305374fbc58..ac312e48efa6696ae8b00a75c3b63ccc951ae007 100644 (file)
 <?php
 
 namespace Cubist\Util\Files;
+use SplFileInfo;
+
 class VirtualDirectory
 {
 
-       protected $_path;
-       protected $_copy;
-       protected $_directories;
-       protected $_contents;
-       protected $_tmp;
-       protected $_dirs = array();
-
-       public function __construct($path)
-       {
-               if (!file_exists($path)) {
-                       mkdir($path, 0777, true);
-                       $path = realpath($path);
-               }
-
-               $this->_path = $path;
-               $this->_copy = array();
-               $this->_contents = array();
-               $this->_directories = array();
-               $this->_tmp = array();
-       }
-
-       public function copy($from, $to)
-       {
-               $realto = $this->path($to);
-               if (!$realto) {
-                       return;
-               }
-               $this->_copy[$realto] = $from;
-               return $this;
-       }
-
-       public function copyDirectory($from, $to)
-       {
-               $this->_directories[$this->path($to)] = $from;
-               return $this;
-       }
-
-       public function file_put_contents($to, $data)
-       {
-               $this->_contents[$this->path($to)] = $data;
-               return $this;
-       }
-
-       public function sync($delete = false)
-       {
-               $existing = array();
-
-               foreach ($this->_directories as $to => $from) {
-                       $this->_addDirectory($from, $to);
-               }
-
-               // Create dirs before copying
-               foreach ($this->_dirs as $dir => $t) {
-                       if (!file_exists($dir)) {
-                               mkdir($dir, 0777, true);
-                       }
-               }
-
-               foreach ($this->_contents as $to => $data) {
-                       if ($delete) {
-                               $existing[$to] = true;
-                       }
-
-                       file_put_contents($to, $data);
-               }
-
-               foreach ($this->_copy as $to => $from) {
-                       if (!file_exists($from)) {
-                               continue;
-                       }
-                       if ($delete) {
-                               $existing[$to] = true;
-                       }
-                       if (!file_exists($to) || filesize($from) != filesize($to) || filemtime($from) > filemtime($to)) {
-                               copy($from, $to);
-                       }
-               }
-
-               if ($delete) {
-                       $this->_delete($existing);
-               }
-               $this->cleanTemp();
-       }
-
-       protected function _addDirectory($from, $to)
-       {
-               if (!file_exists($from)) {
-                       return;
-               }
-
-               $from = realpath($from);
-               $files = Files::getRecursiveDirectoryIterator($from);
-
-               foreach ($files as $file) {
-                       /* @var $file SplFileInfo */
-                       if ($file->isDir()) {
-                               continue;
-                       }
-                       $path = $file->getRealPath();
-                       $dest = str_replace($from, '', $path);
-                       $this->copy($path, $this->relativePath($to) . '/' . ltrim($dest, '/'));
-               }
-       }
-
-       protected function _delete($existing = array())
-       {
-               $files = Files::getRecursiveDirectoryIterator($this->_path);
-               foreach ($files as $file) {
-                       if ($file->isDir()) {
-                               continue;
-                       }
-                       $path = $file->getRealPath();
-                       if (!isset($existing[$path])) {
-                               unlink($path);
-                       }
-               }
-       }
-
-       public function path($local)
-       {
-               $res = str_replace('//', '/',
-                       $this->_path . '/' . ltrim($local, '/'));
-               $this->_dirs[dirname($res)] = true;
-               return $res;
-       }
-
-       public function relativePath($absolute)
-       {
-               return str_replace($this->_path, '', $absolute);
-       }
-
-       public function cleanTemp()
-       {
-               foreach ($this->_tmp as $tmp) {
-                       if (is_file($tmp)) {
-                               unlink($tmp);
-                       }
-                       if (is_dir($tmp)) {
-                               Files::rmdir($tmp);
-                       }
-               }
-       }
-
-       public function addTemp($temp)
-       {
-               $this->_tmp[] = $temp;
-       }
+    protected $_path;
+    protected $_copy;
+    protected $_directories;
+    protected $_contents;
+    protected $_tmp;
+    protected $_dirs = array();
+
+    public function __construct($path)
+    {
+        $this->_path = Files::mkdir($path);
+        $this->_copy = array();
+        $this->_contents = array();
+        $this->_directories = array();
+        $this->_tmp = array();
+    }
+
+    public function copy($from, $to)
+    {
+        $realto = $this->path($to);
+        if (!$realto) {
+            return $this;
+        }
+        $this->_copy[$realto] = $from;
+        return $this;
+    }
+
+    public function copyDirectory($from, $to)
+    {
+        $this->_directories[$this->path($to)] = $from;
+        return $this;
+    }
+
+    public function file_put_contents($to, $data)
+    {
+        $this->_contents[$this->path($to)] = $data;
+        return $this;
+    }
+
+    public function sync($delete = false)
+    {
+        $existing = array();
+
+        foreach ($this->_directories as $to => $from) {
+            $this->_addDirectory($from, $to);
+        }
+
+        // Create dirs before copying
+        foreach ($this->_dirs as $dir => $t) {
+            Files::mkdir($dir);
+        }
+
+        foreach ($this->_contents as $to => $data) {
+            if ($delete) {
+                $existing[$to] = true;
+            }
+
+            file_put_contents($to, $data);
+        }
+
+        foreach ($this->_copy as $to => $from) {
+            if (!file_exists($from)) {
+                continue;
+            }
+            if ($delete) {
+                $existing[$to] = true;
+            }
+            if (!file_exists($to) || filesize($from) != filesize($to) || filemtime($from) > filemtime($to)) {
+                copy($from, $to);
+            }
+        }
+
+        if ($delete) {
+            $this->_delete($existing);
+        }
+        $this->cleanTemp();
+    }
+
+    protected function _addDirectory($from, $to)
+    {
+        if (!file_exists($from)) {
+            return;
+        }
+
+        $from = realpath($from);
+        $files = Files::getRecursiveDirectoryIterator($from);
+
+        foreach ($files as $file) {
+            /* @var $file SplFileInfo */
+            if ($file->isDir()) {
+                continue;
+            }
+            $path = $file->getRealPath();
+            $dest = str_replace($from, '', $path);
+            $this->copy($path, $this->relativePath($to) . '/' . ltrim($dest, '/'));
+        }
+    }
+
+    protected function _delete($existing = array())
+    {
+        $files = Files::getRecursiveDirectoryIterator($this->_path);
+        foreach ($files as $file) {
+            if ($file->isDir()) {
+                continue;
+            }
+            $path = $file->getRealPath();
+            if (!isset($existing[$path])) {
+                unlink($path);
+            }
+        }
+    }
+
+    public function path($local)
+    {
+        $res = str_replace('//', '/',
+            $this->_path . '/' . ltrim($local, '/'));
+        $this->_dirs[dirname($res)] = true;
+        return $res;
+    }
+
+    public function relativePath($absolute)
+    {
+        return str_replace($this->_path, '', $absolute);
+    }
+
+    public function cleanTemp()
+    {
+        foreach ($this->_tmp as $tmp) {
+            if (is_file($tmp)) {
+                unlink($tmp);
+            }
+            if (is_dir($tmp)) {
+                Files::rmdir($tmp);
+            }
+        }
+    }
+
+    public function addTemp($temp)
+    {
+        $this->_tmp[] = $temp;
+    }
 }
\ No newline at end of file