dbPool = $app->dbPool; $apiApp->unitId = $app->unitId; $apiApp->accountManager = $app->accountManager; $apiApp->appSettings = $app->appSettings; $apiApp->moduleManager = $app->moduleManager; $apiApp->eventManager = $app->eventManager; self::createTriggers($apiApp, $workflowData['triggers'] ?? []); self::createActions($apiApp, $workflowData['actions'] ?? []); self::createWorkflows($apiApp, $workflowData['workflows'] ?? []); self::installWorkflow($apiApp, $workflowData['templates'] ?? []); return []; } /** * Create a workflow template * * @param ApplicationAbstract $app Application * @param array $data Workflow schemas * * @return void * * @since 1.0.0 */ private static function createWorkflows(ApplicationAbstract $app, array $data) : void { /** @var \Modules\Workflow\Controller\ApiController $module */ $module = $app->moduleManager->get('Workflow'); foreach ($data as $name => $workflow) { $response = new HttpResponse(); $request = new HttpRequest(); $request->header->account = 1; $request->setData('name', $name); $request->setData('schema', \json_encode($workflow)); $module->apiWorkflowTemplateCreate($request, $response); } } /** * Install trigger. * * @param ApplicationAbstract $app Application * @param array $data Additional data * * @return void * * @since 1.0.0 */ private static function createTriggers(ApplicationAbstract $app, array $data) : void { $path = __DIR__ . '/../Definitions/triggers.json'; if (!\is_file($path)) { return; } $installed = \file_get_contents($path); if ($installed === false) { return; } $installedData = \json_decode($installed, true); if ($installedData === false) { return; } $new = $installedData + $data; \file_put_contents($path, \json_encode($new)); } /** * Install action. * * @param ApplicationAbstract $app Application * @param array $data Additional data * * @return void * * @since 1.0.0 */ private static function createActions(ApplicationAbstract $app, array $data) : void { $path = __DIR__ . '/../Definitions/actions.json'; if (!\is_file($path)) { return; } $installed = \file_get_contents($path); if ($installed === false) { return; } $installedData = \json_decode($installed, true); if ($installedData === false) { return; } $new = $installedData + $data; \file_put_contents($path, \json_encode($new)); } /** * Install application page. * * @param ApplicationAbstract $app Application * @param array $data Additional data * * @return void * * @since 1.0.0 */ private static function installWorkflow(ApplicationAbstract $app, array $data) : void { /** @var \Modules\Workflow\Controller\ApiController $module */ $module = $app->moduleManager->get('Workflow'); foreach ($data as $template) { $response = new HttpResponse(); $request = new HttpRequest(); $request->header->account = 1; $request->setData('name', $template['name']); $tempPath = __DIR__ . '/../../../temp/'; $workflowFiles = \scandir(__DIR__ . '/../../..' . $template['path']); if ($workflowFiles === false) { return; } foreach ($workflowFiles as $filePath) { if (!\is_file(__DIR__ . '/../../..' . $template['path'] . '/' . $filePath) || $filePath === '..' || $filePath === '.') { continue; } \copy( __DIR__ . '/../../..' . $template['path'] . '/' . $filePath, $tempPath . $filePath ); $request->addFile([ 'error' => \UPLOAD_ERR_OK, 'type' => \substr($filePath, \strrpos($filePath, '.') + 1), 'name' => $filePath, 'tmp_name' => $tempPath . $filePath, 'size' => \filesize($tempPath . $filePath), ]); } $module->apiWorkflowTemplateCreate($request, $response); } } }