diff --git a/Module/PackageManager.php b/Module/PackageManager.php index 12decb867..f97f13606 100644 --- a/Module/PackageManager.php +++ b/Module/PackageManager.php @@ -18,6 +18,8 @@ use phpOMS\System\File\Local\Directory; use phpOMS\System\File\Local\File; use phpOMS\System\File\Local\LocalStorage; use phpOMS\System\File\PathException; +use phpOMS\System\OperatingSystem; +use phpOMS\System\SystemType; use phpOMS\Utils\IO\Zip\Zip; use phpOMS\Utils\StringUtils; @@ -85,7 +87,7 @@ final class PackageManager public function __construct(string $path, string $basePath, string $publicKey) { $this->path = $path; - $this->basePath = $basePath; // todo: maybe remove from here because stupid + $this->basePath = \rtrim($basePath, '\\/'); $this->publicKey = $publicKey; } @@ -180,45 +182,78 @@ final class PackageManager throw new \Exception(); } - foreach ($this->info as $key => $components) { - if (\function_exists($this->{$key})) { - $this->{$key}($components); + foreach ($this->info['update'] as $steps) { + foreach ($steps as $key => $components) { + if (\function_exists($this->{$key})) { + $this->{$key}($components); + } } } } + /** + * Download files + * + * @param array $components Component data + * + * @return void + * + * @since 1.0.0 + */ + private function download(array $components) : void + { + foreach ($components as $from => $to) { + $fp = \fopen($this->basePath . '/' . $to, 'w+'); + $ch = \curl_init(\str_replace(' ','%20', $from)); + + \curl_setopt($ch, \CURLOPT_TIMEOUT, 50); + \curl_setopt($ch, \CURLOPT_FILE, $fp); + \curl_setopt($ch, \CURLOPT_FOLLOWLOCATION, true); + + \curl_exec($ch); + + \curl_close($ch); + \fclose($fp); + } + } + /** * Move files * - * @param mixed $components Component data + * @param array $components Component data * * @return void * * @since 1.0.0 */ - private function move($components) : void + private function move(array $components) : void { - foreach ($components as $component) { - LocalStorage::move($this->basePath . '/' . $component['from'], $this->basePath . '/' . $component['to'], true); + foreach ($components as $from => $to) { + $fromPath = StringUtils::startsWith($from, '/Package/') ? $this->extractPath : $this->basePath; + $toPath = StringUtils::startsWith($to, '/Package/') ? $this->extractPath : $this->basePath; + + LocalStorage::move($fromPath . '/' . $from, $toPath . '/' . $to, true); } } /** * Copy files * - * @param mixed $components Component data + * @param array $components Component data * * @return void * * @since 1.0.0 */ - private function copy($components) : void + private function copy(array $components) : void { - foreach ($components as $component) { - if (StringUtils::startsWith($component['from'], 'Package/')) { - LocalStorage::copy($this->path . '/' . $component['from'], $this->basePath . '/' . $component['to'], true); - } else { - LocalStorage::copy($this->basePath . '/' . $component['from'], $this->basePath . '/' . $component['to'], true); + foreach ($components as $from => $tos) { + $fromPath = StringUtils::startsWith($from, '/Package/') ? $this->extractPath : $this->basePath; + + foreach ($tos as $to) { + $toPath = StringUtils::startsWith($to, '/Package/') ? $this->extractPath : $this->basePath; + + LocalStorage::copy($fromPath . '/' . $from, $toPath . '/' . $to, true); } } } @@ -226,32 +261,46 @@ final class PackageManager /** * Delete files * - * @param mixed $components Component data + * @param array $components Component data * * @return void * * @since 1.0.0 */ - private function delete($components) : void + private function delete(array $components) : void { foreach ($components as $component) { - LocalStorage::delete($this->basePath . '/' . $component); + $path = StringUtils::startsWith($component, '/Package/') ? $this->extractPath : $this->basePath; + LocalStorage::delete($path . '/' . $component); } } /** * Execute commands * - * @param mixed $components Component data + * @param array $components Component data * * @return void * * @since 1.0.0 */ - private function execute($components) : void + private function cmd(array $components) : void { foreach ($components as $component) { - include $this->basePath . '/' . $component; + $pipes = []; + $cmd = ''; + $path = StringUtils::startsWith($component, '/Package/') ? $this->extractPath : $this->basePath; + if (StringUtils::endsWith($component, '.php')) { + $cmd = 'php ' . $path . '/' . $component; + } elseif (StringUtils::endsWith($component, '.sh') && OperatingSystem::getSystem() === SystemType::LINUX) { + $cmd = '.' . $path . '/' . $component; + } elseif (StringUtils::endsWith($component, '.batch') && OperatingSystem::getSystem() === SystemType::WIN) { + $cmd = '.' . $path . '/' . $component; + } + + if ($cmd !== '') { + \proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes, __DIR__); + } } } diff --git a/tests/Module/PackageManagerTest.php b/tests/Module/PackageManagerTest.php index 8b27faa8b..cc085a7f1 100644 --- a/tests/Module/PackageManagerTest.php +++ b/tests/Module/PackageManagerTest.php @@ -83,7 +83,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase { $package = new PackageManager( __DIR__ . '/testPackage.zip', - '/invalid', + __DIR__ . 'dummyModule/', \file_get_contents(__DIR__ . '/public.key') ); @@ -92,6 +92,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase self::assertTrue($package->isValid()); $package->load(); + $package->install(); } public function testNotExtractedLoad() : void diff --git a/tests/Module/dummyModule/Remove/me.md b/tests/Module/dummyModule/Remove/me.md new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/dummyModule/Replace.md b/tests/Module/dummyModule/Replace.md new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/dummyModule/toCopy/a.md b/tests/Module/dummyModule/toCopy/a.md new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/dummyModule/toCopy/sub/b.txt b/tests/Module/dummyModule/toCopy/sub/b.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/dummyModule/toMove/a.md b/tests/Module/dummyModule/toMove/a.md new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/dummyModule/toMove/sub/b.txt b/tests/Module/dummyModule/toMove/sub/b.txt new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/testPackage/info.json b/tests/Module/testPackage/info.json index 2c209f641..be72f6d10 100644 --- a/tests/Module/testPackage/info.json +++ b/tests/Module/testPackage/info.json @@ -2,24 +2,27 @@ "name": "", "type": "Modules", "version": "1.0.1", - "update": { - "add": { - "https://....": "..." - }, - "replace": { - "https://....": "" - }, - "move": { - "https://....": "" - }, - "remove": [ - "...." - ], - "run": [ - "...." - ], - "cmd": [ - "...." - ] - } + "update": [ + { + "download": { + "https://raw.githubusercontent.com/Orange-Management/Orange-Management/develop/README.md": "README.md" + }, + "move": { + "toMove": "moveHere" + }, + "copy": { + "toCopy": [ + "copyHere" + ] + }, + "delete": [ + "Remove" + ], + "cmd": [ + "/Package/run.php", + "/Package/run.sh", + "/Package/run.batch" + ] + } + ] } \ No newline at end of file diff --git a/tests/Module/testPackage/testSubPackage/run.batch b/tests/Module/testPackage/testSubPackage/run.batch new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/testPackage/testSubPackage/run.php b/tests/Module/testPackage/testSubPackage/run.php new file mode 100644 index 000000000..e69de29bb diff --git a/tests/Module/testPackage/testSubPackage/run.sh b/tests/Module/testPackage/testSubPackage/run.sh new file mode 100644 index 000000000..e69de29bb