From f84fd11c86f6cf5ca27c5b427fa81e8f84bea88f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 12 Mar 2016 15:05:54 +0100 Subject: [PATCH] File handler draft --- System/File/Directory.php | 147 +++++++++++++++++++++++++++++++++++ System/File/File.php | 46 +++++++++++ System/File/FileAbstract.php | 65 ++++++++++++++++ System/NullFile.php | 34 ++++++++ 4 files changed, 292 insertions(+) create mode 100644 System/File/Directory.php create mode 100644 System/File/File.php create mode 100644 System/File/FileAbstract.php create mode 100644 System/NullFile.php diff --git a/System/File/Directory.php b/System/File/Directory.php new file mode 100644 index 000000000..ee0072ffa --- /dev/null +++ b/System/File/Directory.php @@ -0,0 +1,147 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\System; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class Directory extends FileAbstract implements Iterator, ArrayAccess +{ + private $path = ''; + private $name = 'new_directory'; + private $count = 0; + private $size = 0; + private $filter = '*'; + private $nodes = []; + + public function __construct(string $path, string $filter = '*') + { + $this->filter = $filter; + parent::__constrct($path); + } + + public function create() + { + // todo: implement? + } + + public function get(string $name) : FileInterface + { + return $this->node[$name] ?? new NullFile(); + } + + public function add(FileInterface $file) + { + $this->count += $file->getCount(); + $this->size += $file->getSize(); + $this->nodes[$this->getName()] = $file; + } + + public function remove(string $name) : bool + { + if(isset($this->nodes[$name])) { + $this->count -= $this->nodes[$name]->getCount(); + $this->size -= $this->nodes[$name]->getSize(); + + unset($this->nodes[$name]); + // todo: unlink??? + + return true; + } + + return false; + } + + public function index() + { + foreach (glob($this->path . DIRECTORY_SEPARATOR . $this->filter) as $filename) { + // todo: handle . and ..???!!! + if(is_dir($filename)) { + $file = new Directory($filename); + $file->index(); + } else { + $file = new File($filename); + } + + $this->add($file); + } + } + + /* Iterator */ + public function rewind() + { + reset($this->node); + } + + public function current() + { + return current($this->node); + } + + public function key() + { + return key($this->node); + } + + public function next() + { + return next($this->node); + } + + public function valid() + { + $key = key($this->node); + + return ($key !== null && $key !== false); + } + + /* ArrayAccess */ + public function offsetSet(string $offset, FileInterface $value) + { + if (is_null($offset)) { + $this->add($value) + } else { + $this->node[$offset] = $value; + } + } + + public function offsetExists(string $offset) + { + return isset($this->node[$offset]); + } + + public function offsetUnset(string $offset) + { + if(isset($this->node[$offset])) { + unset($this->node[$offset]); + } + } + + public function offsetGet(string $offset) + { + return $this->node[$offset] ?? null; + } +} \ No newline at end of file diff --git a/System/File/File.php b/System/File/File.php new file mode 100644 index 000000000..f7e8254a5 --- /dev/null +++ b/System/File/File.php @@ -0,0 +1,46 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\System; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class File extends FileAbstract +{ + public function __construct(string $path) + { + parent::__constrct($path); + + if(file_exists($this->path)) { + $this->index(); + } + } + + public function index() + { + $this->size = filesize($this->path); + } +} \ No newline at end of file diff --git a/System/File/FileAbstract.php b/System/File/FileAbstract.php new file mode 100644 index 000000000..62da287db --- /dev/null +++ b/System/File/FileAbstract.php @@ -0,0 +1,65 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\System\File; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class FileAbstract +{ + private $path = ''; + private $name = 'new_directory'; + private $count = 0; + private $size = 0; + + public function __construct(string $path) + { + $this->path = $path; + $this->name = basename($path); + } + + public function getCount() : int + { + return $this->count; + } + + public function getSize() : int + { + return $this->size; + } + + public function getName() : string + { + return $this->name; + } + + public function getPath() : string + { + return $this->path; + } + + abstract private function index(); +} \ No newline at end of file diff --git a/System/NullFile.php b/System/NullFile.php new file mode 100644 index 000000000..ff50ce30e --- /dev/null +++ b/System/NullFile.php @@ -0,0 +1,34 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\System; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category System + * @package Framework + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +class NullFile extends File +{ + +} \ No newline at end of file