diff --git a/Module/ModuleAbstract.php b/Module/ModuleAbstract.php index 3a7a7e40f..61e189d73 100755 --- a/Module/ModuleAbstract.php +++ b/Module/ModuleAbstract.php @@ -105,6 +105,14 @@ abstract class ModuleAbstract */ public bool $active = true; + /** + * Auditor for logging. + * + * @var null|ModuleAbstract + * @since 1.0.0 + */ + public static ?ModuleAbstract $auditor = null; + /** * Constructor. * @@ -115,6 +123,11 @@ abstract class ModuleAbstract public function __construct(ApplicationAbstract $app = null) { $this->app = $app ?? new class() extends ApplicationAbstract {}; + + if (self::$auditor === null && static::ID !== 1006200000) { + self::$auditor = $this->app?->moduleManager->get('Auditor', 'Api'); + self::$auditor = static::$auditor::ID === 0 ? null : self::$auditor; + } } /** @@ -732,7 +745,8 @@ protected function createModel(int $account, mixed $obj, string | \Closure $mapp $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogCreate(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogCreate(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } @@ -777,7 +791,8 @@ protected function createModels(int $account, array $objs, string | \Closure $ma $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogCreate(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogCreate(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } } @@ -823,7 +838,8 @@ protected function updateModel(int $account, mixed $old, mixed $new, string | \C $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogUpdate(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogUpdate(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } @@ -867,7 +883,8 @@ protected function deleteModel(int $account, mixed $obj, string | \Closure $mapp $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogDelete(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogDelete(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } @@ -915,7 +932,8 @@ protected function createModelRelation( $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogRelationCreate(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogRelationCreate(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } @@ -955,7 +973,8 @@ protected function deleteModelRelation(int $account, mixed $rel1, mixed $rel2, s $ip, ]; - $this->app->moduleManager->get('Auditor', 'Api')->eventLogRelationDelete(...$data); + /** @phpstan-ignore-next-line */ + self::$auditor?->eventLogRelationDelete(...$data); $this->app->eventManager->triggerSimilar('POST:Module:' . $trigger, '', $data); } } diff --git a/tests/Module/ModuleAbstractTest.php b/tests/Module/ModuleAbstractTest.php index 1d0841f56..ff14928a7 100755 --- a/tests/Module/ModuleAbstractTest.php +++ b/tests/Module/ModuleAbstractTest.php @@ -21,6 +21,7 @@ use phpOMS\Message\Http\HttpRequest; use phpOMS\Message\Http\HttpResponse; use phpOMS\Module\ModuleAbstract; +use phpOMS\Module\ModuleManager; use phpOMS\tests\DataStorage\Database\TestModel\BaseModel; use phpOMS\tests\DataStorage\Database\TestModel\BaseModelMapper; use phpOMS\tests\DataStorage\Database\TestModel\ManyToManyRelModel; @@ -55,8 +56,9 @@ protected function setUp() : void public function __construct() { - $this->app = new class() extends ApplicationAbstract {}; - $this->app->eventManager = new EventManager(); + $this->app = new class() extends ApplicationAbstract {}; + $this->app->eventManager = new EventManager(); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__); } public function fillJson(HttpRequest $request, HttpResponse $response, string $status, string $title, string $message, array $data) : void