/** * Cleanup the Audit data * * @return int|void */ public function actionCleanup() { /** @var Audit $audit */ $audit = Yii::$app->getModule(Audit::findModuleIdentifier()); $panels = $this->panels !== null ? explode(',', $this->panels) : array_keys($audit->panels); // summary $this->preCleanupSummary($this->entry, $panels, $this->age); // confirm if ($this->confirm('Cleanup the above data?')) { // cleanup panels foreach ($panels as $id) { if (!$this->cleanupPanel($id, $this->age)) { $this->stdout("\nCleanup failed. The rest of the cleanups are canceled.\n", Console::FG_RED); return self::EXIT_CODE_ERROR; } } // cleanup audit_entry if ($this->entry) { if (!$this->cleanupEntry($this->age)) { $this->stdout("\nCleanup failed.\n", Console::FG_RED); return self::EXIT_CODE_ERROR; } } // success! $this->stdout("\nCleanup was successful.\n", Console::FG_GREEN); } return self::EXIT_CODE_NORMAL; }
/** * @param Exception $exception */ public function logException($exception) { try { $isMemoryError = strncmp($exception->getMessage(), 'Allowed memory size of', 22) === 0; /** @var Audit $audit */ $audit = Audit::getInstance(); if (!$audit && !$isMemoryError) { // Only attempt to load the module if this isn't an out of memory error, not enough room otherwise $audit = \Yii::$app->getModule(Audit::findModuleIdentifier()); } if (!$audit) { throw new \Exception('Audit module cannot be loaded'); } $entry = $audit->getEntry(!$isMemoryError); if ($entry) { /** @var ErrorPanel $errorPanel */ $errorPanel = $audit->getPanel($audit->findPanelIdentifier(ErrorPanel::className())); $errorPanel->log($entry->id, $exception); $entry->finalize(); } } catch (\Exception $e) { // if we catch an exception here, let it slide, we don't want recursive errors killing the script } parent::logException($exception); }
/** * Bootstrap method to be called during application bootstrap stage. * * @param Application $app the application currently running */ public function bootstrap($app) { // Make sure to register the base folder as alias as well or things like assets won't work anymore \Yii::setAlias('@bedezign/yii2/audit', __DIR__); if ($app instanceof \yii\console\Application) { $app->controllerMap['audit'] = 'bedezign\\yii2\\audit\\commands\\AuditController'; } $moduleName = Audit::findModuleIdentifier(); if ($moduleName) { // The module was added in the configuration, make sure to add it to the application bootstrap so it gets loaded $app->bootstrap[] = $moduleName; $app->bootstrap = array_unique($app->bootstrap, SORT_REGULAR); } if ($app->has('i18n')) { $app->i18n->translations['audit'] = ['class' => 'yii\\i18n\\PhpMessageSource', 'sourceLanguage' => 'en', 'basePath' => '@bedezign/yii2/audit/messages']; } }
/** * @param Exception $exception */ public function logException($exception) { try { $isMemoryError = strncmp($exception->getMessage(), 'Allowed memory size of', 22) === 0; $module = Audit::getInstance(); if (!$module && !$isMemoryError) { // Only attempt to load the module if this isn't an out of memory error, not enough room otherwise $module = \Yii::$app->getModule(Audit::findModuleIdentifier()); } if (!$module) { throw new \Exception('Audit module cannot be loaded'); } $entry = $module->getEntry(!$isMemoryError); if ($entry) { AuditError::log($entry, $exception); $entry->finalize(); } } catch (\Exception $e) { // if we catch an exception here, let it slide, we don't want recursive errors killing the script } parent::logException($exception); }
/** * @return models\AuditEntry|null|static * @throws \Exception */ protected function getAuditEntryId() { $module = Audit::getInstance(); if (!$module) { $module = \Yii::$app->getModule(Audit::findModuleIdentifier()); } if (!$module) { throw new \Exception('Audit module cannot be loaded'); } return Audit::getInstance()->getEntry(true)->id; }