Skip to content

Internals Uninstaller Cleanup PackageFilesCleanup

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

uninstaller\cleanup\PackageFilesCleanup

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/uninstaller/cleanup/PackageFilesCleanup.php · Snapshot 8411f4287706.

Uninstallation deletes the package record before attempting each cleanup collaborator. Failures are collected; cleanup continues and can leave partial state.

Imports and dependencies

use uninstaller\interfaces\IPackageCleanup;

PackageFilesCleanup

Declaration and architectural role

class PackageFilesCleanup implements IPackageCleanup

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$scanned private array $scanned = ['dir' => [], 'file' => []]; Used by clean(), scanDirectory().
Constructor-promoted public function __construct(private readonly string $packageRoot) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(private readonly string $packageRoot)
clean public function clean(int $id, string $name): array
scanDirectory private function scanDirectory(string $dir, array &$errors): void

__construct

Source line 11

public function __construct(private readonly string $packageRoot);

Parameters

Parameter Type, modifiers and default Purpose
$packageRoot private readonly string $packageRoot Operation input; the exact use, validation and propagation are shown in the implementation trace.

Concrete no-op/default hook: this method performs no work. Override it where the lifecycle or extension contract calls for behavior.

clean

Source line 13

public function clean(int $id, string $name): array

Parameters

Parameter Type, modifiers and default Purpose
$id int $id Record or runtime identifier selected by this operation.
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

State writes: $this->scanned.

Return expressions (including local callbacks): ['Package root directory does not exist.']; ['Invalid package directory name.']; []; @unlink($path) ? [] : ["Failed to delete: {$path}"]; $errors.

External effects include unlink, rmdir.

Implementation, validation guards and interaction order
public function clean(int $id, string $name): array
    {
        $this->scanned = ['dir' => [], 'file' => []];
        $root = realpath($this->packageRoot);
        if ($root === false || !is_dir($root)) {
            return ['Package root directory does not exist.'];
        }
        if ($name === '' || $name === '.' || $name === '..' || strpbrk($name, "/\\\0") !== false) {
            return ['Invalid package directory name.'];
        }
        $path = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . str_replace(' ', '', $name);
        if (!file_exists($path) && !is_link($path)) {
            return [];
        }
        if (is_link($path) || !is_dir($path)) {
            return @unlink($path) ? [] : ["Failed to delete: {$path}"];
        }
        $errors = [];
        $this->scanDirectory($path, $errors);
        foreach ($this->scanned['file'] as $file) {
            if (!@unlink($file)) {
                $errors[] = "Failed to delete file: {$file}";
            }
        }
        foreach (array_reverse($this->scanned['dir']) as $dir) {
            if (!@rmdir($dir)) {
                $errors[] = "Failed to delete directory: {$dir}";
            }
        }
        if (!@rmdir($path)) {
            $errors[] = "Failed to delete package directory: {$path}";
        }
        return $errors;
    }

scanDirectory

Source line 48

private function scanDirectory(string $dir, array &$errors): void

Parameters

Parameter Type, modifiers and default Purpose
$dir string $dir Operation input; the exact use, validation and propagation are shown in the implementation trace.
$errors array &$errors Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->scanned['dir'][], $this->scanned['file'][].

Implementation, validation guards and interaction order
private function scanDirectory(string $dir, array &$errors): void
    {
        $entries = @scandir($dir);
        if ($entries === false) {
            $errors[] = "Failed to scan directory: {$dir}";
            return;
        }
        foreach ($entries as $entry) {
            if ($entry === '.' || $entry === '..') {
                continue;
            }
            $fullPath = $dir . DIRECTORY_SEPARATOR . $entry;
            // Unlink links themselves, including directory and dangling links.
            if (!is_link($fullPath) && is_dir($fullPath)) {
                $this->scanned['dir'][] = $fullPath;
                $this->scanDirectory($fullPath, $errors);
            } else {
                $this->scanned['file'][] = $fullPath;
            }
        }
    }

Related types

Clone this wiki locally