created
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
!.gitignore
|
||||
composer.lock
|
||||
vendor
|
||||
@@ -1,3 +1,7 @@
|
||||
# files
|
||||
# Files
|
||||
Easy working with files
|
||||
|
||||
Easy working with files
|
||||
## Installation
|
||||
```bash
|
||||
composer require mirzaev/files
|
||||
```
|
||||
|
||||
32
composer.json
Normal file
32
composer.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "mirzaev/files",
|
||||
"description": "Easy working with files",
|
||||
"homepage": "https://git.svoboda.works/mirzaev/files",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"files"
|
||||
],
|
||||
"readme": "README.md",
|
||||
"license": "WTFPL",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Arsen Mirzaev Tatyano-Muradovich",
|
||||
"email": "arsen@mirzaev.sexy",
|
||||
"homepage": "https://mirzaev.sexy",
|
||||
"role": "Creator"
|
||||
}
|
||||
],
|
||||
"support": {
|
||||
"wiki": "https://git.svoboda.works/mirzaev/files/wiki",
|
||||
"issues": "https://git.svoboda.works/mirzaev/files/issues"
|
||||
},
|
||||
"require": {
|
||||
"php": "^8.4"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"mirzaev\\files\\": "mirzaev/files/system"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
67
mirzaev/files/system/files.php
Normal file
67
mirzaev/files/system/files.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace mirzaev\files;
|
||||
|
||||
// Built-in libraries
|
||||
use RuntimeException as exception_runtime;
|
||||
|
||||
/**
|
||||
* Files
|
||||
*
|
||||
* @method static void delete(string $directory, array &$errors)
|
||||
*
|
||||
* @package kodorvan\neurobot\models\traits
|
||||
*
|
||||
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
||||
*/
|
||||
class files
|
||||
{
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* Delete files recursively
|
||||
*
|
||||
* @param string $directory Directory
|
||||
* @param array &$errors Registry of errors
|
||||
*
|
||||
* @throws exception_runtime if directory does not exists
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function delete(string $directory, array &$errors = []): void
|
||||
{
|
||||
try {
|
||||
if (file_exists($directory)) {
|
||||
// Directory exists
|
||||
|
||||
// Deleting descendant files and directories (enter to the recursion)
|
||||
foreach (scandir($directory) as $file) {
|
||||
if ($file === '.' || $file === '..') continue;
|
||||
else if (is_dir("$directory/$file")) static::delete("$directory/$file", $errors);
|
||||
else unlink("$directory/$file");
|
||||
}
|
||||
|
||||
// Deleting the directory
|
||||
rmdir($directory);
|
||||
|
||||
// Exit (success)
|
||||
return;
|
||||
} else throw new exception_runtime('Directory does not exist');
|
||||
} catch (exception $e) {
|
||||
// Writing to the registry of errors
|
||||
$errors[] = [
|
||||
'text' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'stack' => $e->getTrace()
|
||||
];
|
||||
}
|
||||
|
||||
// Exit (fail)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user