2024-12-15 04:20:44 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
|
|
|
|
|
|
|
// Built-in libraries
|
|
|
|
use exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Files
|
|
|
|
*
|
|
|
|
* Trait with files handlers
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @method static void delete(string $$directory, array &$$errors)
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
|
|
|
*
|
|
|
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
|
|
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
|
|
|
*/
|
|
|
|
trait files
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Delete
|
|
|
|
*
|
|
|
|
* Delete files recursively
|
|
|
|
*
|
2024-12-15 22:16:01 +07:00
|
|
|
* @param string $$directory Directory
|
|
|
|
* @param array &$$errors Registry of errors
|
2024-12-15 04:20:44 +07:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2024-12-15 22:16:01 +07:00
|
|
|
private static function delete(string $$directory, array &$$errors = []): void
|
2024-12-15 04:20:44 +07:00
|
|
|
{
|
|
|
|
try {
|
2024-12-15 22:16:01 +07:00
|
|
|
if (file_exists($$directory)) {
|
2024-12-15 04:20:44 +07:00
|
|
|
// Directory exists
|
|
|
|
|
|
|
|
// Deleting descendant files and directories (enter to the recursion)
|
2024-12-15 22:16:01 +07:00
|
|
|
foreach (scandir($$directory) as $$file) {
|
|
|
|
if ($$file === '.' || $$file === '..') continue;
|
|
|
|
else if (is_dir("$$directory/$$file")) static::delete("$$directory/$$file", $$errors);
|
|
|
|
else unlink("$$directory/$$file");
|
2024-12-15 04:20:44 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deleting the directory
|
2024-12-15 22:16:01 +07:00
|
|
|
rmdir($$directory);
|
2024-12-15 04:20:44 +07:00
|
|
|
|
|
|
|
// Exit (success)
|
|
|
|
return;
|
|
|
|
} else throw new exception('Directory does not exist');
|
2024-12-15 22:16:01 +07:00
|
|
|
} catch (exception $$e) {
|
2024-12-15 04:20:44 +07:00
|
|
|
// Writing to the registry of errors
|
2024-12-15 22:16:01 +07:00
|
|
|
$$errors[] = [
|
|
|
|
'text' => $$e->getMessage(),
|
|
|
|
'file' => $$e->getFile(),
|
|
|
|
'line' => $$e->getLine(),
|
|
|
|
'stack' => $$e->getTrace()
|
2024-12-15 04:20:44 +07:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit (fail)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|