Application.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\AppInfo;
  7. use Closure;
  8. use OCA\WorkflowEngine\Helper\LogContext;
  9. use OCA\WorkflowEngine\Listener\LoadAdditionalSettingsScriptsListener;
  10. use OCA\WorkflowEngine\Manager;
  11. use OCA\WorkflowEngine\Service\Logger;
  12. use OCP\AppFramework\App;
  13. use OCP\AppFramework\Bootstrap\IBootContext;
  14. use OCP\AppFramework\Bootstrap\IBootstrap;
  15. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  16. use OCP\EventDispatcher\Event;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
  19. use OCP\WorkflowEngine\IEntity;
  20. use OCP\WorkflowEngine\IOperation;
  21. use Psr\Container\ContainerExceptionInterface;
  22. use Psr\Container\ContainerInterface;
  23. use Psr\Log\LoggerInterface;
  24. class Application extends App implements IBootstrap {
  25. public const APP_ID = 'workflowengine';
  26. public function __construct() {
  27. parent::__construct(self::APP_ID);
  28. }
  29. public function register(IRegistrationContext $context): void {
  30. $context->registerEventListener(
  31. LoadSettingsScriptsEvent::class,
  32. LoadAdditionalSettingsScriptsListener::class,
  33. -100
  34. );
  35. }
  36. public function boot(IBootContext $context): void {
  37. $context->injectFn(Closure::fromCallable([$this, 'registerRuleListeners']));
  38. }
  39. private function registerRuleListeners(IEventDispatcher $dispatcher,
  40. ContainerInterface $container,
  41. LoggerInterface $logger): void {
  42. /** @var Manager $manager */
  43. $manager = $container->get(Manager::class);
  44. $configuredEvents = $manager->getAllConfiguredEvents();
  45. foreach ($configuredEvents as $operationClass => $events) {
  46. foreach ($events as $entityClass => $eventNames) {
  47. array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass): void {
  48. $dispatcher->addListener(
  49. $eventName,
  50. function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass): void {
  51. $ruleMatcher = $manager->getRuleMatcher();
  52. try {
  53. /** @var IEntity $entity */
  54. $entity = $container->get($entityClass);
  55. /** @var IOperation $operation */
  56. $operation = $container->get($operationClass);
  57. $ruleMatcher->setEventName($eventName);
  58. $ruleMatcher->setEntity($entity);
  59. $ruleMatcher->setOperation($operation);
  60. $ctx = new LogContext();
  61. $ctx
  62. ->setOperation($operation)
  63. ->setEntity($entity)
  64. ->setEventName($eventName);
  65. /** @var Logger $flowLogger */
  66. $flowLogger = $container->get(Logger::class);
  67. $flowLogger->logEventInit($ctx);
  68. if ($event instanceof Event) {
  69. $entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
  70. $operation->onEvent($eventName, $event, $ruleMatcher);
  71. } else {
  72. $logger->debug(
  73. 'Cannot handle event {name} of {event} against entity {entity} and operation {operation}',
  74. [
  75. 'app' => self::APP_ID,
  76. 'name' => $eventName,
  77. 'event' => get_class($event),
  78. 'entity' => $entityClass,
  79. 'operation' => $operationClass,
  80. ]
  81. );
  82. }
  83. $flowLogger->logEventDone($ctx);
  84. } catch (ContainerExceptionInterface $e) {
  85. // Ignore query exceptions since they might occur when an entity/operation were set up before by an app that is disabled now
  86. }
  87. }
  88. );
  89. }, $eventNames ?? []);
  90. }
  91. }
  92. }
  93. }