Started to extend file handling

This commit is contained in:
Dennis Eichhorn 2016-08-19 13:07:32 +02:00
parent c3d6da2539
commit b3e689e481
5 changed files with 173 additions and 40 deletions

View File

@ -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);

View File

@ -250,7 +250,7 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
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);

View File

@ -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 <d.eichhorn@oms.com>
*/
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;
}
}

View File

@ -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.

View File

@ -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;
}