From 033c4db9144f2b56851d4f10caae2406874e65c1 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 22 Aug 2021 13:33:27 +0100 Subject: [PATCH] [MODULES] Add function to defer module initialization and cleanup to plugin and component. Add example in Avatar component Forward onInitializeModule to onInitializePlugin if the component is a plugin --- components/Avatar/Avatar.php | 4 ++++ src/Core/Modules/Module.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/components/Avatar/Avatar.php b/components/Avatar/Avatar.php index 677e426623..da5ddb49df 100644 --- a/components/Avatar/Avatar.php +++ b/components/Avatar/Avatar.php @@ -32,6 +32,10 @@ use Symfony\Component\HttpFoundation\Request; class Avatar extends Component { + public function onInitializeComponent() + { + } + public function onAddRoute($r): bool { $r->connect('avatar', '/{gsactor_id<\d+>}/avatar/{size?full}', [Controller\Avatar::class, 'avatar_view']); diff --git a/src/Core/Modules/Module.php b/src/Core/Modules/Module.php index 3adf9c8861..e9bb32d983 100644 --- a/src/Core/Modules/Module.php +++ b/src/Core/Modules/Module.php @@ -60,4 +60,25 @@ abstract class Module } return $obj; } + + // ------- Module initialize and cleanup ---------- + + private function defer(string $cycle) + { + $type = ucfirst(static::MODULE_TYPE); + if (method_exists($this, $method = "on{$cycle}{$type}")) { + $this->{$method}(); + } + } + + // Can't use __call or it won't be found by our event function finder + public function onInitializeModule() + { + $this->defer('Initialize'); + } + + public function onCleanupModule() + { + $this->defer('Cleanup'); + } }