prepare package manager functionality

This commit is contained in:
Dennis Eichhorn 2019-11-09 10:47:36 +01:00
parent 8a2437051b
commit f890c78e83
12 changed files with 95 additions and 42 deletions

View File

@ -18,6 +18,8 @@ use phpOMS\System\File\Local\Directory;
use phpOMS\System\File\Local\File; use phpOMS\System\File\Local\File;
use phpOMS\System\File\Local\LocalStorage; use phpOMS\System\File\Local\LocalStorage;
use phpOMS\System\File\PathException; use phpOMS\System\File\PathException;
use phpOMS\System\OperatingSystem;
use phpOMS\System\SystemType;
use phpOMS\Utils\IO\Zip\Zip; use phpOMS\Utils\IO\Zip\Zip;
use phpOMS\Utils\StringUtils; use phpOMS\Utils\StringUtils;
@ -85,7 +87,7 @@ final class PackageManager
public function __construct(string $path, string $basePath, string $publicKey) public function __construct(string $path, string $basePath, string $publicKey)
{ {
$this->path = $path; $this->path = $path;
$this->basePath = $basePath; // todo: maybe remove from here because stupid $this->basePath = \rtrim($basePath, '\\/');
$this->publicKey = $publicKey; $this->publicKey = $publicKey;
} }
@ -180,45 +182,78 @@ final class PackageManager
throw new \Exception(); throw new \Exception();
} }
foreach ($this->info as $key => $components) { foreach ($this->info['update'] as $steps) {
if (\function_exists($this->{$key})) { foreach ($steps as $key => $components) {
$this->{$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 * Move files
* *
* @param mixed $components Component data * @param array $components Component data
* *
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private function move($components) : void private function move(array $components) : void
{ {
foreach ($components as $component) { foreach ($components as $from => $to) {
LocalStorage::move($this->basePath . '/' . $component['from'], $this->basePath . '/' . $component['to'], true); $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 * Copy files
* *
* @param mixed $components Component data * @param array $components Component data
* *
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private function copy($components) : void private function copy(array $components) : void
{ {
foreach ($components as $component) { foreach ($components as $from => $tos) {
if (StringUtils::startsWith($component['from'], 'Package/')) { $fromPath = StringUtils::startsWith($from, '/Package/') ? $this->extractPath : $this->basePath;
LocalStorage::copy($this->path . '/' . $component['from'], $this->basePath . '/' . $component['to'], true);
} else { foreach ($tos as $to) {
LocalStorage::copy($this->basePath . '/' . $component['from'], $this->basePath . '/' . $component['to'], true); $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 * Delete files
* *
* @param mixed $components Component data * @param array $components Component data
* *
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private function delete($components) : void private function delete(array $components) : void
{ {
foreach ($components as $component) { foreach ($components as $component) {
LocalStorage::delete($this->basePath . '/' . $component); $path = StringUtils::startsWith($component, '/Package/') ? $this->extractPath : $this->basePath;
LocalStorage::delete($path . '/' . $component);
} }
} }
/** /**
* Execute commands * Execute commands
* *
* @param mixed $components Component data * @param array $components Component data
* *
* @return void * @return void
* *
* @since 1.0.0 * @since 1.0.0
*/ */
private function execute($components) : void private function cmd(array $components) : void
{ {
foreach ($components as $component) { 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__);
}
} }
} }

View File

@ -83,7 +83,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase
{ {
$package = new PackageManager( $package = new PackageManager(
__DIR__ . '/testPackage.zip', __DIR__ . '/testPackage.zip',
'/invalid', __DIR__ . 'dummyModule/',
\file_get_contents(__DIR__ . '/public.key') \file_get_contents(__DIR__ . '/public.key')
); );
@ -92,6 +92,7 @@ class PackageManagerTest extends \PHPUnit\Framework\TestCase
self::assertTrue($package->isValid()); self::assertTrue($package->isValid());
$package->load(); $package->load();
$package->install();
} }
public function testNotExtractedLoad() : void public function testNotExtractedLoad() : void

View File

View File

View File

View File

View File

@ -2,24 +2,27 @@
"name": "", "name": "",
"type": "Modules", "type": "Modules",
"version": "1.0.1", "version": "1.0.1",
"update": { "update": [
"add": { {
"https://....": "..." "download": {
}, "https://raw.githubusercontent.com/Orange-Management/Orange-Management/develop/README.md": "README.md"
"replace": { },
"https://....": "" "move": {
}, "toMove": "moveHere"
"move": { },
"https://....": "" "copy": {
}, "toCopy": [
"remove": [ "copyHere"
"...." ]
], },
"run": [ "delete": [
"...." "Remove"
], ],
"cmd": [ "cmd": [
"...." "/Package/run.php",
] "/Package/run.sh",
} "/Package/run.batch"
]
}
]
} }