2016-07-26 05:16:34 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-07-26 05:16:34 -04:00
|
|
|
/**
|
2024-05-30 14:13:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-07-26 05:16:34 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\WorkflowEngine\AppInfo;
|
|
|
|
|
|
2020-07-16 11:08:03 -04:00
|
|
|
use Closure;
|
2020-01-14 04:43:50 -05:00
|
|
|
use OCA\WorkflowEngine\Helper\LogContext;
|
2020-06-23 15:09:09 -04:00
|
|
|
use OCA\WorkflowEngine\Listener\LoadAdditionalSettingsScriptsListener;
|
2019-09-03 06:42:57 -04:00
|
|
|
use OCA\WorkflowEngine\Manager;
|
2020-01-14 04:43:50 -05:00
|
|
|
use OCA\WorkflowEngine\Service\Logger;
|
2020-06-18 19:16:53 -04:00
|
|
|
use OCP\AppFramework\App;
|
2020-06-23 14:57:09 -04:00
|
|
|
use OCP\AppFramework\Bootstrap\IBootContext;
|
|
|
|
|
use OCP\AppFramework\Bootstrap\IBootstrap;
|
|
|
|
|
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
2019-11-06 04:54:37 -05:00
|
|
|
use OCP\EventDispatcher\Event;
|
2020-06-18 19:16:53 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2020-08-10 08:29:21 -04:00
|
|
|
use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
|
2019-09-09 11:17:39 -04:00
|
|
|
use OCP\WorkflowEngine\IEntity;
|
2019-09-03 06:42:57 -04:00
|
|
|
use OCP\WorkflowEngine\IOperation;
|
2023-03-03 06:48:14 -05:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2018-01-25 17:16:13 -05:00
|
|
|
|
2020-06-23 14:57:09 -04:00
|
|
|
class Application extends App implements IBootstrap {
|
2020-04-10 10:54:27 -04:00
|
|
|
public const APP_ID = 'workflowengine';
|
2019-08-09 07:24:48 -04:00
|
|
|
|
2016-07-26 05:16:34 -04:00
|
|
|
public function __construct() {
|
2019-08-09 07:24:48 -04:00
|
|
|
parent::__construct(self::APP_ID);
|
2020-06-23 14:57:09 -04:00
|
|
|
}
|
2016-07-26 05:16:34 -04:00
|
|
|
|
2020-06-23 14:57:09 -04:00
|
|
|
public function register(IRegistrationContext $context): void {
|
2020-06-23 15:09:09 -04:00
|
|
|
$context->registerEventListener(
|
2020-08-10 08:29:21 -04:00
|
|
|
LoadSettingsScriptsEvent::class,
|
2020-06-23 15:09:09 -04:00
|
|
|
LoadAdditionalSettingsScriptsListener::class,
|
|
|
|
|
-100
|
|
|
|
|
);
|
2020-06-23 14:57:09 -04:00
|
|
|
}
|
2019-09-03 06:42:57 -04:00
|
|
|
|
2020-06-23 14:57:09 -04:00
|
|
|
public function boot(IBootContext $context): void {
|
2020-07-16 11:08:03 -04:00
|
|
|
$context->injectFn(Closure::fromCallable([$this, 'registerRuleListeners']));
|
2016-07-26 05:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-23 14:57:09 -04:00
|
|
|
private function registerRuleListeners(IEventDispatcher $dispatcher,
|
2023-11-23 04:22:34 -05:00
|
|
|
ContainerInterface $container,
|
|
|
|
|
LoggerInterface $logger): void {
|
2020-08-21 11:36:01 -04:00
|
|
|
/** @var Manager $manager */
|
2023-03-03 06:48:14 -05:00
|
|
|
$manager = $container->get(Manager::class);
|
2020-06-23 15:09:09 -04:00
|
|
|
$configuredEvents = $manager->getAllConfiguredEvents();
|
2019-09-03 06:42:57 -04:00
|
|
|
|
|
|
|
|
foreach ($configuredEvents as $operationClass => $events) {
|
|
|
|
|
foreach ($events as $entityClass => $eventNames) {
|
2024-09-20 11:38:36 -04:00
|
|
|
array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass): void {
|
2020-06-23 14:57:09 -04:00
|
|
|
$dispatcher->addListener(
|
2019-09-03 06:42:57 -04:00
|
|
|
$eventName,
|
2024-09-20 11:38:36 -04:00
|
|
|
function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass): void {
|
2020-06-23 15:09:09 -04:00
|
|
|
$ruleMatcher = $manager->getRuleMatcher();
|
2019-10-15 06:54:22 -04:00
|
|
|
try {
|
|
|
|
|
/** @var IEntity $entity */
|
2023-03-03 06:48:14 -05:00
|
|
|
$entity = $container->get($entityClass);
|
2019-10-15 06:54:22 -04:00
|
|
|
/** @var IOperation $operation */
|
2023-03-03 06:48:14 -05:00
|
|
|
$operation = $container->get($operationClass);
|
2019-11-06 04:54:37 -05:00
|
|
|
|
2020-08-21 11:36:01 -04:00
|
|
|
$ruleMatcher->setEventName($eventName);
|
2019-11-27 07:59:34 -05:00
|
|
|
$ruleMatcher->setEntity($entity);
|
|
|
|
|
$ruleMatcher->setOperation($operation);
|
|
|
|
|
|
2020-01-14 04:43:50 -05:00
|
|
|
$ctx = new LogContext();
|
|
|
|
|
$ctx
|
|
|
|
|
->setOperation($operation)
|
|
|
|
|
->setEntity($entity)
|
|
|
|
|
->setEventName($eventName);
|
|
|
|
|
|
|
|
|
|
/** @var Logger $flowLogger */
|
2023-03-03 06:48:14 -05:00
|
|
|
$flowLogger = $container->get(Logger::class);
|
2020-01-14 04:43:50 -05:00
|
|
|
$flowLogger->logEventInit($ctx);
|
|
|
|
|
|
2019-11-06 04:54:37 -05:00
|
|
|
if ($event instanceof Event) {
|
|
|
|
|
$entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
|
|
|
|
|
$operation->onEvent($eventName, $event, $ruleMatcher);
|
|
|
|
|
} else {
|
2019-12-16 09:30:50 -05:00
|
|
|
$logger->debug(
|
2019-11-06 04:54:37 -05: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 04:43:50 -05:00
|
|
|
$flowLogger->logEventDone($ctx);
|
2023-03-03 06:48:14 -05:00
|
|
|
} catch (ContainerExceptionInterface $e) {
|
|
|
|
|
// Ignore query exceptions since they might occur when an entity/operation were set up before by an app that is disabled now
|
2019-10-15 06:54:22 -04:00
|
|
|
}
|
2019-09-03 06:42:57 -04:00
|
|
|
}
|
|
|
|
|
);
|
2019-12-06 04:32:37 -05:00
|
|
|
}, $eventNames ?? []);
|
2019-09-03 06:42:57 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-26 05:16:34 -04:00
|
|
|
}
|