Skip to content

Commit

Permalink
Merge pull request #67 from getkirby/fix/linux-commands-to-php-functions
Browse files Browse the repository at this point in the history
Switch linux commands to php functions
  • Loading branch information
bastianallgeier authored Feb 26, 2024
2 parents a9c3c83 + a38fd51 commit f480d13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 19 deletions.
30 changes: 23 additions & 7 deletions commands/unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,46 @@
]
],
'command' => static function (CLI $cli): void {
if (class_exists('ZipArchive') === false) {
throw new Exception('ZipArchive library could not be found');
}

$file = $cli->arg('file');
$to = $cli->arg('to');
$temp = $to . '.temp';

if (is_file($file) === false) {
throw new Exception('The ZIP file does not exist');
}

if (is_dir($to) === true) {
if (is_dir($to) === true || is_dir($temp) === true) {
throw new Exception('The target directory exists');
}

// extract the zip file
exec('unzip ' . escapeshellarg($file) . ' -d ' . escapeshellarg($to));
// extract the zip file to the temp directory
// to move temp directory to target directory
// since there is not a php native function to move entire directory into parent
$zipArchive = new ZipArchive();
if ($zipArchive->open($file) === true) {
$zipArchive->extractTo($temp);
$zipArchive->close();
} else {
throw new Exception('The zip file could not read');
}

$temp = realpath($temp);

$to = realpath($to);
// target path doesn't exist yet and realpath won't work for it. So use temp path.
$to = substr($temp, 0, strlen($temp) - strlen('.temp'));

// find the archive folder in that tmp dir
$archive = glob($to . '/*', GLOB_ONLYDIR)[0] ?? null;
$archive = glob($temp . '/*', GLOB_ONLYDIR)[0] ?? null;

if (is_dir($archive) === false) {
throw new Exception('The archive directory could not be found');
}

exec('mv ' . escapeshellarg($to) . '/*/{.[!.],}* ' . escapeshellarg($to) . '/');
exec('rm -rf ' . escapeshellarg($archive));
rename($archive, $to);
$cli->rmdir($to . '.temp');
}
];
45 changes: 33 additions & 12 deletions src/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,7 @@ public function confirmToDelete(string $item, string $message): bool
// we need to implement Dir::remove and F::remove here again, because
// the Kirby installation might not be available when we need this
if (is_dir($item) === true) {
$iterator = new RecursiveDirectoryIterator($item, RecursiveDirectoryIterator::SKIP_DOTS);
$children = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);

foreach ($children as $child) {
if ($child->isDir()) {
rmdir($child->getRealPath());
} else {
unlink($child->getRealPath());
}
}

rmdir($item);
$this->rmdir($item);
} else {
unlink($item);
}
Expand Down Expand Up @@ -458,6 +447,38 @@ public function prompt(string $prompt, bool $required = true)
return $value;
}

/**
* Removes a folder including all containing files and folders
*/
public function rmdir($dir): bool
{
$dir = realpath($dir);

if (is_dir($dir) === false) {
return true;
}

if (is_link($dir) === true) {
return unlink($dir);
}

foreach (scandir($dir) as $childName) {
if (in_array($childName, ['.', '..']) === true) {
continue;
}

$child = $dir . '/' . $childName;

if (is_dir($child) === true && is_link($child) === false) {
$this->rmdir($child);
} else {
unlink($child);
}
}

return rmdir($dir);
}

/**
* Returns a root either from the custom roots
* array or from the Kirby instance
Expand Down

0 comments on commit f480d13

Please sign in to comment.