20250608
[File] обновлён метод RemoveDir. Теперь он корректно удаляет не пустую директорию. [File] добавлен метод DirectoryExists (string $directory, bool $checkReadAccess = true, bool $checkWriteAccess = false): bool
This commit is contained in:
parent
0730509096
commit
28dbf773f1
@ -20,7 +20,7 @@
|
|||||||
"ext-mbstring": "*"
|
"ext-mbstring": "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^12.1.6"
|
"phpunit/phpunit": "^12.2.1"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
34
composer.lock
generated
34
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "0c5a35b9c8987ac3fd0f7480fea87d4c",
|
"content-hash": "f5bf0ec4042cb12fb3a702cad65f099d",
|
||||||
"packages": [],
|
"packages": [],
|
||||||
"packages-dev": [
|
"packages-dev": [
|
||||||
{
|
{
|
||||||
@ -69,16 +69,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "nikic/php-parser",
|
"name": "nikic/php-parser",
|
||||||
"version": "v5.4.0",
|
"version": "v5.5.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/nikic/PHP-Parser.git",
|
"url": "https://github.com/nikic/PHP-Parser.git",
|
||||||
"reference": "447a020a1f875a434d62f2a401f53b82a396e494"
|
"reference": "ae59794362fe85e051a58ad36b289443f57be7a9"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/447a020a1f875a434d62f2a401f53b82a396e494",
|
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/ae59794362fe85e051a58ad36b289443f57be7a9",
|
||||||
"reference": "447a020a1f875a434d62f2a401f53b82a396e494",
|
"reference": "ae59794362fe85e051a58ad36b289443f57be7a9",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -121,9 +121,9 @@
|
|||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/nikic/PHP-Parser/issues",
|
"issues": "https://github.com/nikic/PHP-Parser/issues",
|
||||||
"source": "https://github.com/nikic/PHP-Parser/tree/v5.4.0"
|
"source": "https://github.com/nikic/PHP-Parser/tree/v5.5.0"
|
||||||
},
|
},
|
||||||
"time": "2024-12-30T11:07:19+00:00"
|
"time": "2025-05-31T08:24:38+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phar-io/manifest",
|
"name": "phar-io/manifest",
|
||||||
@ -579,16 +579,16 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "phpunit/phpunit",
|
"name": "phpunit/phpunit",
|
||||||
"version": "12.1.6",
|
"version": "12.2.1",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
"url": "https://github.com/sebastianbergmann/phpunit.git",
|
||||||
"reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0"
|
"reference": "5f09fda04e7caea93cff50b4e90319184f3e6ee3"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2fdf0056c673c8f0f1eed00030be5f8243c1e6e0",
|
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/5f09fda04e7caea93cff50b4e90319184f3e6ee3",
|
||||||
"reference": "2fdf0056c673c8f0f1eed00030be5f8243c1e6e0",
|
"reference": "5f09fda04e7caea93cff50b4e90319184f3e6ee3",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -602,7 +602,7 @@
|
|||||||
"phar-io/manifest": "^2.0.4",
|
"phar-io/manifest": "^2.0.4",
|
||||||
"phar-io/version": "^3.2.1",
|
"phar-io/version": "^3.2.1",
|
||||||
"php": ">=8.3",
|
"php": ">=8.3",
|
||||||
"phpunit/php-code-coverage": "^12.2.1",
|
"phpunit/php-code-coverage": "^12.3.0",
|
||||||
"phpunit/php-file-iterator": "^6.0.0",
|
"phpunit/php-file-iterator": "^6.0.0",
|
||||||
"phpunit/php-invoker": "^6.0.0",
|
"phpunit/php-invoker": "^6.0.0",
|
||||||
"phpunit/php-text-template": "^5.0.0",
|
"phpunit/php-text-template": "^5.0.0",
|
||||||
@ -610,7 +610,7 @@
|
|||||||
"sebastian/cli-parser": "^4.0.0",
|
"sebastian/cli-parser": "^4.0.0",
|
||||||
"sebastian/comparator": "^7.0.1",
|
"sebastian/comparator": "^7.0.1",
|
||||||
"sebastian/diff": "^7.0.0",
|
"sebastian/diff": "^7.0.0",
|
||||||
"sebastian/environment": "^8.0.1",
|
"sebastian/environment": "^8.0.2",
|
||||||
"sebastian/exporter": "^7.0.0",
|
"sebastian/exporter": "^7.0.0",
|
||||||
"sebastian/global-state": "^8.0.0",
|
"sebastian/global-state": "^8.0.0",
|
||||||
"sebastian/object-enumerator": "^7.0.0",
|
"sebastian/object-enumerator": "^7.0.0",
|
||||||
@ -624,7 +624,7 @@
|
|||||||
"type": "library",
|
"type": "library",
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-main": "12.1-dev"
|
"dev-main": "12.2-dev"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
@ -656,7 +656,7 @@
|
|||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
|
||||||
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
|
||||||
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.1.6"
|
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.2.1"
|
||||||
},
|
},
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
@ -680,7 +680,7 @@
|
|||||||
"type": "tidelift"
|
"type": "tidelift"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2025-05-21T12:36:31+00:00"
|
"time": "2025-06-07T05:17:47+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "sebastian/cli-parser",
|
"name": "sebastian/cli-parser",
|
||||||
@ -1621,4 +1621,4 @@
|
|||||||
},
|
},
|
||||||
"platform-dev": {},
|
"platform-dev": {},
|
||||||
"plugin-api-version": "2.6.0"
|
"plugin-api-version": "2.6.0"
|
||||||
}
|
}
|
@ -2,12 +2,15 @@
|
|||||||
|
|
||||||
namespace goodboyalex\php_components_pack\classes;
|
namespace goodboyalex\php_components_pack\classes;
|
||||||
|
|
||||||
|
use RecursiveDirectoryIterator;
|
||||||
|
use RecursiveIteratorIterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Класс, реализующий функционал работы с файлами и выполнение операций над файлами одной командой.
|
* Класс, реализующий функционал работы с файлами и выполнение операций над файлами одной командой.
|
||||||
*
|
*
|
||||||
* @author Александр Бабаев
|
* @author Александр Бабаев
|
||||||
* @package php_components_pack
|
* @package php_components_pack
|
||||||
* @version 1.0
|
* @version 1.0.1
|
||||||
* @since 1.0.21
|
* @since 1.0.21
|
||||||
*/
|
*/
|
||||||
final class File
|
final class File
|
||||||
@ -111,38 +114,67 @@ final class File
|
|||||||
*/
|
*/
|
||||||
public static function RemoveDir (string $directory): bool
|
public static function RemoveDir (string $directory): bool
|
||||||
{
|
{
|
||||||
// Если директории не существует
|
// Проверяем, существует ли директория
|
||||||
if (!file_exists($directory))
|
if (!self::DirectoryExists(directory: $directory, checkWriteAccess: true))
|
||||||
// - то удаление не требуется
|
// - если нет, то возвращаем false
|
||||||
return true;
|
return false;
|
||||||
|
|
||||||
// Если это не директория
|
// Получаем список файлов и каталогов в заданной директории
|
||||||
if (!is_dir($directory))
|
$files = new RecursiveIteratorIterator(
|
||||||
// - то удаляем файл
|
new RecursiveDirectoryIterator($directory),
|
||||||
return unlink($directory);
|
RecursiveIteratorIterator::CHILD_FIRST
|
||||||
|
);
|
||||||
|
|
||||||
// Перебираем все файлы и поддиректории
|
// Перебираем файлы и каталоги
|
||||||
foreach (scandir($directory) as $item) {
|
foreach ($files as $file)
|
||||||
// - если это '.' или '..'
|
// - если это каталог
|
||||||
if ($item == '.' || $item == '..')
|
if ($file->isDir())
|
||||||
// -- то пропускаем
|
// -- то удаляем его
|
||||||
continue;
|
rmdir($file->getRealPath());
|
||||||
|
else
|
||||||
// - задаём переменную - целевую директорию
|
// -- иначе удаляем файл
|
||||||
$target = $directory . DIRECTORY_SEPARATOR . $item;
|
unlink($file->getRealPath());
|
||||||
|
|
||||||
// - если это директория
|
|
||||||
if (is_dir($target))
|
|
||||||
// -- то рекурсивно удаляем её
|
|
||||||
if (!self::RemoveDir($target))
|
|
||||||
// --- и прерываем удаление директории, если ошибка
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
// -- иначе удаляем файл
|
|
||||||
unlink($target);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Удаляем директорию
|
// Удаляем директорию
|
||||||
return rmdir($directory);
|
return rmdir($directory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Проверяет, существует ли директория.
|
||||||
|
*
|
||||||
|
* @param string $directory Путь к директории.
|
||||||
|
* @param bool $checkReadAccess Проверять ли доступ на чтение директории (по умолчанию true).
|
||||||
|
* @param bool $checkWriteAccess Проверять ли доступ на запись директории (по умолчанию false).
|
||||||
|
*
|
||||||
|
* @return bool Результат проверки.
|
||||||
|
*/
|
||||||
|
public static function DirectoryExists (string $directory, bool $checkReadAccess = true,
|
||||||
|
bool $checkWriteAccess = false): bool
|
||||||
|
{
|
||||||
|
// Очищаем кэш
|
||||||
|
clearstatcache();
|
||||||
|
|
||||||
|
// Проверяем, существует ли директория
|
||||||
|
if (!file_exists($directory))
|
||||||
|
// - если нет, то возвращаем false
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Проверяем, является ли директория директорией, а не файлом
|
||||||
|
if (!is_dir($directory))
|
||||||
|
// - если нет, то возвращаем false
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Проверяем, есть ли доступ на чтение директории
|
||||||
|
if ($checkReadAccess && !is_readable($directory))
|
||||||
|
// - если нет, то возвращаем false
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Проверяем, есть ли доступ на запись директории
|
||||||
|
if ($checkWriteAccess && !is_writable($directory))
|
||||||
|
// - если нет, то возвращаем false
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Если все проверки пройдены успешно, то возвращаем true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
@ -59,4 +59,18 @@ class FileTest extends TestCase
|
|||||||
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("c:/tmp/test.txt"));
|
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("c:/tmp/test.txt"));
|
||||||
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("\\tmp\\test.txt"));
|
$this->assertEquals("test", File::ExtractFileNameWithoutExtension("\\tmp\\test.txt"));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public function testRemoveDir ()
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ВАЖНО! Перед запуском теста необходимо создать директорию D:\TestDelete и наполнить её ненужными файлами
|
||||||
|
*/
|
||||||
|
|
||||||
|
$this->PrepareForTest();
|
||||||
|
|
||||||
|
$result = File::RemoveDir("D:\\TestDelete");
|
||||||
|
|
||||||
|
$this->assertTrue($result);
|
||||||
|
$this->assertFalse(File::DirectoryExists("D:\\TestDelete"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user