File handler draft

This commit is contained in:
Dennis Eichhorn 2016-03-12 15:05:54 +01:00
parent 6020aba7a3
commit f84fd11c86
4 changed files with 292 additions and 0 deletions

147
System/File/Directory.php Normal file
View File

@ -0,0 +1,147 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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;
}
}

46
System/File/File.php Normal file
View File

@ -0,0 +1,46 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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);
}
}

View File

@ -0,0 +1,65 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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();
}

34
System/NullFile.php Normal file
View File

@ -0,0 +1,34 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class NullFile extends File
{
}