mc-cms-namelessmc/core/classes/Misc/UpgradeScript.php

168 lines
4.6 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Used for abstracting common tasks done during upgrades.
*
* @package NamelessMC\Misc
* @author Aberdeener
* @version 2.0.0-pr13
* @license MIT
*/
abstract class UpgradeScript
{
2021-12-07 22:14:12 -08:00
protected Cache $_cache;
public function __construct()
{
2021-12-07 22:14:12 -08:00
$this->_cache = new Cache(
['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']
);
}
2021-12-07 22:14:12 -08:00
/**
* Get instance of UpgradeScript for a specific NamelessMC version, null if it doesn't exist.
2021-12-07 22:14:12 -08:00
*
* @param string $current_version Current NamelessMC version (ie: `2.0.0-pr12`, `2.0.0`)
2021-12-07 22:14:12 -08:00
* @return UpgradeScript|null Instance of UpgradeScript from file
*/
public static function get(string $current_version): ?UpgradeScript
{
2021-12-07 22:14:12 -08:00
$path = ROOT_PATH . '/core/includes/updates/' . str_replace('.', '', $current_version) . '.php';
if (!file_exists($path)) {
return null;
}
2022-04-23 19:30:31 +01:00
return require $path;
2021-12-07 22:14:12 -08:00
}
/**
* Execute this UpgradeScript.
2021-12-07 22:14:12 -08:00
*/
abstract public function run(): void;
2022-06-10 18:58:36 -06:00
/**
* Logs a message to the screen and the warning-log.log file.
*
* @param string $message Message to log
*/
protected function log(string $message): void
{
2022-06-10 18:58:36 -06:00
echo $message . '<br/>';
2022-08-05 22:37:24 +01:00
ErrorHandler::logWarning('UPGRADING EXCEPTION: ' . $message);
2022-06-10 18:58:36 -06:00
}
/**
* Run a single database query.
*
* @param Closure $query Function which returns the query
* @return mixed The result of the closure, if any
*/
protected function databaseQuery(Closure $query)
{
return $this->databaseQueries([$query])[0];
}
/**
* Run multiple queries.
*
* @param Closure[] $queries Array of queries to execute one after another
* @return array Results from queries in order
*/
protected function databaseQueries(array $queries): array
{
$results = [];
foreach ($queries as $query) {
try {
2022-04-23 12:18:54 -07:00
$results[] = $query(DB::getInstance());
} catch (Exception $exception) {
$results[] = null;
2022-06-10 18:58:36 -06:00
$this->log($exception->getMessage());
}
}
2021-11-24 15:42:13 -08:00
return $results;
}
/**
* Delete one or more folders or files in a path.
*
* @param string $path Prefix path to append to each of the files in `$files` array
* @param array $files Name of folders/files in `$path` to delete. Use `*` for all folders/files
* @param bool $recursive Whether to recursively delete
*/
protected function deleteFilesInPath(string $path, array $files, bool $recursive = false): void
{
if (in_array('*', $files)) {
$files = scandir($path);
}
foreach ($files as $file) {
if ($file[0] == '.') {
continue;
}
2022-01-15 15:18:05 -08:00
if (file_exists($newFile = implode(DIRECTORY_SEPARATOR, [$path, $file]))) {
if (is_dir($newFile)) {
if ($recursive) {
$this->deleteFilesInPath($newFile, ['*'], true);
2022-05-15 15:47:18 -06:00
$this->deleteFiles($newFile);
}
} else {
2022-05-15 15:47:18 -06:00
$this->deleteFiles($newFile);
}
} else {
2022-06-10 18:58:36 -06:00
$this->log("'$newFile' does not exist, cannot delete.");
}
}
}
2021-12-07 22:14:12 -08:00
/**
* Delete a single folder or file.
2021-12-07 22:14:12 -08:00
*
* @param string|array $paths Path to folder or file to delete
2021-12-07 22:14:12 -08:00
*/
protected function deleteFiles($paths): void
{
foreach ((array) $paths as $path) {
$path = ROOT_PATH . '/' . $path;
if (!file_exists($path)) {
2022-06-10 18:58:36 -06:00
$this->log("'$path' does not exist, cannot delete.");
continue;
}
2021-12-07 22:14:12 -08:00
if (!is_writable($path)) {
2022-06-10 18:58:36 -06:00
$this->log("'$path' is not writable, cannot delete.");
return;
}
if (is_dir($path) && !rmdir($path)) {
2022-06-10 18:58:36 -06:00
$this->log("Could not delete '$path', is it empty?");
}
2021-12-07 22:14:12 -08:00
unlink($path);
}
2021-12-07 22:14:12 -08:00
}
2022-05-31 11:29:47 -06:00
/**
* Execute any pending database migrations.
*/
protected function runMigrations(): void
{
PhinxAdapter::migrate('Core');
2022-05-31 11:29:47 -06:00
}
/**
* Update the version of this NamelessMC website in the database.
*
* @param string $version Version to set
*/
protected function setVersion(string $version): void
{
Settings::set('nameless_version', $version);
Settings::set('version_update', null);
}
2022-01-15 15:18:05 -08:00
}