<?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