Sindbad~EG File Manager
<?php
/*
-------------------------------------------------------------------------
Screenshot
Copyright (C) 2020-2021 by Curtis Conard
https://github.com/cconard96/glpi-screenshot-plugin
-------------------------------------------------------------------------
LICENSE
This file is part of Screenshot.
Screenshot is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Screenshot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Screenshot. If not, see <http://www.gnu.org/licenses/>.
----
/**
* Handles migrating between plugin versions
*/
class PluginScreenshotMigration
{
private const BASE_VERSION = '1.0.0';
/** @var Migration */
protected $glpiMigration;
/** @var DBmysql */
protected $db;
public function __construct(string $version)
{
global $DB;
/** @phpstan-ignore-next-line Error exists in core, not plugin */
$this->glpiMigration = new Migration($version);
$this->db = $DB;
}
public function applyMigrations(): void
{
$rc = new ReflectionClass($this);
$otherMigrationFunctions = array_map(static function ($rm) {
return $rm->getShortName();
}, array_filter($rc->getMethods(), static function ($m) {
return preg_match('/(?<=^apply_)(.*)(?=_migration$)/', $m->getShortName());
}));
if (count($otherMigrationFunctions)) {
// Map versions to functions
$versionMap = [];
foreach ($otherMigrationFunctions as $function) {
$ver = str_replace(['apply_', '_migration', '_'], ['', '', '.'], $function);
$versionMap[$ver] = $function;
}
// Sort semantically
uksort($versionMap, 'version_compare');
// Get last known recorded version. If none exists, assume this is 1.0.0 (start migration from beginning).
// Migrations should be replayable so nothing should be lost on multiple runs.
$lastKnownVersion = Config::getConfigurationValues('plugin:screenshot')['plugin_version'] ?? self::BASE_VERSION;
// Call each migration in order starting from the last known version
foreach ($versionMap as $version => $func) {
// Last known version is the same or greater than release version
if (version_compare($lastKnownVersion, $version, '<=')) {
$this->$func();
$this->glpiMigration->executeMigration();
if ($version !== self::BASE_VERSION) {
$this->setPluginVersionInDB($version);
$lastKnownVersion = $version;
}
}
}
}
}
private function setPluginVersionInDB(string $version): void
{
$this->db->updateOrInsert(Config::getTable(), [
'value' => $version,
'context' => 'plugin:screenshot',
'name' => 'plugin_version'
], [
'context' => 'plugin:screenshot',
'name' => 'plugin_version'
]);
}
/**
* Apply the migrations for the base plugin version (1.0.0).
*/
private function apply_1_0_0_migration(): void
{
// No DB change
}
private function apply_1_1_0_migration(): void
{
$this->glpiMigration->addRight('plugin_screenshot_recording', CREATE);
}
private function apply_1_1_3_migration(): void
{
// Add previously missing default format
$this->glpiMigration->addConfig([
'screenshot_format' => 'image/png'
], 'plugin:screenshot');
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists