Cleanup and documentation

This commit is contained in:
Dennis Eichhorn 2016-03-28 11:20:47 +02:00
parent 98ff5af05b
commit f2b017a5b3
9 changed files with 497 additions and 137 deletions

View File

@ -15,7 +15,7 @@
*/
namespace phpOMS\DataStorage\Cache;
use phpOMS\System\FileSystem;
use phpOMS\System\File\Directory;
/**
* MemCache class.
@ -98,7 +98,7 @@ class FileCache implements CacheInterface
public function stats() : array
{
$stats = [];
$stats['count'] = FileSystem::getFileCount(self::CACHE_PATH);
$stats['count'] = Directory::getFileCount(self::CACHE_PATH);
// size, avg. last change compared to now

View File

@ -104,12 +104,12 @@ class FileLogger implements LoggerInterface
}
if (is_dir($lpath) || strpos($lpath, '.') === false) {
Directory::create($lpath, 0644);
File::create($path = $lpath . '/' . date('Y-m-d') . '.log');
Directory::createPath($lpath, 0644);
File::createFile($path = $lpath . '/' . date('Y-m-d') . '.log');
$path = realpath($path);
} else {
File::create($lpath);
File::createFile($lpath);
$path = realpath($lpath);
}

View File

@ -30,13 +30,108 @@ namespace phpOMS\System\File;
*/
class Directory extends FileAbstract implements \Iterator, \ArrayAccess
{
/**
* Direcotry list filter.
*
* @var string
* @since 1.0.0
*/
private $filter = '*';
/**
* Direcotry nodes (files and directories).
*
* @var string
* @since 1.0.0
*/
private $nodes = [];
public static function create(string $path, int $permission = 0644, bool $recursive = false) : bool
/**
* Get file count inside path.
*
* @param string $path Path to folder
* @param bool $recursive Should sub folders be counted as well?
* @param array $ignore Ignore these sub-paths
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getFileCount(string $path, bool $recursive = true, array $ignore = ['.', '..', 'cgi-bin',
'.DS_Store'])
{
$size = 0;
$files = scandir($path);
foreach ($files as $t) {
if (in_array($t, $ignore)) {
continue;
}
if (is_dir(rtrim($path, '/') . '/' . $t)) {
if ($recursive) {
$size += self::getFileCount(rtrim($path, '/') . '/' . $t, true, $ignore);
}
} else {
$size++;
}
}
return $size;
}
/**
* Delete directory and all its content.
*
* @param string $path Path to folder
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function deletePath($path) : bool
{
$path = realpath($oldPath = $path);
if ($path === false || !is_dir($path) || Validator::startsWith($path, ROOT_PATH)) {
throw new PathException($oldPath);
}
$files = scandir($path);
/* Removing . and .. */
unset($files[1]);
unset($files[0]);
foreach ($files as $file) {
if (is_dir($file)) {
self::deletePath($file);
} else {
unlink($file);
}
}
rmdir($path);
return true;
}
/**
* Create directory.
*
* @param string $path Path
* @param int $permission Directory permission
* @param bool $recursive Create parent directories if applicable
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function createPath(string $path, int $permission = 0644, bool $recursive = false) : bool
{
if($recursive && !file_exists($parent = self::getParent($path))) {
self::create($parent, $permission, $recursive);
self::createPath($parent, $permission, $recursive);
}
if (!file_exists($path)) {
@ -45,14 +140,23 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
return true;
} else {
throw new PathException($path);
throw new PermissionException($path);
}
}
return false;
}
/**
* Get parent directory path.
*
* @param string $path Path
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getParent(string $path) : string
{
$path = explode('/', str_replace('\\', '/', $path));
@ -61,6 +165,15 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
return implode('/', $path);
}
/**
* Constructor.
*
* @param string $path Path
* @param string $filter Filter
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(string $path, string $filter = '*')
{
$this->filter = $filter;
@ -71,18 +184,62 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
}
}
/**
* Get node by name.
*
* @param string $name File/direcotry name
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function get(string $name) : FileAbstract
{
return $this->nodes[$name] ?? new NullFile();
}
public function add(FileAbstract $file)
/**
* Add file or directory.
*
* @param FileAbstract $file File to add
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function add(FileAbstract $file) : bool
{
$this->count += $file->getCount();
$this->size += $file->getSize();
$this->nodes[$this->getName()] = $file;
return $file->createNode();
}
/**
* {@inheritdoc}
*/
private function createNode() : bool
{
return self::createPath($this->path, $this->permission, true);
}
/**
* {@inheritdoc}
*/
private function removeNode() : bool
{
return true;
}
/**
* Remove by name.
*
* @param string $name Name to remove
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function remove(string $name) : bool
{
if(isset($this->nodes[$name])) {
@ -98,6 +255,14 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
return false;
}
/**
* Index directory.
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function index()
{
parent::index();
@ -116,26 +281,41 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
}
/* Iterator */
/**
* {@inheritdoc}
*/
public function rewind()
{
reset($this->nodes);
}
/**
* {@inheritdoc}
*/
public function current()
{
return current($this->nodes);
}
/**
* {@inheritdoc}
*/
public function key()
{
return key($this->nodes);
}
/**
* {@inheritdoc}
*/
public function next()
{
return next($this->nodes);
}
/**
* {@inheritdoc}
*/
public function valid()
{
$key = key($this->nodes);
@ -144,6 +324,9 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
}
/* ArrayAccess */
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
@ -153,11 +336,17 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
}
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return isset($this->nodes[$offset]);
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
if(isset($this->nodes[$offset])) {
@ -165,6 +354,9 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
}
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->nodes[$offset] ?? null;

View File

