Skip to content

Commit

Permalink
Fix issue with copy
Browse files Browse the repository at this point in the history
  • Loading branch information
bittrframework committed May 29, 2019
1 parent 1e85df7 commit d79253d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 89 deletions.
8 changes: 6 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

require_once 'src/Option.php';

(new Option('tmp-dir', true, true))->createFolder();
$m = (new Option('tmp-dir', true, true))->createFolder();

//var_dump($m->msg());
(new Option('tmp-dir/tmp-file1', true, true))->createFile();
(new Option('tmp-dir/tmp-file2', true, true))->createFile();
(new Option('tmp-dir/tmp-file3', true, true))->createFile();
Expand All @@ -16,4 +18,6 @@
(new Option('new-tmp-dir/tmp-file2', true, true))->copy('tmp-file-copy1');


//(new Option('tmp-dir', true, true))->copy('new-tmp-dir2');
(new Option('tmp-dir', true, true))->copy('new-tmp-dir2');

(new Option('tmp-dir'))->rename('bar');
Empty file removed new-tmp-dir/tmp-file1
Empty file.
Empty file removed new-tmp-dir/tmp-file2
Empty file.
Empty file removed new-tmp-dir/tmp-file3
Empty file.
141 changes: 54 additions & 87 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
namespace Bittr;

use Closure;
use DirectoryIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RuntimeException;
use SplFileInfo;
use Throwable;

Expand All @@ -58,8 +58,8 @@ class Option extends SplFileInfo
private $status = false;
/** @var null|string */
private $base_path = null;
/** @var bool */
private $as_cut = false;
/** @var string */
private $msg = false;
/** @var int */
private $mode = 0755;
/** @var null|string */
Expand Down Expand Up @@ -103,19 +103,10 @@ public function __construct(string $file, bool $force = false, bool $suppress_er
$this->base_path = $this->getPath();
}

/**
* Handles error and status.
*
* @param string $message
* @return \Bittr\Option
*/
private function error(string $message): Option
private function stat(bool $status, string $type, string $arg, string $arg2 = null)
{
$this->status = false;
if (! $this->silent)
{
throw new RuntimeException($message);
}
$this->status = $status;
$this->msg = ($status ? 'complete' : 'failed') . "::{$type} for ({$arg}" . ($arg2 ? ", {$arg2})" : ')');

return $this;
}
Expand Down Expand Up @@ -182,7 +173,7 @@ public function delete(): Option
{
if ($this->isFile())
{
$this->status = @unlink($name);
$this->stat(@unlink($name), 'unlink', $name);
}
else
{
Expand All @@ -193,22 +184,24 @@ public function delete(): Option

if ($iterator->valid() && ! $this->force)
{
$this->error("The folder(\"{$name}\") you are trying to delete is not empty.");
return $this->stat(false, 'rmdir(on non empty directory)', $name);
}
else

/** @var SplFileInfo $file_info */
foreach ($iterator as $file_info)
{
/** @var SplFileInfo $file_info */
foreach ($iterator as $file_info)
{
$file_info->isFile() ? unlink($file_info->getRealPath()) : rmdir($file_info->getRealPath());
}
$this->status = @rmdir($this->getPathname());
$path = $file_info->getRealPath();
$file_info->isFile()
? $this->stat(@unlink($path), 'unlink', $path)
: $this->stat(@rmdir($path), 'rmdir', $path);
}

return $this->stat(@rmdir($name), 'rmdir', $name);
}
}
else
{
$this->error("\"{$name}\" is not a directory. Or insufficient access permission.");
return $this->stat(false, 'rm/unlink(on non existing file/dir or insufficient permission)', $name);
}

return $this;
Expand All @@ -221,17 +214,9 @@ public function delete(): Option
*/
public function createFolder(): Option
{
$this->status = true;
if ($this->isReadable())
{
return $this->error("Directory: \"{$this->getFilename()}\" already exist in \"{$this->getPath()}\".");
}
else
{
$this->status = @mkdir($this->getPathname(), $this->mode, true);
}
$path = $this->getPathname();

return $this;
return $this->stat(@mkdir($path, $this->mode, true), 'mkdir', $path);
}

