diff --git a/config/services.yaml b/config/services.yaml index 56ce3f10..dfe3de8f 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -73,6 +73,11 @@ services: tags: - { name: "doctrine.orm.entity_listener" } + App\EventSubscriber\MigrationListener: + tags: + - { name: 'doctrine.event_subscriber' } + + tree_invalidation_listener: class: App\EntityListeners\TreeCacheInvalidationListener tags: diff --git a/src/Entity/LogSystem/AbstractLogEntry.php b/src/Entity/LogSystem/AbstractLogEntry.php index c5812f4e..a7750b04 100644 --- a/src/Entity/LogSystem/AbstractLogEntry.php +++ b/src/Entity/LogSystem/AbstractLogEntry.php @@ -150,6 +150,12 @@ abstract class AbstractLogEntry extends DBElement */ protected $extra = []; + public function __construct() + { + $this->timestamp = new DateTime(); + $this->level = self::LEVEL_WARNING; + } + /** * Get the user that caused the event associated with this log entry. * @return User diff --git a/src/Entity/LogSystem/DatabaseUpdatedLogEntry.php b/src/Entity/LogSystem/DatabaseUpdatedLogEntry.php index a51af69c..9d7cb9af 100644 --- a/src/Entity/LogSystem/DatabaseUpdatedLogEntry.php +++ b/src/Entity/LogSystem/DatabaseUpdatedLogEntry.php @@ -31,9 +31,11 @@ class DatabaseUpdatedLogEntry extends AbstractLogEntry { protected $typeString = "database_updated"; - public function __construct() + public function __construct(string $oldVersion, string $newVersion) { - throw new LogEntryObsoleteException(); + parent::__construct(); + $this->extra['o'] = $oldVersion; + $this->extra['n'] = $newVersion; } /** @@ -42,25 +44,26 @@ class DatabaseUpdatedLogEntry extends AbstractLogEntry */ public function isSuccessful(): bool { - return $this->extra['s']; + //We dont save unsuccessful updates now, so just assume it to save space. + return $this->extra['s'] ?? true; } /** * Gets the database version before update. * @return int */ - public function getOldVersion(): int + public function getOldVersion(): string { - return $this->extra['o']; + return (string) $this->extra['o']; } /** * Gets the (target) database version after update. * @return int */ - public function getNewVersion(): int + public function getNewVersion(): string { - return $this->extra['n']; + return (string) $this->extra['n']; } } \ No newline at end of file diff --git a/src/Entity/LogSystem/UserLoginLogEntry.php b/src/Entity/LogSystem/UserLoginLogEntry.php index cbfbc0f9..734010d5 100644 --- a/src/Entity/LogSystem/UserLoginLogEntry.php +++ b/src/Entity/LogSystem/UserLoginLogEntry.php @@ -35,6 +35,7 @@ class UserLoginLogEntry extends AbstractLogEntry public function __construct(string $ip_address, bool $anonymize = true) { + parent::__construct(); $this->level = self::LEVEL_INFO; $this->setIPAddress($ip_address, $anonymize); } diff --git a/src/Entity/LogSystem/UserLogoutLogEntry.php b/src/Entity/LogSystem/UserLogoutLogEntry.php index c99c2b55..b3fe0124 100644 --- a/src/Entity/LogSystem/UserLogoutLogEntry.php +++ b/src/Entity/LogSystem/UserLogoutLogEntry.php @@ -35,6 +35,7 @@ class UserLogoutLogEntry extends AbstractLogEntry public function __construct(string $ip_address, bool $anonymize = true) { + parent::__construct(); $this->level = self::LEVEL_INFO; $this->setIPAddress($ip_address, $anonymize); } diff --git a/src/EventSubscriber/MigrationListener.php b/src/EventSubscriber/MigrationListener.php new file mode 100644 index 00000000..dd7c2441 --- /dev/null +++ b/src/EventSubscriber/MigrationListener.php @@ -0,0 +1,87 @@ +eventLogger = $eventLogger; + } + + public function onMigrationsMigrated(MigrationsEventArgs $args): void + { + //Dont do anything if this was a dry run + if ($args->isDryRun()) { + return; + } + + //Save the version after the migration + $this->new_version = $args->getConfiguration()->getCurrentVersion(); + + //After everything is done, write the results to DB log + $this->old_version = empty($this->old_version) ? 'legacy/empty' : $this->old_version; + $this->new_version = empty($this->new_version) ? 'unknown' : $this->new_version; + + + try { + $log = new DatabaseUpdatedLogEntry($this->old_version, $this->new_version); + $this->eventLogger->logAndFlush($log); + } catch (\Exception $exception) { + //Ignore any exception occuring here... + } + + } + + + public function onMigrationsMigrating(MigrationsEventArgs $args): void + { + // Save the version before any migration + if ($this->old_version == null) { + $this->old_version = $args->getConfiguration()->getCurrentVersion(); + } + } + + /** + * @inheritDoc + */ + public function getSubscribedEvents() + { + return [ + Events::onMigrationsMigrated, + Events::onMigrationsMigrating, + ]; + } +} \ No newline at end of file