better way to manage cleaners stack

This commit is contained in:
Jean-Christian Paul Denis 2023-04-19 23:50:27 +02:00
parent 69cb2d8e9d
commit 09b9939b65
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
3 changed files with 29 additions and 35 deletions

View File

@ -14,8 +14,6 @@ declare(strict_types=1);
namespace Dotclear\Plugin\Uninstaller;
use ArrayObject;
/**
* Cleaner abstract class.
*
@ -55,17 +53,6 @@ abstract class AbstractCleaner
$this->actions = $actions;
}
/**
* Create and add the Cleaner to a stack.
*
* @param ArrayObject $stack The cleaners stack
*/
public static function create(ArrayObject $stack): void
{
$class = static::class;
$stack->append(new $class());
}
/**
* Initialize Cleaner properties.
*

View File

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Dotclear\Plugin\Uninstaller;
use ArrayObject;
use dcCore;
use Exception;
@ -27,24 +26,28 @@ class Cleaners
private array $cleaners = [];
/**
* Constructor register the cleaners.
* Contructor load cleaners.
*/
public function __construct()
{
$cleaners = new ArrayObject();
# --BEHAVIOR-- UninstallerConstruct: Uninstaller
dcCore::app()->callBehavior('UninstallerCleanersConstruct', $this);
}
try {
# --BEHAVIOR-- UninstallerAddCleaner: ArrayObject
dcCore::app()->callBehavior('UninstallerAddCleaner', $cleaners);
foreach ($cleaners as $cleaner) {
if (is_a($cleaner, AbstractCleaner::class) && !isset($this->cleaners[$cleaner->id])) {
$this->cleaners[$cleaner->id] = $cleaner;
}
}
} catch (Exception $e) {
dcCore::app()->error->add($e->getMessage());
/**
* Add a cleaner.
*
* @param AbstractCleaner $cleaner The cleaner instance
*
* @return Cleaners Self instance
*/
public function add(AbstractCleaner $cleaner): Cleaners
{
if (!isset($this->cleaners[$cleaner->id])) {
$this->cleaners[$cleaner->id] = $cleaner;
}
return $this;
}
/**

View File

@ -35,14 +35,18 @@ class Prepend extends dcNsProcess
}
// Add cleaners to Uninstaller
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Settings::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Tables::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Versions::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Logs::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Caches::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Vars::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Themes::class, 'create']);
dcCore::app()->addBehavior('UninstallerAddCleaner', [Cleaner\Plugins::class, 'create']);
dcCore::app()->addBehavior('UninstallerCleanersConstruct', function (Uninstaller $uninstaller): void {
$uninstaller->cleaners
->add(new Cleaner\Settings())
->add(new Cleaner\Tables())
->add(new Cleaner\Versions())
->add(new Cleaner\Logs())
->add(new Cleaner\Caches())
->add(new Cleaner\Vars())
->add(new Cleaner\Themes())
->add(new Cleaner\Plugins())
;
});
return true;
}