|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the MultiFlexi package |
| 7 | + * |
| 8 | + * https://multiflexi.eu/ |
| 9 | + * |
| 10 | + * (c) Vítězslav Dvořák <http://vitexsoftware.com> |
| 11 | + * |
| 12 | + * For the full copyright and license information, please view the LICENSE |
| 13 | + * file that was distributed with this source code. |
| 14 | + */ |
| 15 | + |
| 16 | +namespace MultiFlexi\Api\Server; |
| 17 | + |
| 18 | +use Psr\Http\Message\ResponseInterface; |
| 19 | +use Psr\Http\Message\ServerRequestInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * Description of DefaultApi. |
| 23 | + * |
| 24 | + * @author vitex |
| 25 | + * |
| 26 | + * @no-named-arguments |
| 27 | + */ |
| 28 | +class AppApi extends \MultiFlexi\Api\Server\AbstractAppApi |
| 29 | +{ |
| 30 | + public $engine; |
| 31 | + |
| 32 | + /** |
| 33 | + * App Handler Engine. |
| 34 | + */ |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + $this->engine = new \MultiFlexi\Application(); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * App Info by ID. |
| 42 | + * |
| 43 | + * @url http://localhost/EASE/MultiFlexi/src/api/VitexSoftware/MultiFlexi/1.0.0/app/1 |
| 44 | + */ |
| 45 | + public function getAppById(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, int $appId, string $suffix): \Psr\Http\Message\ResponseInterface |
| 46 | + { |
| 47 | + $this->engine->loadFromSQL($appId); |
| 48 | + $appData = $this->engine->getData(); |
| 49 | + |
| 50 | + // Add environment configuration fields |
| 51 | + $conffield = new \MultiFlexi\Conffield(); |
| 52 | + $envFields = $conffield->appConfigs($appId); |
| 53 | + $appData['environment'] = []; |
| 54 | + |
| 55 | + foreach ($envFields as $keyname => $fieldData) { |
| 56 | + $appData['environment'][$keyname] = [ |
| 57 | + 'type' => $fieldData['type'], |
| 58 | + 'description' => $fieldData['description'], |
| 59 | + 'defval' => $fieldData['defval'], |
| 60 | + 'required' => (bool) $fieldData['required'], |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + // Add exit codes by querying directly |
| 65 | + $appData['exitCodes'] = []; |
| 66 | + $exitCodesEngine = new \MultiFlexi\DBEngine(); |
| 67 | + $exitCodesEngine->myTable = 'app_exit_codes'; |
| 68 | + $exitCodesData = $exitCodesEngine->listingQuery() |
| 69 | + ->where('app_id', $appId) |
| 70 | + ->orderBy('exit_code') |
| 71 | + ->orderBy('lang') |
| 72 | + ->fetchAll(); |
| 73 | + |
| 74 | + foreach ($exitCodesData as $exitCodeRow) { |
| 75 | + $code = (string) $exitCodeRow['exit_code']; |
| 76 | + $lang = $exitCodeRow['lang']; |
| 77 | + |
| 78 | + if (!isset($appData['exitCodes'][$code])) { |
| 79 | + $appData['exitCodes'][$code] = [ |
| 80 | + 'severity' => $exitCodeRow['severity'], |
| 81 | + 'retry' => (bool) $exitCodeRow['retry'], |
| 82 | + 'description' => [], |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + $appData['exitCodes'][$code]['description'][$lang] = $exitCodeRow['description']; |
| 87 | + } |
| 88 | + |
| 89 | + switch ($suffix) { |
| 90 | + case 'html': |
| 91 | + // $appData['name'] = new \Ease\Html\ATag($appData['id'] . '.html', $appData['name']); |
| 92 | + $appData['image'] = new \Ease\Html\ATag($appData['id'].'.html', new \Ease\Html\ImgTag($appData['image'], $appData['name'], ['width' => '64'])); |
| 93 | + $appData = [array_keys($appData), $appData]; |
| 94 | + |
| 95 | + break; |
| 96 | + |
| 97 | + default: |
| 98 | + break; |
| 99 | + } |
| 100 | + |
| 101 | + return DefaultApi::prepareResponse($response, $appData, $suffix, null, 'application'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * All Apps. |
| 106 | + * |
| 107 | + * @url http://localhost/EASE/MultiFlexi/src/api/VitexSoftware/MultiFlexi/1.0.0/apps |
| 108 | + */ |
| 109 | + public function listApps(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, string $suffix): \Psr\Http\Message\ResponseInterface |
| 110 | + { |
| 111 | + $appsList = []; |
| 112 | + |
| 113 | + foreach ($this->engine->getAll() as $app) { |
| 114 | + $appsList[$app['id']] = $app; |
| 115 | + |
| 116 | + switch ($suffix) { |
| 117 | + case 'html': |
| 118 | + $appsList[$app['id']]['name'] = new \Ease\Html\ATag('app/'.$app['id'].'.html', $app['name']); |
| 119 | + |
| 120 | + if (file_exists('../images/'.$app['uuid'].'.svg')) { |
| 121 | + $appIcon = \Ease\Html\ImgTag::fileBase64src('../images/'.$app['uuid'].'.svg'); |
| 122 | + } else { |
| 123 | + $appIcon = $app['image']; |
| 124 | + } |
| 125 | + |
| 126 | + $appsList[$app['id']]['image'] = new \Ease\Html\ATag('app/'.$app['id'].'.html', new \Ease\Html\ImgTag($appIcon, $app['name'], ['width' => '64'])); |
| 127 | + |
| 128 | + break; |
| 129 | + |
| 130 | + default: |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return DefaultApi::prepareResponse($response, $appsList, $suffix, null, 'application'); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * POST setAppById |
| 140 | + * Summary: Create or Update Application |
| 141 | + * Notes: Create or Update App by ID |
| 142 | + * Output-Formats: [application/xml, application/json]. |
| 143 | + * |
| 144 | + * @param ServerRequestInterface $request Request |
| 145 | + * @param ResponseInterface $response Response |
| 146 | + */ |
| 147 | + public function setAppById(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface |
| 148 | + { |
| 149 | + $queryParams = $request->getQueryParams(); |
| 150 | + $appId = (\array_key_exists('appId', $queryParams)) ? $queryParams['appId'] : null; |
| 151 | + $appInfo = ['id' => $appId, 'success' => $this->engine->dbsync($queryParams)]; |
| 152 | + |
| 153 | + return DefaultApi::prepareResponse($response, $appInfo, $suffix, 'app'.$appId); |
| 154 | + } |
| 155 | +} |
0 commit comments