@ -31,7 +31,17 @@ namespace phpOMS\System\File;
class File extends FileAbstract
{
public static function create(string $path) : bool
/**
* Create file.
*
* @param string $path Path
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function createFile(string $path) : bool
{
if (!file_exists($path)) {
if(is_writable(Directory::getParent($path))) {
@ -39,13 +49,21 @@ class File extends FileAbstract
return true;
} else {
throw new PathException($path);
throw new PermissionException($path);
}
}
return false;
}
/**
* Constructor.
*
* @param string $path Path
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(string $path)
{
parent::__construct($path);
@ -56,6 +74,56 @@ class File extends FileAbstract
}
}
/**
* {@inheritdoc}
*/
private function createNode() : bool
{
return self::create($this->path);
}
/**
* {@inheritdoc}
*/
private function removeNode() : bool
{
return true;
}
/**
* Get file content.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getContent() : string
{
return file_get_contents($this->path);
}
/**
* Set file content.
*
* @param string $content Content to set
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setContent(string $content)
{
file_put_contents($this->path, $content);
}
/**
* Index file.
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function index()
{
parent::index();

View File

@ -30,15 +30,78 @@ namespace phpOMS\System\File;
*/
abstract class FileAbstract
{
/**
* Path.
*
* @var string
* @since 1.0.0
*/
protected $path = '';
protected $name = 'new_directory';
protected $count = 0;
protected $size = 0;
private $createdAt = null;
private $changedAt = null;
private $owner = 0;
private $permission = '0000';
/**
* Name.
*
* @var string
* @since 1.0.0
*/
protected $name = 'new_directory';
/**
* Directory/File count.
*
* @var int
* @since 1.0.0
*/
protected $count = 0;
/**
* Directory/Filesize in bytes.
*
* @var int
* @since 1.0.0
*/
protected $size = 0;
/**
* Created at.
*
* @var \DateTime
* @since 1.0.0
*/
protected $createdAt = null;
/**
* Last changed at.
*
* @var \DateTime
* @since 1.0.0
*/
protected $changedAt = null;
/**
* Owner.
*
* @var int
* @since 1.0.0
*/
protected $owner = 0;
/**
* Permission.
*
* @var string
* @since 1.0.0
*/
protected $permission = '0000';
/**
* Constructor.
*
* @param string $path Path
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(string $path)
{
$this->path = $path;
@ -48,51 +111,141 @@ abstract class FileAbstract
$this->changedAt = new \DateTime('now');
}
/**
* Get directory/file count.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCount() : int
{
return $this->count;
}
/**
* Get directory/file size.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getSize() : int
{
return $this->size;
}
/**
* Get name.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getName() : string
{
return $this->name;
}
/**
* Get path.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getPath() : string
{
return $this->path;
}
/**
* Get parent directory.
*
* @return Directory
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function parent() : Directory
{
return new Directory(Directory::getParent($this->path));
}
/**
* Create file/directory.
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
abstract private function createNode() : bool;
/**
* Get created at.
*
* @return \DateTime
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCreatedAt() : \DateTime
{
return $this->createdAt;
}
/**
* Get last changed at.
*
* @return \DateTime
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getChangedAt() : \DateTime
{
return $this->changedAt;
}
/**
* Get owner.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getOwner() : int
{
return $this->owner;
}
/**
* Get permission.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getPermission() : string
{
return $this->permission;
}
/**
* (Re-)Index path.
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function index()
{
$this->createdAt->setTimestamp(filemtime($this->path));

View File

@ -16,9 +16,7 @@
namespace phpOMS\System\File;
/**
* Filesystem class.
*
* Performing operations on the file system
* Path exception class.
*
* @category System
* @package Framework

View File

@ -0,0 +1,45 @@
<?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;
/**
* Permission exception class.
*
* @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 PermissionException extends \RuntimeException
{
/**
* Constructor.
*
* @param string $message Exception message
* @param int $code Exception code
* @param \Exception Previous exception
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(string $message, int $code = 0, \Exception $previous = null)
{
parent::__construct('Insufficient permissions for "' . $message . '" operations.', $code, $previous);
}
}

View File

@ -1,115 +0,0 @@
<?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;
use phpOMS\Validation\Validator;
/**
* 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 FileSystem
{
/**
* Constructor.
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
private function __construct()
{
}
/**
* Get file count inside path.
*
* @param string $path Path to folder
* @param bool $recursive Should sub folders be counted as well?
* @param array $ignore Ignore these sub-paths
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getFileCount(string $path, bool $recursive = true, array $ignore = ['.', '..', 'cgi-bin',
'.DS_Store'])
{
$size = 0;
$files = scandir($path);
foreach ($files as $t) {
if (in_array($t, $ignore)) {
continue;
}
if (is_dir(rtrim($path, '/') . '/' . $t)) {
if ($recursive) {
$size += self::getFileCount(rtrim($path, '/') . '/' . $t, true, $ignore);
}
} else {
$size++;
}
}
return $size;
}
/**
* Delete directory and all its content.
*
* @param string $path Path to folder
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function deletePath($path) : bool
{
$path = realpath($oldPath = $path);
if ($path === false || !is_dir($path) || Validator::startsWith($path, ROOT_PATH)) {
throw new PathException($oldPath);
}
$files = scandir($path);
/* Removing . and .. */
unset($files[1]);
unset($files[0]);
foreach ($files as $file) {
if (is_dir($file)) {
self::deletePath($file);
} else {
unlink($file);
}
}
rmdir($path);
return true;
}
}

View File

@ -15,7 +15,26 @@
*/
namespace phpOMS\System;
/**
* Operating system class.
*
* @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
*/
final class OperatingSystem {
/**
* Get OS.
*
* @return int|SystemType
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getSystem() : int
{
switch(PHP_OS) {