diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 70cfba3a9..3095c4402 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -104,7 +104,7 @@ class FileLogger implements LoggerInterface } if (is_dir($lpath) || strpos($lpath, '.') === false) { - Directory::createPath($lpath, '0644'); + Directory::create($lpath, '0644'); File::createFile($path = $lpath . '/' . date('Y-m-d') . '.log'); $path = realpath($path); diff --git a/System/File/Directory.php b/System/File/Directory.php index 1ce556cd9..fcd764e05 100644 --- a/System/File/Directory.php +++ b/System/File/Directory.php @@ -250,7 +250,7 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess * @since 1.0.0 * @author Dennis Eichhorn */ - public static function createPath(string $path, string $permission = '0644', bool $recursive = false) : bool + public static function create(string $path, string $permission = '0644', bool $recursive = false) : bool { if ($recursive && !file_exists($parent = self::getParent($path))) { self::createPath($parent, $permission, $recursive); diff --git a/System/File/File.php b/System/File/File.php index 3d2e4732b..84dfb211b 100644 --- a/System/File/File.php +++ b/System/File/File.php @@ -69,32 +69,7 @@ class File extends FileAbstract */ public function createNode() : bool { - return self::createFile($this->path); - } - - /** - * 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))) { - touch($path); - - return true; - } else { - throw new PermissionException($path); - } - } - - return false; + return self::create($this->path); } /** @@ -122,4 +97,158 @@ class File extends FileAbstract { file_put_contents($this->path, $content); } + + public static function put(string $path, string $content, bool $overwrite = true) : bool + { + if($overwrite || !file_exists($path)) { + if(!Directory::exists(dirname($path))) { + Directory::create(dirname($path), '0644', true); + } + + file_put_contents($path, $content); + + return true; + } + + return false; + } + + public static function get(string $path) : string + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return file_get_contents($path); + } + + public static function exists(string $path) : bool + { + return file_exists($path); + } + + public static function create(string $path) : string + { + if(!file_exists($path)) { + if(!Directory::exists(dirname($path))) { + Directory::create(dirname($path), '0644', true); + } + + touch($path); + + return true; + } + + return false; + } + + public static function parent(string $path) : string + { + return Directory::parent(dirname($path)); + } + + public static function created(string $path) : \DateTime + { + if(!file_exists($path)) { + throw new PathException($path); + } + + $created = new \DateTime(); + $created->setTimestamp(filemtime($path)); + + return $created; + } + + public static function changed(string $path) : \DateTime + { + if(!file_exists($path)) { + throw new PathException($path); + } + + $changed = new \DateTime(); + $changed->setTimestamp(filectime($path)); + + return $changed; + } + + public static function size(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return filesize($path); + } + + public static function owner(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return fileperms($path); + } + + public static function permission(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return fileowner($path); + } + + public static function dirname(string $path) : string + { + return dirname($path); + } + + public static function copy(string $from, string $to, bool $overwrite = false) : bool + { + if(!file_exists($from)) { + throw new PathException($from); + } + + if($overwrite || !file_exists($to)) { + if(!Directory::exists(dirname($to))) { + Directory::create(dirname($to), '0644', true); + } + + copy($from, $to); + + return true; + } + + return false; + } + + public static function move(string $from, string $to, bool $overwrite = false) : bool + { + if(!file_exists($from)) { + throw new PathException($from); + } + + if($overwrite || !file_exists($to)) { + if(!Directory::exists(dirname($to))) { + Directory::create(dirname($to), '0644', true); + } + + rename($from, $to); + + return true; + } + + return false; + } + + public static function delete(string $path) : bool + { + if(!file_exists($path)) { + return false; + } + + unlink($path); + + return true; + } } \ No newline at end of file diff --git a/System/File/FileAbstract.php b/System/File/FileAbstract.php index 96a3a184e..d157336ae 100644 --- a/System/File/FileAbstract.php +++ b/System/File/FileAbstract.php @@ -28,7 +28,7 @@ namespace phpOMS\System\File; * @link http://orange-management.com * @since 1.0.0 */ -abstract class FileAbstract +abstract class FileAbstract implements FileInterface { /** * Path. diff --git a/System/File/FileInterface.php b/System/File/FileInterface.php index 20d86cc16..b2425bb11 100644 --- a/System/File/FileInterface.php +++ b/System/File/FileInterface.php @@ -62,25 +62,29 @@ interface FileInterface public function index(); - public static function createdAt() : \DateTime; + public static function created(string $path) : \DateTime; - public static function changedAt() : \DateTime; + public static function changed(string $path) : \DateTime; - public static function owner() : int; + public static function owner(string $path) : int; - public static function permission() : string; + public static function permission(string $path) : string; - public static function parent() : string; + public static function parent(string $path) : string; - public static function create() : bool; + public static function create(string $path) : bool; - public static function delete() : bool; + public static function delete(string $path) : bool; - public static function copy() : bool; + public static function copy(string $from, string $to, bool $overwrite = false) : bool; - public static function move() : bool; + public static function move(string $from, string $to, bool $overwrite = false) : bool; - public static function put() : bool; + public static function put(string $path, string $content, bool $overwrite = true) : bool; - public static function get() : string; + public static function get(string $path) : string; + + public static function size(string $path) : string; + + public static function exists(string $path) : bool; }