From 7df8349afe8b2cc32711b1e2a9635784a6551aaf Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 22 Jun 2023 02:15:06 +0200 Subject: [PATCH 01/21] chore: add new classes --- src/Classes/ArchiveHandler.php | 100 +++++++++++++++++++++++++++++++++ src/Classes/ArchiveName.php | 58 +++++++++++++++++++ src/Classes/ArchiveNew.php | 69 +++++++++++++++++++++++ src/Classes/ArchivePuller.php | 71 +++++++++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 src/Classes/ArchiveHandler.php create mode 100644 src/Classes/ArchiveName.php create mode 100644 src/Classes/ArchiveNew.php create mode 100644 src/Classes/ArchivePuller.php diff --git a/src/Classes/ArchiveHandler.php b/src/Classes/ArchiveHandler.php new file mode 100644 index 00000000..af3b6f59 --- /dev/null +++ b/src/Classes/ArchiveHandler.php @@ -0,0 +1,100 @@ +localModuleLoader = $localModuleLoader; + $this->modulesRootPath = $modulesRootPath; + } + + public function pack(ArchiveNew $archive): void + { + $module = $this->localModuleLoader->loadByArchiveNameAndVersion( + (string) $archive->getArchiveName(), + (string) $archive->getVersion() + ); + + if (!$module) { + throw new \RuntimeException("Failed to load module for archive: " . $archive->getArchiveName()); + } + + $this->createDirIfNotExists($archive->getArchivesRootPath()); + $this->deleteFileIfExists($archive->getFilePath()); + + $filePaths = FileHelper::scanDirRecursive( + $this->getModulePathFromModule($module), + FileHelper::FILES_ONLY + ); + + set_time_limit(60 * 10); + $tarArchive = new \PharData($archive->getFilePath()); + foreach ($filePaths as $filePath) { + if (file_exists($filePath)) { + $tarPath = FileHelper::stripBasePath($this->modulesRootPath, $filePath); + $tarArchive->addFile($filePath, $tarPath); + } + } + } + + public function extract(ArchiveNew $archive, bool $external = false): void + { + $modulePath = $this->getModulePathFromArchive($archive); + if (file_exists($modulePath)) { + throw new \RuntimeException("Module already exists for archive: " . $archive->getArchiveName()); + } + + $this->createDirIfNotExists($this->modulesRootPath); + + $tarArchive = new \PharData($archive->getFilePath()); + $tarArchive->extractTo($this->modulesRootPath); + + if ($external) { + $vendorDirPath = $this->modulesRootPath . '/' . $archive->getArchiveName()->getVendorName(); + $moduleDirPath = $this->modulesRootPath . '/' . $archive->getArchiveName(); + + $this->createDirIfNotExists($vendorDirPath); + $this->createDirIfNotExists($moduleDirPath); + + rename( + $this->modulesRootPath . '/' . $tarArchive->getFileName(), + $this->getModulePathFromArchive($archive) + ); + } + } + + private function getModulePathFromArchive(ArchiveNew $archiveNew): string + { + return $this->modulesRootPath . '/' . $archiveNew->getArchiveName() . '/' . $archiveNew->getVersion(); + } + + private function getModulePathFromModule(Module $module): string + { + return $module->getLocalRootPath() . DIRECTORY_SEPARATOR . $module->getModulePath(); + } + + private function createDirIfNotExists(string $path): void + { + if (!@mkdir($path) && !is_dir($path)) { + throw new \RuntimeException("Failed to create directory: " . $path); + } + } + + private function deleteFileIfExists(string $path): void + { + if (!@unlink($path) && file_exists($path)) { + throw new \RuntimeException("Failed to delete file: " . $path); + } + } +} diff --git a/src/Classes/ArchiveName.php b/src/Classes/ArchiveName.php new file mode 100644 index 00000000..710668b5 --- /dev/null +++ b/src/Classes/ArchiveName.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace RobinTheHood\ModifiedModuleLoaderClient; + +class ArchiveName +{ + private string $value; + + private string $vendorName; + + private string $moduleName; + + public function __construct(string $value) + { + if (!$this->isValidArchiveName($value)) { + throw new \InvalidArgumentException('No valid ArchiveName'); + } + + $this->value = $value; + + $parts = explode('/', $value); + $this->vendorName = $parts[0]; + $this->moduleName = $parts[1]; + } + + public function getModuleName(): string + { + return $this->moduleName; + } + + public function getVendorName(): string + { + return $this->vendorName; + } + + public function __toString(): string + { + return $this->value; + } + + private function isValidArchiveName(string $archiveName): bool + { + $pattern = '/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/'; + + return preg_match($pattern, $archiveName) === 1; + } +} diff --git a/src/Classes/ArchiveNew.php b/src/Classes/ArchiveNew.php new file mode 100644 index 00000000..bb8dd516 --- /dev/null +++ b/src/Classes/ArchiveNew.php @@ -0,0 +1,69 @@ +archiveName = $archiveName; + $this->archivesRootPath = $archivesRootPath; + // $this->urlRootPath = $urlRootPath; + $this->version = $version; + } + + public function getArchiveName(): ArchiveName + { + return $this->archiveName; + } + + public function getVersion(): Version + { + return $this->version; + } + + public function getArchivesRootPath(): string + { + return $this->archivesRootPath; + } + + // public function getUrlRootPath(): string + // { + // return $this->urlRootPath; + // } + + public function getFileName(): string + { + return + str_replace('/', '_', $this->getArchiveName()->__toString()) + . '_' + . $this->getVersion() + . '.tar'; + } + + public function getFilePath(): string + { + return $this->getArchivesRootPath() . '/' . $this->getFileName(); + } +} diff --git a/src/Classes/ArchivePuller.php b/src/Classes/ArchivePuller.php new file mode 100644 index 00000000..e895699b --- /dev/null +++ b/src/Classes/ArchivePuller.php @@ -0,0 +1,71 @@ +httpRequest = $httpRequest; + $this->parser = $parser; + $this->archivesRootPath = $archivesRootPath; + } + + public function pull(string $archiveName, string $version, string $url): ArchiveNew + { + $archive = $this->createArchive($archiveName, $version); + + $tarArchiveContent = $this->httpRequest->sendGetRequest($url); + + if (!$tarArchiveContent || !$this->isTarArchive($tarArchiveContent)) { + throw new RuntimeException("Failed to pull Archive: {$archiveName}:{$version}"); + } + + $this->createDirIfNotExists($archive->getArchivesRootPath()); + file_put_contents($archive->getFilePath(), $tarArchiveContent); + + return $archive; + } + + private function createArchive(string $archiveName, string $version): ArchiveNew + { + $archiveNameObj = new ArchiveName($archiveName); + $versionObj = $this->parser->parse($version); + + return new ArchiveNew($archiveNameObj, $versionObj, $this->archivesRootPath); + } + + private function isTarArchive($content): bool + { + // Überprüfen, ob $content mit der Tarball-Signatur beginnt + $tarballSignature = "\x1f\x8b\x08"; + $firstBytes = substr($content, 0, 3); + + return ($firstBytes === $tarballSignature); + } + + private function createDirIfNotExists(string $path): void + { + if (!@mkdir($path) && !is_dir($path)) { + throw new \RuntimeException("Failed to create directory: " . $path); + } + } +} From 1dfab48e8a4886088009987021bcc16faeac7f0b Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 22 Jun 2023 22:21:56 +0200 Subject: [PATCH 02/21] feat: move Archive classes to Archive and add create() methods --- .../{ArchiveNew.php => Archive/Archive.php} | 14 +++++++++++-- src/Classes/{ => Archive}/ArchiveHandler.php | 21 +++++++++++++++---- src/Classes/{ => Archive}/ArchiveName.php | 2 +- src/Classes/{ => Archive}/ArchivePuller.php | 17 ++++++++------- 4 files changed, 39 insertions(+), 15 deletions(-) rename src/Classes/{ArchiveNew.php => Archive/Archive.php} (76%) rename src/Classes/{ => Archive}/ArchiveHandler.php (82%) rename src/Classes/{ => Archive}/ArchiveName.php (95%) rename src/Classes/{ => Archive}/ArchivePuller.php (81%) diff --git a/src/Classes/ArchiveNew.php b/src/Classes/Archive/Archive.php similarity index 76% rename from src/Classes/ArchiveNew.php rename to src/Classes/Archive/Archive.php index bb8dd516..f913581c 100644 --- a/src/Classes/ArchiveNew.php +++ b/src/Classes/Archive/Archive.php @@ -2,17 +2,27 @@ declare(strict_types=1); -namespace RobinTheHood\ModifiedModuleLoaderClient; +namespace RobinTheHood\ModifiedModuleLoaderClient\Archive; +use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser; use RobinTheHood\ModifiedModuleLoaderClient\Semver\Version; -class ArchiveNew +class Archive { private ArchiveName $archiveName; private Version $version; private string $archivesRootPath; // private string $urlRootPath; + public static function create(string $archiveName, string $version, string $archivesRootPath): Archive + { + $archiveNameObj = new ArchiveName($archiveName); + $semverParser = Parser::create(); + $versionObj = $semverParser->parse($version); + + return new Archive($archiveNameObj, $versionObj, $archivesRootPath); + } + public function __construct( ArchiveName $archiveName, Version $version, diff --git a/src/Classes/ArchiveHandler.php b/src/Classes/Archive/ArchiveHandler.php similarity index 82% rename from src/Classes/ArchiveHandler.php rename to src/Classes/Archive/ArchiveHandler.php index af3b6f59..5728539f 100644 --- a/src/Classes/ArchiveHandler.php +++ b/src/Classes/Archive/ArchiveHandler.php @@ -2,10 +2,12 @@ declare(strict_types=1); -namespace RobinTheHood\ModifiedModuleLoaderClient; +namespace RobinTheHood\ModifiedModuleLoaderClient\Archive; +use RobinTheHood\ModifiedModuleLoaderClient\App; use RobinTheHood\ModifiedModuleLoaderClient\Helpers\FileHelper; use RobinTheHood\ModifiedModuleLoaderClient\Loader\LocalModuleLoader; +use RobinTheHood\ModifiedModuleLoaderClient\Module; class ArchiveHandler { @@ -13,13 +15,24 @@ class ArchiveHandler private string $modulesRootPath; + + public static function create(int $mode): ArchiveHandler + { + $localModuleLoader = LocalModuleLoader::create($mode); + $archiveHandler = new ArchiveHandler( + $localModuleLoader, + App::getModulesRoot() + ); + return $archiveHandler; + } + public function __construct(LocalModuleLoader $localModuleLoader, string $modulesRootPath) { $this->localModuleLoader = $localModuleLoader; $this->modulesRootPath = $modulesRootPath; } - public function pack(ArchiveNew $archive): void + public function pack(Archive $archive): void { $module = $this->localModuleLoader->loadByArchiveNameAndVersion( (string) $archive->getArchiveName(), @@ -48,7 +61,7 @@ public function pack(ArchiveNew $archive): void } } - public function extract(ArchiveNew $archive, bool $external = false): void + public function extract(Archive $archive, bool $external = false): void { $modulePath = $this->getModulePathFromArchive($archive); if (file_exists($modulePath)) { @@ -74,7 +87,7 @@ public function extract(ArchiveNew $archive, bool $external = false): void } } - private function getModulePathFromArchive(ArchiveNew $archiveNew): string + private function getModulePathFromArchive(Archive $archiveNew): string { return $this->modulesRootPath . '/' . $archiveNew->getArchiveName() . '/' . $archiveNew->getVersion(); } diff --git a/src/Classes/ArchiveName.php b/src/Classes/Archive/ArchiveName.php similarity index 95% rename from src/Classes/ArchiveName.php rename to src/Classes/Archive/ArchiveName.php index 710668b5..94838e50 100644 --- a/src/Classes/ArchiveName.php +++ b/src/Classes/Archive/ArchiveName.php @@ -11,7 +11,7 @@ declare(strict_types=1); -namespace RobinTheHood\ModifiedModuleLoaderClient; +namespace RobinTheHood\ModifiedModuleLoaderClient\Archive; class ArchiveName { diff --git a/src/Classes/ArchivePuller.php b/src/Classes/Archive/ArchivePuller.php similarity index 81% rename from src/Classes/ArchivePuller.php rename to src/Classes/Archive/ArchivePuller.php index e895699b..92c42b15 100644 --- a/src/Classes/ArchivePuller.php +++ b/src/Classes/Archive/ArchivePuller.php @@ -2,9 +2,10 @@ declare(strict_types=1); -namespace RobinTheHood\ModifiedModuleLoaderClient; +namespace RobinTheHood\ModifiedModuleLoaderClient\Archive; use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest; +use RobinTheHood\ModifiedModuleLoaderClient\App; use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser; use RuntimeException; @@ -29,7 +30,7 @@ public function __construct(HttpRequest $httpRequest, Parser $parser, string $ar $this->archivesRootPath = $archivesRootPath; } - public function pull(string $archiveName, string $version, string $url): ArchiveNew + public function pull(string $archiveName, string $version, string $url): Archive { $archive = $this->createArchive($archiveName, $version); @@ -45,21 +46,21 @@ public function pull(string $archiveName, string $version, string $url): Archive return $archive; } - private function createArchive(string $archiveName, string $version): ArchiveNew + private function createArchive(string $archiveName, string $version): Archive { $archiveNameObj = new ArchiveName($archiveName); $versionObj = $this->parser->parse($version); - return new ArchiveNew($archiveNameObj, $versionObj, $this->archivesRootPath); + return new Archive($archiveNameObj, $versionObj, $this->archivesRootPath); } private function isTarArchive($content): bool { - // Überprüfen, ob $content mit der Tarball-Signatur beginnt - $tarballSignature = "\x1f\x8b\x08"; - $firstBytes = substr($content, 0, 3); + // Überprüfen, ob $content die Tarball-Signatur beinhaltet + $tarballSignature = "ustar"; + $magic = substr($content, 257, 5); - return ($firstBytes === $tarballSignature); + return ($magic === $tarballSignature); } private function createDirIfNotExists(string $path): void From 2552a38fc8e72038d3480a938b715716bac3bec3 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 22 Jun 2023 22:22:34 +0200 Subject: [PATCH 03/21] refactor: use new Archiv in ModuleInstaller.php --- src/Classes/ModuleInstaller.php | 35 +++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Classes/ModuleInstaller.php b/src/Classes/ModuleInstaller.php index 1b21f414..2b79031b 100644 --- a/src/Classes/ModuleInstaller.php +++ b/src/Classes/ModuleInstaller.php @@ -18,6 +18,10 @@ use RobinTheHood\ModifiedModuleLoaderClient\Config; use RobinTheHood\ModifiedModuleLoaderClient\FileInfo; use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\ApiRequest; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\Archive as ArchiveArchive; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchiveHandler; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchiveName; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchivePuller; use RobinTheHood\ModifiedModuleLoaderClient\DependencyManager\Combination; use RobinTheHood\ModifiedModuleLoaderClient\DependencyManager\DependencyManager; use RobinTheHood\ModifiedModuleLoaderClient\Loader\LocalModuleLoader; @@ -38,12 +42,29 @@ class ModuleInstaller /** @var LocalModuleLoader */ private $localModuleLoader; + /** @var ArchivePuller */ + private $archivePuller; + + /** @var ArchiveHandler */ + private $archiveHandler; + + + + // new ArchiveHandler($this->localModuleLoader, App::getModulesRoot()); public static function create(int $mode): ModuleInstaller { $dependencyManager = DependencyManager::create($mode); $moduleFilter = ModuleFilter::create($mode); $localModuleLoader = LocalModuleLoader::create($mode); - $moduleInstaller = new ModuleInstaller($dependencyManager, $moduleFilter, $localModuleLoader); + $archivePuller = ArchivePuller::create(); + $archiveHandler = ArchiveHandler::create($mode); + $moduleInstaller = new ModuleInstaller( + $dependencyManager, + $moduleFilter, + $localModuleLoader, + $archivePuller, + $archiveHandler + ); return $moduleInstaller; } @@ -55,11 +76,15 @@ public static function createFromConfig(): ModuleInstaller public function __construct( DependencyManager $dependencyManager, ModuleFilter $moduleFilter, - LocalModuleLoader $localModuleLoader + LocalModuleLoader $localModuleLoader, + ArchivePuller $archivePuller, + ArchiveHandler $archiveHandler ) { $this->dependencyManager = $dependencyManager; $this->moduleFilter = $moduleFilter; $this->localModuleLoader = $localModuleLoader; + $this->archivePuller = $archivePuller; + $this->archiveHandler = $archiveHandler; } public function pull(Module $module): bool @@ -83,6 +108,12 @@ public function pull(Module $module): bool } try { + // New + $archive = $this->archivePuller->pull($module->getArchiveName(), $module->getVersion(), $archiveUrl); + $this->archiveHandler->extract($archive); + return true; + + // Old $archive = Archive::pullArchive($archiveUrl, $module->getArchiveName(), $module->getVersion()); $archive->untarArchive(); return true; From c8976f7d026ec275fc5c7f7adeef6c2585ffec0b Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 22 Jun 2023 22:22:59 +0200 Subject: [PATCH 04/21] docs: improve docblock comment --- src/Classes/App.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Classes/App.php b/src/Classes/App.php index 74db9f49..14117969 100644 --- a/src/Classes/App.php +++ b/src/Classes/App.php @@ -63,11 +63,17 @@ public static function getConfigRoot(): string return self::getRoot() . '/' . self::$configDir; } + /** + * /Archives/ + */ public static function getArchivesRoot(): string { return self::getRoot() . '/' . self::$archivesDir; } + /** + * /Modules/ + */ public static function getModulesRoot(): string { return self::getRoot() . '/' . self::getModulesDirName(); From 777794cfc6dd8923b985a40cec9267e88f2d0486 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 20:04:16 +0200 Subject: [PATCH 05/21] docs: improve docblock comments --- src/Classes/Archive/Archive.php | 12 ++++++++ src/Classes/Archive/ArchiveHandler.php | 42 ++++++++++++++++++++++++-- src/Classes/Archive/ArchiveName.php | 3 ++ src/Classes/Archive/ArchivePuller.php | 13 ++++++-- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/Classes/Archive/Archive.php b/src/Classes/Archive/Archive.php index f913581c..1ca60bb9 100644 --- a/src/Classes/Archive/Archive.php +++ b/src/Classes/Archive/Archive.php @@ -53,6 +53,10 @@ public function getVersion(): Version return $this->version; } + /** + * Liefert den Root Path zum Archive Ordner + * z. B. /.../ModifiedModuleLoaderClient/Archives/ + */ public function getArchivesRootPath(): string { return $this->archivesRootPath; @@ -63,6 +67,10 @@ public function getArchivesRootPath(): string // return $this->urlRootPath; // } + /** + * Liefert den Dateinamen der .tar Datei + * z. B. robinthehood_modified-std-module_0.1.0.tar + */ public function getFileName(): string { return @@ -72,6 +80,10 @@ public function getFileName(): string . '.tar'; } + /** + * Liefert den gesamten Path der .tar Datei. + * z. B. /.../ModifiedModuleLoaderClient/Archives/robinthehood_modified-std-module_0.1.0.tar + */ public function getFilePath(): string { return $this->getArchivesRootPath() . '/' . $this->getFileName(); diff --git a/src/Classes/Archive/ArchiveHandler.php b/src/Classes/Archive/ArchiveHandler.php index 5728539f..919d5828 100644 --- a/src/Classes/Archive/ArchiveHandler.php +++ b/src/Classes/Archive/ArchiveHandler.php @@ -9,6 +9,9 @@ use RobinTheHood\ModifiedModuleLoaderClient\Loader\LocalModuleLoader; use RobinTheHood\ModifiedModuleLoaderClient\Module; +/** + * Diese Klasse ist für das Packen und Entpacken von Archiven zuständig + */ class ArchiveHandler { private LocalModuleLoader $localModuleLoader; @@ -32,6 +35,13 @@ public function __construct(LocalModuleLoader $localModuleLoader, string $module $this->modulesRootPath = $modulesRootPath; } + /** + * Packt ein Archive zu einer .tar Datei. + * + * Dabei ist in der .tar Datei die Verzeichnis-Struktur // enthalten + * + * @throws \RuntimeException if an error occurs + */ public function pack(Archive $archive): void { $module = $this->localModuleLoader->loadByArchiveNameAndVersion( @@ -61,6 +71,20 @@ public function pack(Archive $archive): void } } + /** + * Entpack ein $archive. + * + * Dabei wird das Archive in Modules entpackt. Im .tar archive selbst liegen die Dateien in der Ordner-Strucktur + * /// vor. + * + * @param Archive $archive + * @param bool $external Wenn eine .tar Datei z. B. von Github kommt, ist die Ordner-Strucktur in der .tar Datei + * nicht kompatible. Im der .tar Datei fehlt das Verzeichnis ///. Es ist nur + * das Verzeichnis vorhandnen. In diesem Fall muss das Verzeichnis /// + * angelegt werden. + * + * @throws \RuntimeException if an error occurs + */ public function extract(Archive $archive, bool $external = false): void { $modulePath = $this->getModulePathFromArchive($archive); @@ -87,16 +111,27 @@ public function extract(Archive $archive, bool $external = false): void } } - private function getModulePathFromArchive(Archive $archiveNew): string + /** + * Liefert zu einem Archive den ModulePath + * z. B. /.../ModifiedModuleLoaderClient/Modules/composer/autoload/1.0.0/ + */ + private function getModulePathFromArchive(Archive $archive): string { - return $this->modulesRootPath . '/' . $archiveNew->getArchiveName() . '/' . $archiveNew->getVersion(); + return $this->modulesRootPath . '/' . $archive->getArchiveName() . '/' . $archive->getVersion(); } + /** + * Liefert den gesatem Modul Path zu einem Modul + * z. B. /.../ModifiedModuleLoaderClient/Modules/composer/autoload/ + */ private function getModulePathFromModule(Module $module): string { return $module->getLocalRootPath() . DIRECTORY_SEPARATOR . $module->getModulePath(); } + /** + * // TODO: Man könnte diese Methode in die Klasse FileHelper auslagern + */ private function createDirIfNotExists(string $path): void { if (!@mkdir($path) && !is_dir($path)) { @@ -104,6 +139,9 @@ private function createDirIfNotExists(string $path): void } } + /** + * // TODO: Man könnte diese Methode in die Klasse FileHelper auslagern + */ private function deleteFileIfExists(string $path): void { if (!@unlink($path) && file_exists($path)) { diff --git a/src/Classes/Archive/ArchiveName.php b/src/Classes/Archive/ArchiveName.php index 94838e50..72e9248d 100644 --- a/src/Classes/Archive/ArchiveName.php +++ b/src/Classes/Archive/ArchiveName.php @@ -13,6 +13,9 @@ namespace RobinTheHood\ModifiedModuleLoaderClient\Archive; +/** + * Eine VO (Value Object) Klasse + */ class ArchiveName { private string $value; diff --git a/src/Classes/Archive/ArchivePuller.php b/src/Classes/Archive/ArchivePuller.php index 92c42b15..86c75022 100644 --- a/src/Classes/Archive/ArchivePuller.php +++ b/src/Classes/Archive/ArchivePuller.php @@ -54,15 +54,24 @@ private function createArchive(string $archiveName, string $version): Archive return new Archive($archiveNameObj, $versionObj, $this->archivesRootPath); } - private function isTarArchive($content): bool + /** + * Überprüft, ob $content die Tarball-Signatur beinhaltet + * + * @param string $content .tar File as string + * + * // TODO: Man könnte diese Methode als eigenständige Klasse TarFileVerifier auslagern + */ + private function isTarArchive(string $content): bool { - // Überprüfen, ob $content die Tarball-Signatur beinhaltet $tarballSignature = "ustar"; $magic = substr($content, 257, 5); return ($magic === $tarballSignature); } + /** + * // TODO: Man könnte diese Methode in die Klasse FileHelper auslagern + */ private function createDirIfNotExists(string $path): void { if (!@mkdir($path) && !is_dir($path)) { From 87b89955134584c4368b834112cfae60442f518f Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 20:05:15 +0200 Subject: [PATCH 06/21] refactor: use . '/' . instead of DIRECTORY_SEPARATOR --- src/Classes/Archive/ArchiveHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Classes/Archive/ArchiveHandler.php b/src/Classes/Archive/ArchiveHandler.php index 919d5828..74152e67 100644 --- a/src/Classes/Archive/ArchiveHandler.php +++ b/src/Classes/Archive/ArchiveHandler.php @@ -126,7 +126,7 @@ private function getModulePathFromArchive(Archive $archive): string */ private function getModulePathFromModule(Module $module): string { - return $module->getLocalRootPath() . DIRECTORY_SEPARATOR . $module->getModulePath(); + return $module->getLocalRootPath() . '/' . $module->getModulePath(); } /** From 7902dee5147be0bfee6669e257f3305cf8fbda4b Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:23:50 +0200 Subject: [PATCH 07/21] docs: improve docblock comments --- src/Classes/Archive/ArchiveHandler.php | 2 +- src/Classes/Module.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Classes/Archive/ArchiveHandler.php b/src/Classes/Archive/ArchiveHandler.php index 74152e67..f2822dca 100644 --- a/src/Classes/Archive/ArchiveHandler.php +++ b/src/Classes/Archive/ArchiveHandler.php @@ -16,9 +16,9 @@ class ArchiveHandler { private LocalModuleLoader $localModuleLoader; + /** @var string /.../ModifiedModuleLoaderClient/Modules */ private string $modulesRootPath; - public static function create(int $mode): ArchiveHandler { $localModuleLoader = LocalModuleLoader::create($mode); diff --git a/src/Classes/Module.php b/src/Classes/Module.php index 90793205..c7c940a7 100644 --- a/src/Classes/Module.php +++ b/src/Classes/Module.php @@ -89,8 +89,7 @@ class Module extends ModuleInfo private $isLoadable; /** - * Liefert den absoluten Pfad zum MMLC - * MMLC-Root Verzeichnis. + * Liefert den absoluten Pfad zum MMLC-Root Verzeichnis. * * Beispiel: * /root/dir1/dir2/.../ModifiedModuleLoaderClient From e546153a826d1b83c688d39c48fbc8ac1727c29b Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:25:51 +0200 Subject: [PATCH 08/21] fix: do not allow dot in archive and module name --- src/Classes/Archive/ArchiveName.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Classes/Archive/ArchiveName.php b/src/Classes/Archive/ArchiveName.php index 72e9248d..2af41c95 100644 --- a/src/Classes/Archive/ArchiveName.php +++ b/src/Classes/Archive/ArchiveName.php @@ -54,7 +54,7 @@ public function __toString(): string private function isValidArchiveName(string $archiveName): bool { - $pattern = '/^([A-Za-z0-9_.-]+)\/([A-Za-z0-9_.-]+)$/'; + $pattern = '/^([A-Za-z0-9_-]+)\/([A-Za-z0-9_-]+)$/'; return preg_match($pattern, $archiveName) === 1; } From aa554d2e139a6a50d6f0faf2be48d072c348a34f Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:26:23 +0200 Subject: [PATCH 09/21] test: add ArchiveNameTest --- tests/unit/ArchiveTests/ArchiveNameTest.php | 51 +++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 tests/unit/ArchiveTests/ArchiveNameTest.php diff --git a/tests/unit/ArchiveTests/ArchiveNameTest.php b/tests/unit/ArchiveTests/ArchiveNameTest.php new file mode 100644 index 00000000..c07546f1 --- /dev/null +++ b/tests/unit/ArchiveTests/ArchiveNameTest.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\ArchiveTests; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchiveName; + +class ArchiveNameTest extends TestCase +{ + public function testConstructorValidArchiveName(): void + { + $archiveName = new ArchiveName('vendor/module'); + + $this->assertInstanceOf(ArchiveName::class, $archiveName); + $this->assertSame('module', $archiveName->getModuleName()); + $this->assertSame('vendor', $archiveName->getVendorName()); + $this->assertSame('vendor/module', $archiveName->__toString()); + } + + public function testConstructorInvalidArchiveName1(): void + { + $this->expectException(\InvalidArgumentException::class); + + new ArchiveName('invalid'); + } + + public function testConstructorInvalidArchiveName2(): void + { + $this->expectException(\InvalidArgumentException::class); + + new ArchiveName('invalid/invalid/'); + } + + public function testConstructorInvalidArchiveName3(): void + { + $this->expectException(\InvalidArgumentException::class); + + new ArchiveName('in.valid/invalid'); + } +} From 7fcce030ae4575c039d4ae0826cfb57a2e756763 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:26:57 +0200 Subject: [PATCH 10/21] feat: add setModulesRootPath() to LocalModuleLoader --- src/Classes/Loader/LocalModuleLoader.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Classes/Loader/LocalModuleLoader.php b/src/Classes/Loader/LocalModuleLoader.php index 724cbf11..10e5db05 100644 --- a/src/Classes/Loader/LocalModuleLoader.php +++ b/src/Classes/Loader/LocalModuleLoader.php @@ -27,6 +27,9 @@ class LocalModuleLoader /** @var ModuleFilter */ private $moduleFilter; + /** @var string */ + private $modulesRootPath; + public static function create(int $mode): LocalModuleLoader { $moduleFilter = ModuleFilter::create($mode); @@ -37,6 +40,12 @@ public static function create(int $mode): LocalModuleLoader public function __construct(ModuleFilter $moduleFilter) { $this->moduleFilter = $moduleFilter; + $this->modulesRootPath = App::getModulesRoot(); + } + + public function setModulesRootPath(string $path): void + { + $this->modulesRootPath = $path; } /** @@ -127,11 +136,11 @@ public function loadAllInstalledVersions(): array public function getVendorDirs() { - return FileHelper::scanDir(App::getModulesRoot(), FileHelper::DIRS_ONLY); + return FileHelper::scanDir($this->modulesRootPath, FileHelper::DIRS_ONLY); } public function getModuleDirs() { - return FileHelper::scanDirRecursive(App::getModulesRoot(), FileHelper::DIRS_ONLY, false, 3); + return FileHelper::scanDirRecursive($this->modulesRootPath, FileHelper::DIRS_ONLY, false, 3); } } From 4c5ef07b6388a346fcae7b4cfaf18bb0c340c0c3 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:27:27 +0200 Subject: [PATCH 11/21] fix: path building error --- src/Classes/Archive/ArchiveHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Classes/Archive/ArchiveHandler.php b/src/Classes/Archive/ArchiveHandler.php index f2822dca..81f8721a 100644 --- a/src/Classes/Archive/ArchiveHandler.php +++ b/src/Classes/Archive/ArchiveHandler.php @@ -126,7 +126,7 @@ private function getModulePathFromArchive(Archive $archive): string */ private function getModulePathFromModule(Module $module): string { - return $module->getLocalRootPath() . '/' . $module->getModulePath(); + return $module->getLocalRootPath() . $module->getModulePath(); } /** From c0329303c1ad04791fd20634b30c9d38531ce689 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:27:48 +0200 Subject: [PATCH 12/21] fix: use of unset $_SERVER vars --- src/Classes/Helpers/ServerHelper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Classes/Helpers/ServerHelper.php b/src/Classes/Helpers/ServerHelper.php index 5b726c4b..3506ff35 100644 --- a/src/Classes/Helpers/ServerHelper.php +++ b/src/Classes/Helpers/ServerHelper.php @@ -18,7 +18,9 @@ class ServerHelper public static function getUri(): string { $http = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; - $url = $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; + $serverName = $_SERVER['SERVER_NAME'] ?? 'unknown-server-name.de'; + $scriptName = $_SERVER['SCRIPT_NAME'] ?? '/unknown-scriptname.php'; + $url = $serverName . $scriptName; $parts = pathinfo($url); return $http . $parts['dirname']; } From c554d2df04cbe6c34b32bfbf1b922899d9dfd9de Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 22:28:17 +0200 Subject: [PATCH 13/21] test: add ArchiveHandlerTest and ArchiveTest --- .../unit/ArchiveTests/ArchiveHandlerTest.php | 104 ++++++++++++++++++ tests/unit/ArchiveTests/ArchiveTest.php | 62 +++++++++++ 2 files changed, 166 insertions(+) create mode 100644 tests/unit/ArchiveTests/ArchiveHandlerTest.php create mode 100644 tests/unit/ArchiveTests/ArchiveTest.php diff --git a/tests/unit/ArchiveTests/ArchiveHandlerTest.php b/tests/unit/ArchiveTests/ArchiveHandlerTest.php new file mode 100644 index 00000000..b2386eea --- /dev/null +++ b/tests/unit/ArchiveTests/ArchiveHandlerTest.php @@ -0,0 +1,104 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\ArchiveTests; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\Archive; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchiveHandler; +use RobinTheHood\ModifiedModuleLoaderClient\Loader\LocalModuleLoader; + +class ArchiveHandlerTest extends TestCase +{ + /** @var string */ + protected $testDir; + + protected function setUp(): void + { + parent::setUp(); + + // Testordner erstellen + $this->testDir = __DIR__ . '/test_files/'; + @mkdir($this->testDir, 0777, true); + + // Testorder und Dateo im Ordner erstellen + @mkdir($this->testDir . 'ModulesExtracted', 0777, true); + @mkdir($this->testDir . 'Modules/robinthehood/modified-std-module/1.2.3', 0777, true); + file_put_contents( + $this->testDir . 'Modules/robinthehood/modified-std-module/1.2.3/moduleinfo.json', + '{ + "archiveName": "robinthehood/modified-std-module", + "version": "1.2.3" + }' + ); + + @mkdir($this->testDir . 'Archives', 0777, true); + } + + protected function tearDown(): void + { + parent::tearDown(); + + // Testdateien löschen + unlink($this->testDir . 'Modules/robinthehood/modified-std-module/1.2.3/moduleinfo.json'); + rmdir($this->testDir . 'Modules/robinthehood/modified-std-module/1.2.3'); + rmdir($this->testDir . 'Modules/robinthehood/modified-std-module'); + rmdir($this->testDir . 'Modules/robinthehood'); + rmdir($this->testDir . 'Modules'); + + unlink($this->testDir . 'ModulesExtracted/robinthehood/modified-std-module/1.2.3/moduleinfo.json'); + rmdir($this->testDir . 'ModulesExtracted/robinthehood/modified-std-module/1.2.3'); + rmdir($this->testDir . 'ModulesExtracted/robinthehood/modified-std-module'); + rmdir($this->testDir . 'ModulesExtracted/robinthehood'); + rmdir($this->testDir . 'ModulesExtracted'); + + unlink($this->testDir . 'Archives/robinthehood_modified-std-module_1.2.3.tar'); + rmdir($this->testDir . 'Archives'); + + rmdir($this->testDir); + } + + public function testPackAndExtract(): void + { + $mode = 0; // Set the appropriate mode + $archiveName = 'robinthehood/modified-std-module'; + $version = '1.2.3'; + $archivesRootPath = $this->testDir . 'Archives'; + $modulesRootPath = $this->testDir . 'Modules'; + $modulesExtractedRootPath = $this->testDir . 'ModulesExtracted'; + + // Create archive + $archive = Archive::create($archiveName, $version, $archivesRootPath); + + // Create ArchiveHandler + $localModuleLoader = LocalModuleLoader::create(0); + $localModuleLoader->setModulesRootPath($modulesRootPath); + $archiveHandler = new ArchiveHandler($localModuleLoader, $modulesRootPath); + + // Pack archive + $archiveHandler->pack($archive); + + // Check if archive file exists + $this->assertFileExists($archive->getFilePath()); + + // Create archive + $archiveHandlerExtract = new ArchiveHandler($localModuleLoader, $modulesExtractedRootPath); + + // Extract archive + $archiveHandlerExtract->extract($archive); + + // Check if module directory exists + $modulePath = $modulesExtractedRootPath . '/' . $archiveName . '/' . $version; + $this->assertDirectoryExists($modulePath); + } +} diff --git a/tests/unit/ArchiveTests/ArchiveTest.php b/tests/unit/ArchiveTests/ArchiveTest.php new file mode 100644 index 00000000..e0fc53ea --- /dev/null +++ b/tests/unit/ArchiveTests/ArchiveTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\ArchiveTests; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\Archive; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchiveName; +use RobinTheHood\ModifiedModuleLoaderClient\Semver\Version; + +class ArchiveTest extends TestCase +{ + public function testCreate(): void + { + $archiveName = 'robinthehood/modified-std-module'; + $version = '1.0.0'; + $archivesRootPath = '/path/to/archives'; + + $archive = Archive::create($archiveName, $version, $archivesRootPath); + + $this->assertInstanceOf(Archive::class, $archive); + $this->assertInstanceOf(ArchiveName::class, $archive->getArchiveName()); + $this->assertInstanceOf(Version::class, $archive->getVersion()); + $this->assertSame($archiveName, $archive->getArchiveName()->__toString()); + $this->assertSame($version, $archive->getVersion()->__toString()); + $this->assertSame($archivesRootPath, $archive->getArchivesRootPath()); + } + + public function testGetFileName(): void + { + $archiveName = new ArchiveName('robinthehood/modified-std-module'); + $version = new Version(1, 2, 3); + $archivesRootPath = '/path/to/archives'; + + $archive = new Archive($archiveName, $version, $archivesRootPath); + + $expectedFileName = 'robinthehood_modified-std-module_1.2.3.tar'; + $this->assertSame($expectedFileName, $archive->getFileName()); + } + + public function testGetFilePath(): void + { + $archiveName = new ArchiveName('robinthehood/modified-std-module'); + $version = new Version(1, 2, 3); + $archivesRootPath = '/path/to/archives'; + + $archive = new Archive($archiveName, $version, $archivesRootPath); + + $expectedFilePath = '/path/to/archives/robinthehood_modified-std-module_1.2.3.tar'; + $this->assertSame($expectedFilePath, $archive->getFilePath()); + } +} From 25330c793f8272e87016c0f5af4620bc4b9e213e Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 27 Jun 2023 23:44:42 +0200 Subject: [PATCH 14/21] test: add ArchivePullerTest --- tests/unit/ArchiveTests/ArchivePullerTest.php | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 tests/unit/ArchiveTests/ArchivePullerTest.php diff --git a/tests/unit/ArchiveTests/ArchivePullerTest.php b/tests/unit/ArchiveTests/ArchivePullerTest.php new file mode 100644 index 00000000..33413a97 --- /dev/null +++ b/tests/unit/ArchiveTests/ArchivePullerTest.php @@ -0,0 +1,90 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace RobinTheHood\ModifiedModuleLoaderClient\Tests\Unit\ArchiveTests; + +use PHPUnit\Framework\TestCase; +use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\ApiRequest; +use RobinTheHood\ModifiedModuleLoaderClient\Api\V1\HttpRequest; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\Archive; +use RobinTheHood\ModifiedModuleLoaderClient\Archive\ArchivePuller; +use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser; +use RuntimeException; + +class ArchivePullerTest extends TestCase +{ + /** @var string */ + protected $testDir; + + protected function setUp(): void + { + parent::setUp(); + + // Testordner erstellen + $this->testDir = __DIR__ . '/test_files/'; + @mkdir($this->testDir, 0777, true); + + // Testorder und Dateo im Ordner erstellen + + @mkdir($this->testDir . 'Archives', 0777, true); + } + + protected function tearDown(): void + { + parent::tearDown(); + + // Testdateien löschen + @unlink($this->testDir . 'Archives/robinthehood_modified-std-module_0.9.0.tar'); + @rmdir($this->testDir . 'Archives'); + + @rmdir($this->testDir); + } + + public function testPullValidArchive(): void + { + $archivesRootPath = $this->testDir . 'Archives'; + + $archivePuller = new ArchivePuller( + new HttpRequest(), + Parser::create(), + $archivesRootPath + ); + + $apiRequest = new ApiRequest(); + $result = $apiRequest->getArchive('robinthehood/modified-std-module', '0.9.0'); + $content = $result['content'] ?? []; + $archiveUrl = $content['archiveUrl'] ?? ''; + $archive = $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', $archiveUrl); + + $this->assertInstanceOf(Archive::class, $archive); + $this->assertSame('robinthehood/modified-std-module', $archive->getArchiveName()->__toString()); + $this->assertSame('0.9.0', $archive->getVersion()->__toString()); + $this->assertSame($archivesRootPath . '/robinthehood_modified-std-module_0.9.0.tar', $archive->getFilePath()); + } + + public function testPullInvalidArchive(): void + { + $archivesRootPath = $this->testDir . 'Archives'; + + $archivePuller = new ArchivePuller( + new HttpRequest(), + Parser::create(), + $archivesRootPath + ); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Failed to pull Archive: robinthehood/modified-std-module:0.9.0'); + + $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', 'this-is-a-not-working-url.local'); + } +} From dcff4be54972dbb8a39dbbe6856499e60ee1b351 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 29 Jun 2023 22:39:14 +0200 Subject: [PATCH 15/21] test: improve test for new Curl HttpRequest --- tests/unit/ArchiveTests/ArchivePullerTest.php | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tests/unit/ArchiveTests/ArchivePullerTest.php b/tests/unit/ArchiveTests/ArchivePullerTest.php index 33413a97..5672565d 100644 --- a/tests/unit/ArchiveTests/ArchivePullerTest.php +++ b/tests/unit/ArchiveTests/ArchivePullerTest.php @@ -72,6 +72,24 @@ public function testPullValidArchive(): void $this->assertSame($archivesRootPath . '/robinthehood_modified-std-module_0.9.0.tar', $archive->getFilePath()); } + public function testPullArchiveFromInvalidUrl(): void + { + $archivesRootPath = $this->testDir . 'Archives'; + + $archivePuller = new ArchivePuller( + new HttpRequest(), + Parser::create(), + $archivesRootPath + ); + + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage( + 'Fehler beim Senden des GET-Requests: Could not resolve host: this-is-a-not-working-url.local' + ); + + $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', 'this-is-a-not-working-url.local'); + } + public function testPullInvalidArchive(): void { $archivesRootPath = $this->testDir . 'Archives'; @@ -85,6 +103,6 @@ public function testPullInvalidArchive(): void $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Failed to pull Archive: robinthehood/modified-std-module:0.9.0'); - $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', 'this-is-a-not-working-url.local'); + $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', 'https://postman-echo.com/get'); } } From 7282058b61425aa64ebbf94846f47be7aacccc08 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Thu, 29 Jun 2023 22:57:07 +0200 Subject: [PATCH 16/21] test: some debug messages --- tests/unit/ArchiveTests/ArchivePullerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/ArchiveTests/ArchivePullerTest.php b/tests/unit/ArchiveTests/ArchivePullerTest.php index 5672565d..1416967f 100644 --- a/tests/unit/ArchiveTests/ArchivePullerTest.php +++ b/tests/unit/ArchiveTests/ArchivePullerTest.php @@ -62,6 +62,7 @@ public function testPullValidArchive(): void $apiRequest = new ApiRequest(); $result = $apiRequest->getArchive('robinthehood/modified-std-module', '0.9.0'); + var_dump($result); $content = $result['content'] ?? []; $archiveUrl = $content['archiveUrl'] ?? ''; $archive = $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', $archiveUrl); From 7d10a0e847e276bbda8da251f6ce92f7de9b3185 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Fri, 30 Jun 2023 00:09:54 +0200 Subject: [PATCH 17/21] test: remove debug message --- tests/unit/ArchiveTests/ArchivePullerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/ArchiveTests/ArchivePullerTest.php b/tests/unit/ArchiveTests/ArchivePullerTest.php index 1416967f..5672565d 100644 --- a/tests/unit/ArchiveTests/ArchivePullerTest.php +++ b/tests/unit/ArchiveTests/ArchivePullerTest.php @@ -62,7 +62,6 @@ public function testPullValidArchive(): void $apiRequest = new ApiRequest(); $result = $apiRequest->getArchive('robinthehood/modified-std-module', '0.9.0'); - var_dump($result); $content = $result['content'] ?? []; $archiveUrl = $content['archiveUrl'] ?? ''; $archive = $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', $archiveUrl); From f7325d7d95a95e1bb3c586da32abd90cc5bb88aa Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Tue, 4 Jul 2023 20:10:25 +0200 Subject: [PATCH 18/21] fix: test of ArchiveHandlerTest --- tests/unit/ArchiveTests/ArchivePullerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/ArchiveTests/ArchivePullerTest.php b/tests/unit/ArchiveTests/ArchivePullerTest.php index 5672565d..bf17d6ee 100644 --- a/tests/unit/ArchiveTests/ArchivePullerTest.php +++ b/tests/unit/ArchiveTests/ArchivePullerTest.php @@ -83,8 +83,8 @@ public function testPullArchiveFromInvalidUrl(): void ); $this->expectException(RuntimeException::class); - $this->expectExceptionMessage( - 'Fehler beim Senden des GET-Requests: Could not resolve host: this-is-a-not-working-url.local' + $this->expectExceptionMessageMatches( + '/Error sending the GET request:/' ); $archivePuller->pull('robinthehood/modified-std-module', '0.9.0', 'this-is-a-not-working-url.local'); From e9a5cbb0d7a18a3da56b0dbc6477852844f415a3 Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Wed, 5 Jul 2023 09:26:38 +0200 Subject: [PATCH 19/21] refactor: remove unused code --- src/Classes/Archive/Archive.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Classes/Archive/Archive.php b/src/Classes/Archive/Archive.php index 1ca60bb9..7d945cd3 100644 --- a/src/Classes/Archive/Archive.php +++ b/src/Classes/Archive/Archive.php @@ -12,7 +12,6 @@ class Archive private ArchiveName $archiveName; private Version $version; private string $archivesRootPath; - // private string $urlRootPath; public static function create(string $archiveName, string $version, string $archivesRootPath): Archive { @@ -27,19 +26,13 @@ public function __construct( ArchiveName $archiveName, Version $version, string $archivesRootPath - // string $urlRootPath, ) { if (empty($archivesRootPath)) { throw new \InvalidArgumentException('archivesRootPath cannot be empty.'); } - // if (empty($urlRootPath)) { - // throw new \InvalidArgumentException('urlRootPath cannot be empty.'); - // } - $this->archiveName = $archiveName; $this->archivesRootPath = $archivesRootPath; - // $this->urlRootPath = $urlRootPath; $this->version = $version; } @@ -62,11 +55,6 @@ public function getArchivesRootPath(): string return $this->archivesRootPath; } - // public function getUrlRootPath(): string - // { - // return $this->urlRootPath; - // } - /** * Liefert den Dateinamen der .tar Datei * z. B. robinthehood_modified-std-module_0.1.0.tar From 6cb801ff4c28d47547cc4ddde9e0c973f409abeb Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Wed, 5 Jul 2023 09:46:46 +0200 Subject: [PATCH 20/21] docs: improve code documentation --- src/Classes/Archive/Archive.php | 4 ++++ src/Classes/Archive/ArchiveName.php | 4 ++++ src/Classes/Archive/ArchivePuller.php | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/src/Classes/Archive/Archive.php b/src/Classes/Archive/Archive.php index 7d945cd3..0a034bf1 100644 --- a/src/Classes/Archive/Archive.php +++ b/src/Classes/Archive/Archive.php @@ -7,6 +7,10 @@ use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser; use RobinTheHood\ModifiedModuleLoaderClient\Semver\Version; +/** + * Diese Klasse repräsentiert ein Modul gepackt als .tar Datei. Die .tar Datei befindet sich im Datei System und nicht + * als Data-String einem Objekt dieser Klasse. Die Methode getFilePath() liefert die Pfad zu .tar Datei. + */ class Archive { private ArchiveName $archiveName; diff --git a/src/Classes/Archive/ArchiveName.php b/src/Classes/Archive/ArchiveName.php index 2af41c95..9098ec2e 100644 --- a/src/Classes/Archive/ArchiveName.php +++ b/src/Classes/Archive/ArchiveName.php @@ -15,6 +15,10 @@ /** * Eine VO (Value Object) Klasse + * + * Da der Name eines Archives nicht willkührlich gewählt sein darf, wir der Name eines Archives durch diese Klasse + * repräsentiert. Der Konstruktor kann beim Erstellen eines Objekts zudem überprüfen, ob es sich um einen validen + * ArchiveNamen-String handelt. */ class ArchiveName { diff --git a/src/Classes/Archive/ArchivePuller.php b/src/Classes/Archive/ArchivePuller.php index 86c75022..80c06d09 100644 --- a/src/Classes/Archive/ArchivePuller.php +++ b/src/Classes/Archive/ArchivePuller.php @@ -9,6 +9,10 @@ use RobinTheHood\ModifiedModuleLoaderClient\Semver\Parser; use RuntimeException; +/** + * Die Klasse ist für das Herunterladen von Archiven vom Server zuständig. Ein Archive repräsentiert ein Modul + * gepackt als .tar Datei. + */ class ArchivePuller { private HttpRequest $httpRequest; @@ -30,6 +34,9 @@ public function __construct(HttpRequest $httpRequest, Parser $parser, string $ar $this->archivesRootPath = $archivesRootPath; } + /** + * @throws RuntimeException wenn das Archive nicht vom Server geladen werden konnte. + */ public function pull(string $archiveName, string $version, string $url): Archive { $archive = $this->createArchive($archiveName, $version); From e2fcd0d44b27853a04f02fad156b75db156ac46a Mon Sep 17 00:00:00 2001 From: Robin Wieschendorf Date: Wed, 5 Jul 2023 09:47:17 +0200 Subject: [PATCH 21/21] refactor: rename method createArchive() --- src/Classes/Archive/ArchivePuller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Classes/Archive/ArchivePuller.php b/src/Classes/Archive/ArchivePuller.php index 80c06d09..dcc29808 100644 --- a/src/Classes/Archive/ArchivePuller.php +++ b/src/Classes/Archive/ArchivePuller.php @@ -39,7 +39,7 @@ public function __construct(HttpRequest $httpRequest, Parser $parser, string $ar */ public function pull(string $archiveName, string $version, string $url): Archive { - $archive = $this->createArchive($archiveName, $version); + $archive = $this->createArchiveObjFromStrings($archiveName, $version); $tarArchiveContent = $this->httpRequest->sendGetRequest($url); @@ -53,7 +53,7 @@ public function pull(string $archiveName, string $version, string $url): Archive return $archive; } - private function createArchive(string $archiveName, string $version): Archive + private function createArchiveObjFromStrings(string $archiveName, string $version): Archive { $archiveNameObj = new ArchiveName($archiveName); $versionObj = $this->parser->parse($version);