2016-07-26 12:16:34 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\WorkflowEngine\AppInfo;
|
|
|
|
|
2020-07-16 18:08:03 +03:00
|
|
|
use Closure;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\WorkflowEngine\Controller\RequestTime;
|
2020-01-14 12:43:50 +03:00
|
|
|
use OCA\WorkflowEngine\Helper\LogContext;
|
2020-06-23 22:09:09 +03:00
|
|
|
use OCA\WorkflowEngine\Listener\LoadAdditionalSettingsScriptsListener;
|
2019-09-03 13:42:57 +03:00
|
|
|
use OCA\WorkflowEngine\Manager;
|
2020-01-14 12:43:50 +03:00
|
|
|
use OCA\WorkflowEngine\Service\Logger;
|
2020-06-19 02:16:53 +03:00
|
|
|
use OCP\AppFramework\App;
|
2020-06-23 21:57:09 +03:00
|
|
|
use OCP\AppFramework\Bootstrap\IBootContext;
|
|
|
|
use OCP\AppFramework\Bootstrap\IBootstrap;
|
|
|
|
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
2019-10-15 13:54:22 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
2019-11-06 12:54:37 +03:00
|
|
|
use OCP\EventDispatcher\Event;
|
2020-06-19 02:16:53 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2020-06-23 21:57:09 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IServerContainer;
|
2019-09-09 18:17:39 +03:00
|
|
|
use OCP\WorkflowEngine\IEntity;
|
2019-11-06 12:54:37 +03:00
|
|
|
use OCP\WorkflowEngine\IEntityCompat;
|
2019-09-03 13:42:57 +03:00
|
|
|
use OCP\WorkflowEngine\IOperation;
|
2019-11-06 12:54:37 +03:00
|
|
|
use OCP\WorkflowEngine\IOperationCompat;
|
2018-01-26 01:16:13 +03:00
|
|
|
|
2020-06-23 21:57:09 +03:00
|
|
|
class Application extends App implements IBootstrap {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const APP_ID = 'workflowengine';
|
2019-08-09 14:24:48 +03:00
|
|
|
|
2016-07-26 12:16:34 +03:00
|
|
|
public function __construct() {
|
2019-08-09 14:24:48 +03:00
|
|
|
parent::__construct(self::APP_ID);
|
2020-06-23 21:57:09 +03:00
|
|
|
}
|
2016-07-26 12:16:34 +03:00
|
|
|
|
2020-06-23 21:57:09 +03:00
|
|
|
public function register(IRegistrationContext $context): void {
|
|
|
|
$context->registerServiceAlias('RequestTimeController', RequestTime::class);
|
2020-06-23 22:09:09 +03:00
|
|
|
$context->registerEventListener(
|
|
|
|
'OCP\WorkflowEngine::loadAdditionalSettingScripts',
|
|
|
|
LoadAdditionalSettingsScriptsListener::class,
|
|
|
|
-100
|
|
|
|
);
|
2020-06-23 21:57:09 +03:00
|
|
|
}
|
2019-09-03 13:42:57 +03:00
|
|
|
|
2020-06-23 21:57:09 +03:00
|
|
|
public function boot(IBootContext $context): void {
|
2020-07-16 18:08:03 +03:00
|
|
|
$context->injectFn(Closure::fromCallable([$this, 'registerRuleListeners']));
|
2016-07-26 12:16:34 +03:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:57:09 +03:00
|
|
|
private function registerRuleListeners(IEventDispatcher $dispatcher,
|
|
|
|
IServerContainer $container,
|
|
|
|
ILogger $logger): void {
|
2020-06-23 22:09:09 +03:00
|
|
|
$manager = $container->query(Manager::class);
|
|
|
|
$configuredEvents = $manager->getAllConfiguredEvents();
|
2019-09-03 13:42:57 +03:00
|
|
|
|
|
|
|
foreach ($configuredEvents as $operationClass => $events) {
|
|
|
|
foreach ($events as $entityClass => $eventNames) {
|
2020-06-23 22:09:09 +03:00
|
|
|
array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass) {
|
2020-06-23 21:57:09 +03:00
|
|
|
$dispatcher->addListener(
|
2019-09-03 13:42:57 +03:00
|
|
|
$eventName,
|
2020-06-23 22:09:09 +03:00
|
|
|
function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass) {
|
|
|
|
$ruleMatcher = $manager->getRuleMatcher();
|
2019-10-15 13:54:22 +03:00
|
|
|
try {
|
|
|
|
/** @var IEntity $entity */
|
2020-06-23 21:57:09 +03:00
|
|
|
$entity = $container->query($entityClass);
|
2019-10-15 13:54:22 +03:00
|
|
|
/** @var IOperation $operation */
|
2020-06-23 21:57:09 +03:00
|
|
|
$operation = $container->query($operationClass);
|
2019-11-06 12:54:37 +03:00
|
|
|
|
2019-11-27 15:59:34 +03:00
|
|
|
$ruleMatcher->setEntity($entity);
|
|
|
|
$ruleMatcher->setOperation($operation);
|
|
|
|
|
2020-01-14 12:43:50 +03:00
|
|
|
$ctx = new LogContext();
|
|
|
|
$ctx
|
|
|
|
->setOperation($operation)
|
|
|
|
->setEntity($entity)
|
|
|
|
->setEventName($eventName);
|
|
|
|
|
|
|
|
/** @var Logger $flowLogger */
|
2020-06-23 21:57:09 +03:00
|
|
|
$flowLogger = $container->query(Logger::class);
|
2020-01-14 12:43:50 +03:00
|
|
|
$flowLogger->logEventInit($ctx);
|
|
|
|
|
2019-11-06 12:54:37 +03:00
|
|
|
if ($event instanceof Event) {
|
|
|
|
$entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
|
|
|
|
$operation->onEvent($eventName, $event, $ruleMatcher);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($entity instanceof IEntityCompat && $operation instanceof IOperationCompat) {
|
2019-11-06 12:54:37 +03:00
|
|
|
// TODO: Remove this block (and the compat classes) in the first major release in 2023
|
|
|
|
$entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event);
|
|
|
|
$operation->onEventCompat($eventName, $event, $ruleMatcher);
|
|
|
|
} else {
|
2019-12-16 17:30:50 +03:00
|
|
|
$logger->debug(
|
2019-11-06 12:54:37 +03:00
|
|
|
'Cannot handle event {name} of {event} against entity {entity} and operation {operation}',
|
|
|
|
[
|
|
|
|
'app' => self::APP_ID,
|
|
|
|
'name' => $eventName,
|
|
|
|
'event' => get_class($event),
|
|
|
|
'entity' => $entityClass,
|
|
|
|
'operation' => $operationClass,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2020-01-14 12:43:50 +03:00
|
|
|
$flowLogger->logEventDone($ctx);
|
2019-10-15 13:54:22 +03:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
// Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now
|
|
|
|
}
|
2019-09-03 13:42:57 +03:00
|
|
|
}
|
|
|
|
);
|
2019-12-06 12:32:37 +03:00
|
|
|
}, $eventNames ?? []);
|
2019-09-03 13:42:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 12:16:34 +03:00
|
|
|
}
|