diff --git a/src/Classes/Notification.php b/src/Classes/Notification.php index 46776c38..e787c919 100644 --- a/src/Classes/Notification.php +++ b/src/Classes/Notification.php @@ -15,6 +15,9 @@ class Notification { + public const TYPE_ERROR = 'error'; + public const TYPE_WARNING = 'warning'; + private const MMLC_SESSION_NAME = 'mmlc'; /** diff --git a/src/Classes/SelfUpdater.php b/src/Classes/SelfUpdater.php index bfe15cbe..9115e9be 100644 --- a/src/Classes/SelfUpdater.php +++ b/src/Classes/SelfUpdater.php @@ -21,6 +21,8 @@ use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest; use RobinTheHood\ModifiedModuleLoaderClient\Semver\Filter; use RobinTheHood\ModifiedModuleLoaderClient\Semver\Sorter; +use RuntimeException; +use Throwable; class SelfUpdater { @@ -98,13 +100,20 @@ public function update(MmlcVersionInfo $mmlcVersionInfo): void $this->createRestore($mmlcVersionInfo); $this->download($mmlcVersionInfo); - $this->backup($mmlcVersionInfo); $this->untar($mmlcVersionInfo); $this->verifyUntar($mmlcVersionInfo); - $this->install($mmlcVersionInfo); - $this->setupConfig($mmlcVersionInfo); - $this->setupVersion($mmlcVersionInfo); - $this->verifyUpdate($mmlcVersionInfo); + + $check = $this->systemCheck($mmlcVersionInfo); + + if ($check) { + $this->backup($mmlcVersionInfo); + $this->install($mmlcVersionInfo); + $this->setupConfig($mmlcVersionInfo); + $this->setupVersion($mmlcVersionInfo); + $this->verifyUpdate($mmlcVersionInfo); + } + + $this->remove($mmlcVersionInfo); $this->removeRestore($mmlcVersionInfo); opcache_reset(); @@ -216,7 +225,11 @@ private function install(MmlcVersionInfo $mmlcVersionInfo): void $files = FileHelper::scanDir($srcPath, FileHelper::FILES_AND_DIRS, true); FileHelper::moveFilesTo($files, $srcPath, $destPath); + } + private function remove(MmlcVersionInfo $mmlcVersionInfo): void + { + $srcPath = $this->appRoot . '/ModifiedModuleLoaderClient'; system('rm -rf ' . $srcPath); } @@ -285,6 +298,53 @@ private function verifyUpdate(MmlcVersionInfo $mmlcVersionInfo): bool return true; } + private function systemCheck(MmlcVersionInfo $mmlcVersionInfo): bool + { + try { + $systemCheck = $this->getSystemCheckObj(); + $check = $systemCheck->check(); + if ($check['result'] === 'passed') { + return true; + } else { + Notification::pushFlashMessage([ + 'text' => "Update canceled - Can not update MMLC. Not all system requirements are met.
\n" + . json_encode($check['checks'], JSON_PRETTY_PRINT), + 'type' => Notification::TYPE_ERROR + ]); + } + } catch (RuntimeException $e) { + Notification::pushFlashMessage([ + 'text' => "Update canceled - " . $e->getMessage(), + 'type' => Notification::TYPE_ERROR + ]); + } + + return false; + } + + private function getSystemCheckObj() + { + $systemCheckFilePath = $this->appRoot . '/ModifiedModuleLoaderClient/src/Classes/SystemCheck.php'; + // $systemCheckFilePath = $this->appRoot . '/src/Classes/SystemCheck.php'; // Für Testzwecke + + if (!file_exists($systemCheckFilePath)) { + throw new RuntimeException("Can not find file $systemCheckFilePath in downloaded .tar file"); + } + + try { + $fileContent = file_get_contents($systemCheckFilePath); + $fileContent = str_replace('class SystemCheck', 'class SystemCheckNew', $fileContent); + $fileContent = str_replace(' + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace RobinTheHood\ModifiedModuleLoaderClient; + +class SystemCheck +{ + public const RESULT_PASSED = 'passed'; + public const RESULT_FAILED = 'failed'; + + public const REQUIRED_PHP_VERSION = '7.4.0'; + + public function check(array $options = []): array + { + $php = [ + 'is' => PHP_VERSION, + 'require' => '>=' . self::REQUIRED_PHP_VERSION, + ]; + + if (version_compare(PHP_VERSION, self::REQUIRED_PHP_VERSION, '<')) { + $php['result'] = self::RESULT_FAILED; + $result = self::RESULT_FAILED; + } else { + $php['result'] = self::RESULT_PASSED; + $result = self::RESULT_PASSED; + } + + return [ + 'result' => $result, + 'checks' => [ + 'php' => $php + ] + ]; + } +} diff --git a/src/Templates/SelfUpdate.tmpl.php b/src/Templates/SelfUpdate.tmpl.php index 206d3eb4..d5094d1f 100644 --- a/src/Templates/SelfUpdate.tmpl.php +++ b/src/Templates/SelfUpdate.tmpl.php @@ -2,7 +2,9 @@ defined('LOADED_FROM_INDEX') && LOADED_FROM_INDEX ?? die('Access denied.'); use RobinTheHood\ModifiedModuleLoaderClient\Config; +use RobinTheHood\ModifiedModuleLoaderClient\ViewModels\NotificationViewModel; +$notificationView = new NotificationViewModel(); ?> @@ -15,6 +17,8 @@
+ renderFlashMessages() ?> +

MMLC - Modified Module Loader Client