fix cleaners DirTrait warnings

This commit is contained in:
Jean-Christian Paul Denis 2023-05-01 18:52:25 +02:00
parent 53ed6f3cf3
commit 2fc1489a28
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
5 changed files with 89 additions and 60 deletions

View File

@ -19,7 +19,8 @@ use Dotclear\Plugin\Uninstaller\{
CleanerDescriptor, CleanerDescriptor,
CleanerParent, CleanerParent,
TraitCleanerDir, TraitCleanerDir,
ValueDescriptor ValueDescriptor,
Helper\DirTrait
}; };
/** /**
@ -30,7 +31,7 @@ use Dotclear\Plugin\Uninstaller\{
*/ */
class Caches extends CleanerParent class Caches extends CleanerParent
{ {
use TraitCleanerDir; use DirTrait;
public function __construct() public function __construct()
{ {
@ -72,10 +73,10 @@ class Caches extends CleanerParent
public function values(): array public function values(): array
{ {
$res = []; $res = [];
foreach (self::getDirs(DC_TPL_CACHE) as $dir) { foreach (self::getDirs(DC_TPL_CACHE) as $path => $count) {
$res[] = new ValueDescriptor( $res[] = new ValueDescriptor(
ns: $dir['key'], ns: $path,
count: (int) $dir['value'] count: $count
); );
} }

View File

@ -19,7 +19,7 @@ use Dotclear\Plugin\Uninstaller\{
CleanerDescriptor, CleanerDescriptor,
CleanerParent, CleanerParent,
ValueDescriptor, ValueDescriptor,
TraitCleanerDir Helper\DirTrait
}; };
/** /**
@ -29,7 +29,7 @@ use Dotclear\Plugin\Uninstaller\{
*/ */
class Plugins extends CleanerParent class Plugins extends CleanerParent
{ {
use TraitCleanerDir; use DirTrait;
public function __construct() public function __construct()
{ {
@ -57,14 +57,14 @@ class Plugins extends CleanerParent
public function values(): array public function values(): array
{ {
$dirs = self::getDirs(explode(PATH_SEPARATOR, DC_PLUGINS_ROOT)); $dirs = self::getDirs(DC_PLUGINS_ROOT);
sort($dirs); sort($dirs);
$res = []; $res = [];
foreach ($dirs as $dir) { foreach ($dirs as $path => $count) {
$res[] = new ValueDescriptor( $res[] = new ValueDescriptor(
ns: $dir['key'], ns: $path,
count: (int) $dir['value'] count: $count
); );
} }
@ -74,8 +74,7 @@ class Plugins extends CleanerParent
public function execute(string $action, string $ns): bool public function execute(string $action, string $ns): bool
{ {
if ($action == 'delete') { if ($action == 'delete') {
$res = explode(PATH_SEPARATOR, DC_PLUGINS_ROOT); self::delDir(DC_PLUGINS_ROOT, $ns, true);
self::delDir($res, $ns, true);
return true; return true;
} }

View File

@ -20,7 +20,7 @@ use Dotclear\Plugin\Uninstaller\{
CleanerDescriptor, CleanerDescriptor,
CleanerParent, CleanerParent,
ValueDescriptor, ValueDescriptor,
TraitCleanerDir Helper\DirTrait
}; };
/** /**
@ -30,7 +30,7 @@ use Dotclear\Plugin\Uninstaller\{
*/ */
class Themes extends CleanerParent class Themes extends CleanerParent
{ {
use TraitCleanerDir; use DirTrait;
public function __construct() public function __construct()
{ {
@ -66,10 +66,10 @@ class Themes extends CleanerParent
sort($dirs); sort($dirs);
$res = []; $res = [];
foreach ($dirs as $dir) { foreach ($dirs as $path => $count) {
$res[] = new ValueDescriptor( $res[] = new ValueDescriptor(
ns: $dir['key'], ns: $path,
count: (int) $dir['value'] count: $count
); );
} }

View File

@ -19,7 +19,7 @@ use Dotclear\Plugin\Uninstaller\{
CleanerDescriptor, CleanerDescriptor,
CleanerParent, CleanerParent,
ValueDescriptor, ValueDescriptor,
TraitCleanerDir Helper\DirTrait
}; };
/** /**
@ -30,7 +30,7 @@ use Dotclear\Plugin\Uninstaller\{
*/ */
class Vars extends CleanerParent class Vars extends CleanerParent
{ {
use TraitCleanerDir; use DirTrait;
public function __construct() public function __construct()
{ {
@ -59,10 +59,10 @@ class Vars extends CleanerParent
public function values(): array public function values(): array
{ {
$res = []; $res = [];
foreach (self::getDirs(DC_VAR) as $dir) { foreach (self::getDirs(DC_VAR) as $path => $count) {
$res[] = new ValueDescriptor( $res[] = new ValueDescriptor(
ns: $dir['key'], ns: $path,
count: (int) $dir['value'] count: $count
); );
} }

View File

@ -12,7 +12,7 @@
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace Dotclear\Plugin\Uninstaller; namespace Dotclear\Plugin\Uninstaller\Helper;
use Dotclear\Helper\File\Files; use Dotclear\Helper\File\Files;
use Dotclear\Helper\File\Path; use Dotclear\Helper\File\Path;
@ -20,7 +20,7 @@ use Dotclear\Helper\File\Path;
/** /**
* Cleaner helper for files structure. * Cleaner helper for files structure.
*/ */
trait TraitCleanerDir trait DirTrait
{ {
/** @var array<int,string> The excluded files */ /** @var array<int,string> The excluded files */
public const EXCLUDED = [ public const EXCLUDED = [
@ -37,46 +37,67 @@ trait TraitCleanerDir
'_disabled', '_disabled',
]; ];
protected static function getDirs(string|array $roots): array /**
* Get path structure.
*
* @param string $paths The directories paths to scan
*
* @return array<string,int> The path structure
*/
protected static function getDirs(string $paths): array
{ {
if (!is_array($roots)) { $paths = explode(PATH_SEPARATOR, $paths);
$roots = [$roots];
} $stack = [];
$rs = []; foreach ($paths as $path) {
$i = 0; $dirs = Files::scanDir($path);
foreach ($roots as $root) {
$dirs = Files::scanDir($root);
foreach ($dirs as $k) { foreach ($dirs as $k) {
if (in_array($k, self::EXCLUDED) || !is_dir($root . DIRECTORY_SEPARATOR . $k)) { if (!is_string($k) || in_array($k, self::EXCLUDED) || !is_dir($path . DIRECTORY_SEPARATOR . $k)) {
continue; continue;
} }
$rs[$i]['key'] = $k; $stack[$k] = count(self::scanDir($path . DIRECTORY_SEPARATOR . $k));
$rs[$i]['value'] = count(self::scanDir($root . DIRECTORY_SEPARATOR . $k));
$i++;
} }
} }
return $rs; return $stack;
} }
protected static function delDir(string|array $roots, string $folder, bool $delfolder = true): bool /**
* Delete path structure.
*
* @param string $paths The directories paths to scan
* @param string $folder The folder in path
* @param bool $delete Also delete folder itself
*
* @return bool True on success
*/
protected static function delDir(string $paths, string $folder, bool $delete = true): bool
{ {
$paths = explode(PATH_SEPARATOR, $paths);
if (strpos($folder, DIRECTORY_SEPARATOR)) { if (strpos($folder, DIRECTORY_SEPARATOR)) {
return false; return false;
} }
if (!is_array($roots)) {
$roots = [$roots]; foreach ($paths as $path) {
} if (file_exists($path . DIRECTORY_SEPARATOR . $folder)) {
foreach ($roots as $root) { return self::delTree($path . DIRECTORY_SEPARATOR . $folder, $delete);
if (file_exists($root . DIRECTORY_SEPARATOR . $folder)) {
return self::delTree($root . DIRECTORY_SEPARATOR . $folder, $delfolder);
} }
} }
return false; return false;
} }
protected static function scanDir(string $path, string $dir = '', array $res = []): array /**
* Scan recursively a directory.
*
* @param string $path The directory path to scan
* @param string $dir The current directory
* @param array<int,string> $stack The paths stack
*
* @return array<int,string> The paths stack
*/
private static function scanDir(string $path, string $dir = '', array $stack = []): array
{ {
$path = Path::real($path); $path = Path::real($path);
if ($path === false || !is_dir($path) || !is_readable($path)) { if ($path === false || !is_dir($path) || !is_readable($path)) {
@ -89,35 +110,43 @@ trait TraitCleanerDir
continue; continue;
} }
if (is_dir($path . DIRECTORY_SEPARATOR . $file)) { if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
$res[] = $file; $stack[] = $file;
$res = self::scanDir($path . DIRECTORY_SEPARATOR . $file, $dir . DIRECTORY_SEPARATOR . $file, $res); $stack = self::scanDir($path . DIRECTORY_SEPARATOR . $file, $dir . DIRECTORY_SEPARATOR . $file, $stack);
} else { } else {
$res[] = empty($dir) ? $file : $dir . DIRECTORY_SEPARATOR . $file; $stack[] = empty($dir) ? $file : $dir . DIRECTORY_SEPARATOR . $file;
} }
} }
return $res; return $stack;
} }
protected static function delTree(string $dir, bool $delroot = true): bool /**
* Delete path tree.
*
* @param string $path The directory path
* @param bool $delete Also delete the directory path
*
* @return bool True on success
*/
private static function delTree(string $path, bool $delete = true): bool
{ {
if (!is_dir($dir) || !is_readable($dir)) { if (!is_dir($path) || !is_readable($path)) {
return false; return false;
} }
if (substr($dir, -1) != DIRECTORY_SEPARATOR) { if (substr($path, -1) != DIRECTORY_SEPARATOR) {
$dir .= DIRECTORY_SEPARATOR; $path .= DIRECTORY_SEPARATOR;
} }
if (($d = @dir($dir)) === false) { if (($d = @dir($path)) === false) {
return false; return false;
} }
while (($entryname = $d->read()) !== false) { while (($entryname = $d->read()) !== false) {
if ($entryname != '.' && $entryname != '..') { if ($entryname != '.' && $entryname != '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $entryname)) { if (is_dir($path . DIRECTORY_SEPARATOR . $entryname)) {
if (!self::delTree($dir . DIRECTORY_SEPARATOR . $entryname)) { if (!self::delTree($path . DIRECTORY_SEPARATOR . $entryname)) {
return false; return false;
} }
} else { } else {
if (!@unlink($dir . DIRECTORY_SEPARATOR . $entryname)) { if (!@unlink($path . DIRECTORY_SEPARATOR . $entryname)) {
return false; return false;
} }
} }
@ -125,8 +154,8 @@ trait TraitCleanerDir
} }
$d->close(); $d->close();
if ($delroot) { if ($delete) {
return @rmdir($dir); return @rmdir($path);
} }
return true; return true;