/**
Expand All @@ -241,17 +226,15 @@ public function createFolder(): Option
*/
public function createFile(): Option
{
$this->status = true;
if ($this->isReadable() && $this->isFile())
{
if ($this->force)
{
$this->status = @unlink($this->getPathname());
}
else
if (! $this->force)
{
return $this->error("File: \"{$this->getFilename()}\" already exist in \"{$this->getPath()}\".");
return $this;
}

$path = $this->getPathname();
$this->stat(@unlink($path), 'unlink', $path);
}
$this->openFile('a');

Expand All @@ -262,17 +245,19 @@ public function createFile(): Option
* Renames a file or directory.
*
* @param string $new_name
* @param bool $cut
* @return Option
*/
public function rename(string $new_name): Option
public function rename(string $new_name, bool $cut = false): Option
{
if (! $this->isReadable())
{
$this->error("\"{$this->getPathname()}\" could not be found. Or insufficient access permission.");
}
else
if ($this->isReadable())
{
$this->status = @rename($this->getPathname(), "{$this->getPath()}/{$new_name}");
$current = $this->getPathname();
if (! $cut)
{
$new_name = rtrim($this->getRealPath(), $current) . "{$new_name}";
}
$this->stat(@rename($current, $new_name), 'rename', $current, $new_name);
}

return $this;
Expand All @@ -290,29 +275,6 @@ public function setMode(int $mode): Option

return $this;
}

private function move(SplFileInfo $from, string $to, array &$failed_copies): void
{
$old_file = $from->getRealPath();
$path = $to . str_replace($this->getRealPath(), '', $old_file);
if (is_readable($path))
{
if (! $this->force)
{
$failed_copies['file'][] = $old_file;
$this->error("File: \"{$from->getFilename()}\" already exist in \"{$to}\".");

return;
}
}

$this->status = @copy($old_file, $path);
if ($this->as_cut)
{
$this->status = @unlink($old_file);
}
}

/**
* Copies file or directories to specified directory.
*
Expand All @@ -333,12 +295,12 @@ public function copy(string $to): Option
}
catch (Throwable $throwable)
{
return $this->error("\"{$original}\" could not be found. Or insufficient access permission.");
return $this->stat(false, 'permission', $original);
}

if (! is_readable($to))
{
mkdir($to, $this->mode, true);
$this->stat(@mkdir($to, $this->mode, true), 'mkdir', $to);
}

/** @var SplFileInfo $file_or_dir */
Expand All @@ -350,12 +312,13 @@ public function copy(string $to): Option
{
if (! $file_or_dir->isReadable() || $this->force)
{
copy($file_or_dir->getRealPath(), $source);
$file = $file_or_dir->getRealPath();
$this->stat(@copy($file, $source), 'copy', $file, $source);
}
}
else
{
@mkdir($source, $this->mode);
$this->stat(@mkdir($source, $this->mode), 'mkdir', $source);
}
}
}
Expand All @@ -369,7 +332,7 @@ public function copy(string $to): Option
{
if (! is_readable($dir))
{
mkdir($dir, $this->mode, true);
$this->stat(@mkdir($dir, $this->mode, true), 'mkdir', $dir);
}
$dir .= DIRECTORY_SEPARATOR;
}
Expand All @@ -380,16 +343,13 @@ public function copy(string $to): Option
}

$new_path = "{$dir}{$file}";

if (is_readable($new_path) && ! $this->force)
{
return $this->error("File \"{$new_path}\" already exist.");
$this->stat(false, 'exist', $new_path);
}

@copy($this->getPathname(), $new_path);
if ($this->as_cut)
else
{
unlink($this->getPathname());
$this->stat(@copy($original, $new_path), 'mkdir', $original, $new_path);
}
}

Expand All @@ -404,10 +364,7 @@ public function copy(string $to): Option
*/
public function cut(string $to): Option
{
$this->as_cut = true;
$this->copy($to);

return $this;
return $this->rename($to, true);
}

/**
Expand All @@ -420,6 +377,16 @@ public function status(): bool
return $this->status;
}

/**
* Gets last exec message
*
* @return string
*/
public function msg(): string
{
return $this->msg;
}

/**
* @return string
*/
Expand Down
Empty file removed tmp-dir/tmp-file1
Empty file.
Empty file removed tmp-dir/tmp-file2
Empty file.
Empty file removed tmp-dir/tmp-file3
Empty file.

0 comments on commit d79253d

Please sign in to comment.