mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-11 22:38:42 +00:00
Cleanup and documentation
This commit is contained in:
parent
98ff5af05b
commit
f2b017a5b3
|
|
@ -15,7 +15,7 @@
|
||||||
*/
|
*/
|
||||||
namespace phpOMS\DataStorage\Cache;
|
namespace phpOMS\DataStorage\Cache;
|
||||||
|
|
||||||
use phpOMS\System\FileSystem;
|
use phpOMS\System\File\Directory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* MemCache class.
|
* MemCache class.
|
||||||
|
|
@ -98,7 +98,7 @@ class FileCache implements CacheInterface
|
||||||
public function stats() : array
|
public function stats() : array
|
||||||
{
|
{
|
||||||
$stats = [];
|
$stats = [];
|
||||||
$stats['count'] = FileSystem::getFileCount(self::CACHE_PATH);
|
$stats['count'] = Directory::getFileCount(self::CACHE_PATH);
|
||||||
|
|
||||||
// size, avg. last change compared to now
|
// size, avg. last change compared to now
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -104,12 +104,12 @@ class FileLogger implements LoggerInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_dir($lpath) || strpos($lpath, '.') === false) {
|
if (is_dir($lpath) || strpos($lpath, '.') === false) {
|
||||||
Directory::create($lpath, 0644);
|
Directory::createPath($lpath, 0644);
|
||||||
File::create($path = $lpath . '/' . date('Y-m-d') . '.log');
|
File::createFile($path = $lpath . '/' . date('Y-m-d') . '.log');
|
||||||
|
|
||||||
$path = realpath($path);
|
$path = realpath($path);
|
||||||
} else {
|
} else {
|
||||||
File::create($lpath);
|
File::createFile($lpath);
|
||||||
|
|
||||||
$path = realpath($lpath);
|
$path = realpath($lpath);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,13 +30,108 @@ namespace phpOMS\System\File;
|
||||||
*/
|
*/
|
||||||
class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Direcotry list filter.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
private $filter = '*';
|
private $filter = '*';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Direcotry nodes (files and directories).
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
private $nodes = [];
|
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))) {
|
if($recursive && !file_exists($parent = self::getParent($path))) {
|
||||||
self::create($parent, $permission, $recursive);
|
self::createPath($parent, $permission, $recursive);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!file_exists($path)) {
|
if (!file_exists($path)) {
|
||||||
|
|
@ -45,14 +140,23 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
throw new PermissionException($path);
|
||||||
throw new PathException($path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
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
|
public static function getParent(string $path) : string
|
||||||
{
|
{
|
||||||
$path = explode('/', str_replace('\\', '/', $path));
|
$path = explode('/', str_replace('\\', '/', $path));
|
||||||
|
|
@ -61,6 +165,15 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
return implode('/', $path);
|
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 = '*')
|
public function __construct(string $path, string $filter = '*')
|
||||||
{
|
{
|
||||||
$this->filter = $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
|
public function get(string $name) : FileAbstract
|
||||||
{
|
{
|
||||||
return $this->nodes[$name] ?? new NullFile();
|
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->count += $file->getCount();
|
||||||
$this->size += $file->getSize();
|
$this->size += $file->getSize();
|
||||||
$this->nodes[$this->getName()] = $file;
|
$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
|
public function remove(string $name) : bool
|
||||||
{
|
{
|
||||||
if(isset($this->nodes[$name])) {
|
if(isset($this->nodes[$name])) {
|
||||||
|
|
@ -98,6 +255,14 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index directory.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
parent::index();
|
parent::index();
|
||||||
|
|
@ -116,26 +281,41 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Iterator */
|
/* Iterator */
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function rewind()
|
public function rewind()
|
||||||
{
|
{
|
||||||
reset($this->nodes);
|
reset($this->nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function current()
|
public function current()
|
||||||
{
|
{
|
||||||
return current($this->nodes);
|
return current($this->nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function key()
|
public function key()
|
||||||
{
|
{
|
||||||
return key($this->nodes);
|
return key($this->nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function next()
|
public function next()
|
||||||
{
|
{
|
||||||
return next($this->nodes);
|
return next($this->nodes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function valid()
|
public function valid()
|
||||||
{
|
{
|
||||||
$key = key($this->nodes);
|
$key = key($this->nodes);
|
||||||
|
|
@ -144,6 +324,9 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ArrayAccess */
|
/* ArrayAccess */
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function offsetSet($offset, $value)
|
public function offsetSet($offset, $value)
|
||||||
{
|
{
|
||||||
if (is_null($offset)) {
|
if (is_null($offset)) {
|
||||||
|
|
@ -153,11 +336,17 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function offsetExists($offset)
|
public function offsetExists($offset)
|
||||||
{
|
{
|
||||||
return isset($this->nodes[$offset]);
|
return isset($this->nodes[$offset]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function offsetUnset($offset)
|
public function offsetUnset($offset)
|
||||||
{
|
{
|
||||||
if(isset($this->nodes[$offset])) {
|
if(isset($this->nodes[$offset])) {
|
||||||
|
|
@ -165,6 +354,9 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
public function offsetGet($offset)
|
public function offsetGet($offset)
|
||||||
{
|
{
|
||||||
return $this->nodes[$offset] ?? null;
|
return $this->nodes[$offset] ?? null;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,17 @@ namespace phpOMS\System\File;
|
||||||
class File extends FileAbstract
|
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 (!file_exists($path)) {
|
||||||
if(is_writable(Directory::getParent($path))) {
|
if(is_writable(Directory::getParent($path))) {
|
||||||
|
|
@ -39,13 +49,21 @@ class File extends FileAbstract
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
throw new PathException($path);
|
throw new PermissionException($path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param string $path Path
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function __construct(string $path)
|
public function __construct(string $path)
|
||||||
{
|
{
|
||||||
parent::__construct($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()
|
public function index()
|
||||||
{
|
{
|
||||||
parent::index();
|
parent::index();
|
||||||
|
|
|
||||||
|
|
@ -30,15 +30,78 @@ namespace phpOMS\System\File;
|
||||||
*/
|
*/
|
||||||
abstract class FileAbstract
|
abstract class FileAbstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Path.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
protected $path = '';
|
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)
|
public function __construct(string $path)
|
||||||
{
|
{
|
||||||
$this->path = $path;
|
$this->path = $path;
|
||||||
|
|
@ -48,51 +111,141 @@ abstract class FileAbstract
|
||||||
$this->changedAt = new \DateTime('now');
|
$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
|
public function getCount() : int
|
||||||
{
|
{
|
||||||
return $this->count;
|
return $this->count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get directory/file size.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getSize() : int
|
public function getSize() : int
|
||||||
{
|
{
|
||||||
return $this->size;
|
return $this->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getName() : string
|
public function getName() : string
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get path.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getPath() : string
|
public function getPath() : string
|
||||||
{
|
{
|
||||||
return $this->path;
|
return $this->path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get parent directory.
|
||||||
|
*
|
||||||
|
* @return Directory
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function parent() : Directory
|
public function parent() : Directory
|
||||||
{
|
{
|
||||||
return new Directory(Directory::getParent($this->path));
|
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
|
public function getCreatedAt() : \DateTime
|
||||||
{
|
{
|
||||||
return $this->createdAt;
|
return $this->createdAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get last changed at.
|
||||||
|
*
|
||||||
|
* @return \DateTime
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getChangedAt() : \DateTime
|
public function getChangedAt() : \DateTime
|
||||||
{
|
{
|
||||||
return $this->changedAt;
|
return $this->changedAt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get owner.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getOwner() : int
|
public function getOwner() : int
|
||||||
{
|
{
|
||||||
return $this->owner;
|
return $this->owner;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get permission.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getPermission() : string
|
public function getPermission() : string
|
||||||
{
|
{
|
||||||
return $this->permission;
|
return $this->permission;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (Re-)Index path.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$this->createdAt->setTimestamp(filemtime($this->path));
|
$this->createdAt->setTimestamp(filemtime($this->path));
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,7 @@
|
||||||
namespace phpOMS\System\File;
|
namespace phpOMS\System\File;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filesystem class.
|
* Path exception class.
|
||||||
*
|
|
||||||
* Performing operations on the file system
|
|
||||||
*
|
*
|
||||||
* @category System
|
* @category System
|
||||||
* @package Framework
|
* @package Framework
|
||||||
|
|
|
||||||
45
System/File/PermissionException.php
Normal file
45
System/File/PermissionException.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -15,7 +15,26 @@
|
||||||
*/
|
*/
|
||||||
namespace phpOMS\System;
|
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 {
|
final class OperatingSystem {
|
||||||
|
/**
|
||||||
|
* Get OS.
|
||||||
|
*
|
||||||
|
* @return int|SystemType
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public static function getSystem() : int
|
public static function getSystem() : int
|
||||||
{
|
{
|
||||||
switch(PHP_OS) {
|
switch(PHP_OS) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user