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; namespace Dotclear\Plugin\Uninstaller;
use ArrayObject;
/** /**
* Cleaner abstract class. * Cleaner abstract class.
* *
@ -55,17 +53,6 @@ abstract class AbstractCleaner
$this->actions = $actions; $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. * Initialize Cleaner properties.
* *

View File

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

View File

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