diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php index 05e3021a0..bf6850482 100644 --- a/DataStorage/Cache/FileCache.php +++ b/DataStorage/Cache/FileCache.php @@ -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 diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 2efa3b162..389667eff 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -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); } diff --git a/System/File/Directory.php b/System/File/Directory.php index a09d62e50..b6c87f9af 100644 --- a/System/File/Directory.php +++ b/System/File/Directory.php @@ -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 + */ + 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 + */ + 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 + */ + 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 + */ 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 + */ 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 + */ 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 + */ + 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 + */ 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 + */ 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; diff --git a/System/File/File.php b/System/File/File.php index f72271023..540544add 100644 --- a/System/File/File.php +++ b/System/File/File.php @@ -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 + */ + 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 + */ 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 + */ + 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 + */ + public function setContent(string $content) + { + file_put_contents($this->path, $content); + } + + /** + * Index file. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function index() { parent::index(); diff --git a/System/File/FileAbstract.php b/System/File/FileAbstract.php index 75a9433b6..87dca8ef3 100644 --- a/System/File/FileAbstract.php +++ b/System/File/FileAbstract.php @@ -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 + */ 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 + */ public function getCount() : int { return $this->count; } + /** + * Get directory/file size. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getSize() : int { return $this->size; } + /** + * Get name. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getName() : string { return $this->name; } + /** + * Get path. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getPath() : string { return $this->path; } + /** + * Get parent directory. + * + * @return Directory + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function parent() : Directory { return new Directory(Directory::getParent($this->path)); } + /** + * Create file/directory. + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + abstract private function createNode() : bool; + + /** + * Get created at. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getCreatedAt() : \DateTime { return $this->createdAt; } + /** + * Get last changed at. + * + * @return \DateTime + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getChangedAt() : \DateTime { return $this->changedAt; } + /** + * Get owner. + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getOwner() : int { return $this->owner; } + /** + * Get permission. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getPermission() : string { return $this->permission; } + /** + * (Re-)Index path. + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function index() { $this->createdAt->setTimestamp(filemtime($this->path)); diff --git a/System/File/PathException.php b/System/File/PathException.php index 7d93a90d8..c50cecfcb 100644 --- a/System/File/PathException.php +++ b/System/File/PathException.php @@ -16,9 +16,7 @@ namespace phpOMS\System\File; /** - * Filesystem class. - * - * Performing operations on the file system + * Path exception class. * * @category System * @package Framework diff --git a/System/File/PermissionException.php b/System/File/PermissionException.php new file mode 100644 index 000000000..14af15294 --- /dev/null +++ b/System/File/PermissionException.php @@ -0,0 +1,45 @@ + + * @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; + +/** + * Permission exception class. + * + * @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 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 + */ + public function __construct(string $message, int $code = 0, \Exception $previous = null) + { + parent::__construct('Insufficient permissions for "' . $message . '" operations.', $code, $previous); + } +} diff --git a/System/FileSystem.php b/System/FileSystem.php deleted file mode 100644 index e638df374..000000000 --- a/System/FileSystem.php +++ /dev/null @@ -1,115 +0,0 @@ - - * @author Dennis Eichhorn - * @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 - * @author Dennis Eichhorn - * @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 - */ - 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 - */ - 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; - } -} diff --git a/System/OperatingSystem.php b/System/OperatingSystem.php index ffdae2932..c5c921630 100644 --- a/System/OperatingSystem.php +++ b/System/OperatingSystem.php @@ -15,7 +15,26 @@ */ namespace phpOMS\System; +/** + * Operating system class. + * + * @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 + */ final class OperatingSystem { + /** + * Get OS. + * + * @return int|SystemType + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function getSystem() : int { switch(PHP_OS) {