From: Vincent Vanwaelscappel Date: Mon, 13 Sep 2021 15:58:30 +0000 (+0200) Subject: wip #4666 @0.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=68279da429ce0b6b7488797541eab9766756910e;p=cubist_util.git wip #4666 @0.5 --- diff --git a/src/Files/Files.php b/src/Files/Files.php index 8335387..764b07b 100644 --- a/src/Files/Files.php +++ b/src/Files/Files.php @@ -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)]; } /** diff --git a/src/Files/VirtualDirectory.php b/src/Files/VirtualDirectory.php index 35d8de2..ac312e4 100644 --- a/src/Files/VirtualDirectory.php +++ b/src/Files/VirtualDirectory.php @@ -1,154 +1,149 @@ _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