Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
  • Loading branch information
bigcat88 committed Feb 20, 2024
1 parent 3119193 commit 4cb42d7
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
9 changes: 5 additions & 4 deletions lib/Command/ExApp/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 3;
}

$extractedDir = '';
$appInfo = $this->exAppService->getAppInfo(
$appId, $input->getOption('info-xml'), $input->getOption('json-info'), $extractedDir
$appId, $input->getOption('info-xml'), $input->getOption('json-info')
);
if (isset($appInfo['error'])) {
$this->logger->error($appInfo['error']);
Expand Down Expand Up @@ -175,13 +174,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if ($daemonConfig->getAcceptsDeployId() !== $this->manualActions->getAcceptsDeployId()) {
$result = $this->exAppArchiveFetcher->installTranslations($appId, $extractedDir);
if (!empty($appInfo['external-app']['translations_folder'])) {
$result = $this->exAppArchiveFetcher->installTranslations($appId, $appInfo['external-app']['translations_folder']);
if ($result) {
$this->logger->error(sprintf('Failed to install translations for %s. Reason: %s', $appId, $result));
if ($outputConsole) {
$output->writeln(sprintf('Failed to install translations for %s. Reason: %s', $appId, $result));
}
$this->exAppService->unregisterExApp($appId);
return 3;
}
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Command/ExApp/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$outputConsole = !$input->getOption('silent');
$appId = $input->getArgument('appid');

$extractedDir = '';
$appInfo = $this->exAppService->getAppInfo(
$appId, $input->getOption('info-xml'), $input->getOption('json-info'), $extractedDir
$appId, $input->getOption('info-xml'), $input->getOption('json-info')
);
if (isset($appInfo['error'])) {
$this->logger->error($appInfo['error']);
Expand Down Expand Up @@ -121,8 +120,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

if ($daemonConfig->getAcceptsDeployId() !== $this->manualActions->getAcceptsDeployId()) {
$result = $this->exAppArchiveFetcher->installTranslations($appId, $extractedDir);
if (!empty($appInfo['external-app']['translations_folder'])) {
$result = $this->exAppArchiveFetcher->installTranslations($appId, $appInfo['external-app']['translations_folder']);
if ($result) {
$this->logger->error(sprintf('Failed to install translations for %s. Reason: %s', $appId, $result));
if ($outputConsole) {
Expand Down
41 changes: 23 additions & 18 deletions lib/Fetcher/ExAppArchiveFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,20 @@ public function downloadInfoXml(array $exAppAppstoreData, string &$extractedDir)
return $infoXml;
}

public function installTranslations(string $appId, string $extractedDir): string {
if (!$extractedDir) {
return '';
}
if (!file_exists($extractedDir)) {
return 'Missing temp directory with ExApp files';
public function installTranslations(string $appId, string $dirTranslations): string {
if (!file_exists($dirTranslations)) {
return sprintf('Can not access directory: %s', $dirTranslations);
}
$writableAppPath = $this->getExAppFolder($appId);
if (!$writableAppPath) {
return 'Can not find writable apps path to perform install.';
return 'Can not find writable apps path to perform installation.';
}

$installL10NPath = $writableAppPath . '/' . $appId . '/l10n';
$installL10NPath = $writableAppPath . '/l10n';
if (file_exists($installL10NPath)) {
$this->rmdirr($installL10NPath); // Remove old l10n folder and files if exists
}
// Move l10n folder from extracted temp to the app folder
rename($extractedDir . '/l10n', $installL10NPath);
$this->copyr($dirTranslations, $installL10NPath);
return '';
}

Expand Down Expand Up @@ -214,14 +210,7 @@ private function splitCerts(string $cert): array {
return $matches[0];
}

/**
* Recursive deletion of folders
*
* @param string $dir path to the folder
* @param bool $deleteSelf if set to false only the content of the folder will be deleted
* @return bool
*/
public static function rmdirr(string $dir, bool $deleteSelf = true): bool {
public function rmdirr(string $dir, bool $deleteSelf = true): bool {
if (is_dir($dir)) {
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
Expand Down Expand Up @@ -251,4 +240,20 @@ public static function rmdirr(string $dir, bool $deleteSelf = true): bool {

return !file_exists($dir);
}

public function copyr(string $src, string $dest): void {
if (is_dir($src)) {
if (!is_dir($dest)) {
mkdir($dest);
}
$files = scandir($src);
foreach ($files as $file) {
if ($file != "." && $file != "..") {
self::copyr("$src/$file", "$dest/$file");
}
}
} elseif (file_exists($src)) {
copy($src, $dest);
}
}
}
2 changes: 1 addition & 1 deletion lib/Middleware/ExAppUIL10NMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function beforeOutput(Controller $controller, string $methodName, string
$nonce = $this->nonceManager->getNonce();
$output = substr_replace($output, '<script nonce="'.$nonce.'" defer src="' . $l10nScriptSrc . '"></script>', $headPos, 0);
} catch (AppPathNotFoundException) {
$this->logger->error(sprintf('Can not find translations for %s ExApp.', $appId));
$this->logger->debug(sprintf('Can not find translations for %s ExApp.', $appId));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Middleware/ExAppUiMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function beforeOutput(Controller $controller, string $methodName, string
$nonce = $this->nonceManager->getNonce();
$output = substr_replace($output, '<script nonce="'.$nonce.'" defer src="' . $l10nScriptSrc . '"></script>', $headPos, 0);
} catch (AppPathNotFoundException) {
$this->logger->error(sprintf('Can not find translations for %s ExApp.', $appId));
$this->logger->debug(sprintf('Can not find translations for %s ExApp.', $appId));
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions lib/Service/ExAppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,14 @@ private function resetCaches(): void {
$this->settingsService->resetCacheEnabled();
}

public function getAppInfo(string $appId, ?string $infoXml, ?string $jsonInfo, string &$extractedDir): array {
public function getAppInfo(string $appId, ?string $infoXml, ?string $jsonInfo): array {
$extractedDir = '';
if ($jsonInfo !== null) {
$appInfo = json_decode($jsonInfo, true);
# fill 'id' if it is missing(this field was called `appid` in previous versions in json)
$appInfo['id'] = $appInfo['id'] ?? $appId;
# during manual install JSON can have all values at root level
foreach (['docker-install', 'scopes', 'system'] as $key) {
foreach (['docker-install', 'scopes', 'system', 'translations_folder'] as $key) {
if (isset($appInfo[$key])) {
$appInfo['external-app'][$key] = $appInfo[$key];
unset($appInfo[$key]);
Expand All @@ -298,6 +299,13 @@ public function getAppInfo(string $appId, ?string $infoXml, ?string $jsonInfo, s
if (isset($appInfo['external-app']['scopes']['value'])) {
$appInfo['external-app']['scopes'] = $appInfo['external-app']['scopes']['value'];
}
if ($extractedDir) {
if (file_exists($extractedDir . '/l10n')) {
$appInfo['translations_folder'] = $extractedDir . '/l10n';
} else {
$this->logger->info(sprintf('Application %s does not support translations', $appId));
}
}
# TO-DO: remove this in AppAPI 2.3.0
if (isset($appInfo['external-app']['scopes']['required']['value'])) {
$appInfo['external-app']['scopes'] = $appInfo['external-app']['scopes']['required']['value'];
Expand Down

0 comments on commit 4cb42d7

Please sign in to comment.