prepare impl. in combination with cli offloading

This commit is contained in:
Dennis Eichhorn 2022-03-11 23:21:21 +01:00
parent 343b665300
commit 78391c8cd4
9 changed files with 368 additions and 141 deletions

20
Admin/Hooks/Cli.php Normal file
View File

@ -0,0 +1,20 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
return [
'/.*/' => [
'callback' => ['\Modules\Workflow\Controller\CliController:findWorkflow'],
],
];

19
Admin/Hooks/Web/Api.php Normal file
View File

@ -0,0 +1,19 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
return [
'/.*/' => [
'callback' => ['\Modules\Workflow\Controller\ApiController:cliEventCall'],
],
];

View File

@ -1,38 +1,38 @@
{
"workflow": {
"name": "workflow",
"name": "workflow_template",
"fields": {
"workflow_id": {
"name": "workflow_id",
"workflow_template_id": {
"name": "workflow_template_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"workflow_name": {
"name": "workflow_name",
"workflow_template_name": {
"name": "workflow_template_name",
"type": "VARCHAR(255)",
"default": null,
"null": true
},
"workflow_status": {
"name": "workflow_status",
"workflow_template_status": {
"name": "workflow_template_status",
"type": "INT",
"null": false
},
"workflow_desc": {
"name": "workflow_desc",
"workflow_template_desc": {
"name": "workflow_template_desc",
"type": "TEXT",
"default": null,
"null": true
},
"workflow_created": {
"name": "workflow_created",
"workflow_template_created": {
"name": "workflow_template_created",
"type": "DATETIME",
"null": false
},
"workflow_created_by": {
"name": "workflow_created_by",
"workflow_template_created_by": {
"name": "workflow_template_created_by",
"type": "INT",
"null": false,
"foreignTable": "account",
@ -40,139 +40,35 @@
}
}
},
"workflow_media": {
"name": "workflow_media",
"workflow_template_media": {
"name": "workflow_template_media",
"fields": {
"workflow_media_id": {
"name": "workflow_media_id",
"workflow_template_media_id": {
"name": "workflow_template_media_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"workflow_media_media": {
"name": "workflow_media_media",
"workflow_template_media_media": {
"name": "workflow_template_media_media",
"type": "INT",
"null": false,
"foreignTable": "media",
"foreignKey": "media_id"
},
"workflow_media_workflow": {
"name": "workflow_media_workflow",
"workflow_template_media_workflow": {
"name": "workflow_template_media_workflow",
"type": "INT",
"null": false,
"foreignTable": "workflow",
"foreignKey": "workflow_id"
"foreignTable": "workflow_template",
"foreignKey": "workflow_template_id"
},
"workflow_media_type": {
"name": "workflow_media_type",
"workflow_template_media_type": {
"name": "workflow_template_media_type",
"type": "TINYINT",
"null": false
}
}
},
"workflow_element": {
"name": "workflow_element",
"fields": {
"workflow_element_id": {
"name": "workflow_element_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"workflow_element_name": {
"name": "workflow_element_name",
"type": "VARCHAR(255)",
"null": false
},
"workflow_element_status": {
"name": "workflow_element_status",
"type": "INT",
"null": false
},
"workflow_element_data": {
"name": "workflow_element_data",
"type": "TEXT",
"null": false
},
"workflow_element_desc": {
"name": "workflow_element_desc",
"type": "TEXT",
"default": null,
"null": true
},
"workflow_element_created": {
"name": "workflow_element_created",
"type": "DATETIME",
"null": false
},
"workflow_element_created_by": {
"name": "workflow_element_created_by",
"type": "INT",
"null": false,
"foreignTable": "account",
"foreignKey": "account_id"
},
"workflow_element_workflow": {
"name": "workflow_element_workflow",
"type": "INT",
"null": false,
"foreignTable": "workflow",
"foreignKey": "workflow_id"
}
}
},
"workflow_element_media": {
"name": "workflow_element_media",
"fields": {
"workflow_element_media_id": {
"name": "workflow_element_media_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"workflow_element_media_media": {
"name": "workflow_element_media_media",
"type": "INT",
"null": false,
"foreignTable": "media",
"foreignKey": "media_id"
},
"workflow_element_media_workflow_element": {
"name": "workflow_element_media_workflow_element",
"type": "INT",
"null": false,
"foreignTable": "workflow_element",
"foreignKey": "workflow_element_id"
}
}
},
"workflow_element_task": {
"name": "workflow_element_task",
"fields": {
"workflow_element_task_id": {
"name": "workflow_element_task_id",
"type": "INT",
"null": false,
"primary": true,
"autoincrement": true
},
"workflow_element_task_task": {
"name": "workflow_element_task_task",
"type": "INT",
"null": false,
"foreignTable": "task",
"foreignKey": "task_id"
},
"workflow_element_task_workflow_element": {
"name": "workflow_element_task_workflow_element",
"type": "INT",
"null": false,
"foreignTable": "workflow_element",
"foreignKey": "workflow_element_id"
}
}
}
}

View File

@ -0,0 +1,56 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
namespace Modules\Workflow\Controller;
use phpOMS\System\SystemUtils;
/**
* Workflow controller class.
*
* @package Modules\Workflow
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
final class ApiController extends Controller
{
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
* @todo maybe this needs to be moved to the admin module if there every is another hook which uses .* regex-match and is forwarded to the cli application
*/
public function cliEventCall(...$data) : void
{
$count = \count($data);
// @todo: if no Cli is available do it in the web app (maybe first web request and if this is also not allowed run it in here)
SystemUtils::runProc(
'php',
__DIR__ . '/../../../cli.php' . ' '
. 'post:/admin/event' . ' '
. '-g ' . \escapeshellarg($data[$count - 2]) . ' '
. '-i ' . \escapeshellarg($data[$count - 1]) . ' '
. '-d ' . \escapeshellarg(\json_encode($data)),
true
);
}
}

View File

@ -0,0 +1,85 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
namespace Modules\Workflow\Controller;
use Modules\Workflow\Models\WorkflowStatus;
use Modules\Workflow\Models\WorkflowTemplate;
use Modules\Workflow\Models\WorkflowTemplateMapper;
use phpOMS\System\SystemUtils;
/**
* Workflow controller class.
*
* @package Modules\Workflow
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
final class CliController extends Controller
{
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function findWorkflow(...$data) : void
{
$workflows = WorkflowTemplateMapper::getAll()->where('status', WorkflowStatus::ACTIVE)->execute();
foreach ($workflows as $workflow) {
$hooks = $workflow->getHooks();
foreach ($hooks as $hook) {
$triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0;
$matched = false;
if ($triggerIsRegex) {
$matched = \preg_match($data[':triggerGroup'], $hook) === 1;
} else {
$matched = $data[':triggerGroup'] === $hook;
}
if (!$matched && \stripos($hook, '/') === 0) {
$matched = \preg_match($hook, $data[':triggerGroup']) === 1;
}
if ($matched) {
$this->runWorkflow($workflow, $hook, $data);
}
}
}
}
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void
{
include $workflow->media->getAbsolutePath();
}
}

View File

@ -14,14 +14,27 @@ declare(strict_types=1);
namespace Modules\Workflow\Models;
use phpOMS\Stdlib\Base\Enum;
/**
* Workflow template class.
* Workflow state enum.
*
* @package Modules\Workflow\Models
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
class Template
abstract class WorkflowState extends Enum
{
public const OPEN = 1;
public const WORKING = 2;
public const SUSPENDED = 3;
public const CANCELED = 4;
public const DONE = 5;
public const CLOSED = 6;
}

12
Models/WorkflowStatus.php Executable file → Normal file
View File

@ -26,15 +26,7 @@ use phpOMS\Stdlib\Base\Enum;
*/
abstract class WorkflowStatus extends Enum
{
public const OPEN = 1;
public const ACTIVE = 1;
public const WORKING = 2;
public const SUSPENDED = 3;
public const CANCELED = 4;
public const DONE = 5;
public const CLOSED = 6;
public const INACTIVE = 2;
}

View File

@ -0,0 +1,76 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
namespace Modules\Workflow\Models;
use Modules\Admin\Models\Account;
use Modules\Admin\Models\NullAccount;
/**
* Workflow template class.
*
* @package Modules\Workflow\Models
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
class WorkflowTemplate
{
/**
* ID.
*
* @var int
* @since 1.0.0
*/
protected int $id = 0;
/**
* Creator.
*
* @var Account
* @since 1.0.0
*/
public Account $createdBy;
/**
* Created.
*
* @var \DateTimeImmutable
* @since 1.0.0
*/
public \DateTimeImmutable $createdAt;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
$this->createdBy = new NullAccount();
$this->createdAt = new \DateTimeImmutable('now');
}
/**
* Get id
*
* @return int
*
* @since 1.0.0
*/
public function getId() : int
{
return $this->id;
}
}

View File

@ -0,0 +1,70 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
namespace Modules\Workflow\Models;
use Modules\Admin\Models\AccountMapper;
use Modules\Calendar\Models\ScheduleMapper;
use Modules\Media\Models\MediaMapper;
use Modules\Tag\Models\TagMapper;
use phpOMS\DataStorage\Database\Mapper\DataMapperFactory;
use phpOMS\DataStorage\Database\Mapper\ReadMapper;
use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\Database\Query\Where;
/**
* Mapper class.
*
* @package Modules\Workflow\Models
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
final class WorkflowTemplateMapper extends DataMapperFactory
{
/**
* Columns.
*
* @var array<string, array{name:string, type:string, internal:string, autocomplete?:bool, readonly?:bool, writeonly?:bool, annotations?:array}>
* @since 1.0.0
*/
public const COLUMNS = [
'workflow_template_id' => ['name' => 'workflow_template_id', 'type' => 'int', 'internal' => 'id'],
'workflow_template_created_at' => ['name' => 'workflow_template_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true],
];
/**
* Primary table.
*
* @var string
* @since 1.0.0
*/
public const TABLE = 'workflow_template';
/**
* Created at.
*
* @var string
* @since 1.0.0
*/
public const CREATED_AT = 'workflow_template_created_at';
/**
* Primary field name.
*
* @var string
* @since 1.0.0
*/
public const PRIMARYFIELD ='workflow_template_id';
}