You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Application.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\AppInfo;
  22. use Closure;
  23. use OCA\WorkflowEngine\Controller\RequestTime;
  24. use OCA\WorkflowEngine\Helper\LogContext;
  25. use OCA\WorkflowEngine\Listener\LoadAdditionalSettingsScriptsListener;
  26. use OCA\WorkflowEngine\Manager;
  27. use OCA\WorkflowEngine\Service\Logger;
  28. use OCP\AppFramework\App;
  29. use OCP\AppFramework\Bootstrap\IBootContext;
  30. use OCP\AppFramework\Bootstrap\IBootstrap;
  31. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  32. use OCP\AppFramework\QueryException;
  33. use OCP\EventDispatcher\Event;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\ILogger;
  36. use OCP\IServerContainer;
  37. use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
  38. use OCP\WorkflowEngine\IEntity;
  39. use OCP\WorkflowEngine\IEntityCompat;
  40. use OCP\WorkflowEngine\IOperation;
  41. use OCP\WorkflowEngine\IOperationCompat;
  42. class Application extends App implements IBootstrap {
  43. public const APP_ID = 'workflowengine';
  44. public function __construct() {
  45. parent::__construct(self::APP_ID);
  46. }
  47. public function register(IRegistrationContext $context): void {
  48. $context->registerServiceAlias('RequestTimeController', RequestTime::class);
  49. $context->registerEventListener(
  50. LoadSettingsScriptsEvent::class,
  51. LoadAdditionalSettingsScriptsListener::class,
  52. -100
  53. );
  54. }
  55. public function boot(IBootContext $context): void {
  56. $context->injectFn(Closure::fromCallable([$this, 'registerRuleListeners']));
  57. }
  58. private function registerRuleListeners(IEventDispatcher $dispatcher,
  59. IServerContainer $container,
  60. ILogger $logger): void {
  61. /** @var Manager $manager */
  62. $manager = $container->query(Manager::class);
  63. $configuredEvents = $manager->getAllConfiguredEvents();
  64. foreach ($configuredEvents as $operationClass => $events) {
  65. foreach ($events as $entityClass => $eventNames) {
  66. array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass) {
  67. $dispatcher->addListener(
  68. $eventName,
  69. function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass) {
  70. $ruleMatcher = $manager->getRuleMatcher();
  71. try {
  72. /** @var IEntity $entity */
  73. $entity = $container->query($entityClass);
  74. /** @var IOperation $operation */
  75. $operation = $container->query($operationClass);
  76. $ruleMatcher->setEventName($eventName);
  77. $ruleMatcher->setEntity($entity);
  78. $ruleMatcher->setOperation($operation);
  79. $ctx = new LogContext();
  80. $ctx
  81. ->setOperation($operation)
  82. ->setEntity($entity)
  83. ->setEventName($eventName);
  84. /** @var Logger $flowLogger */
  85. $flowLogger = $container->query(Logger::class);
  86. $flowLogger->logEventInit($ctx);
  87. if ($event instanceof Event) {
  88. $entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
  89. $operation->onEvent($eventName, $event, $ruleMatcher);
  90. } elseif ($entity instanceof IEntityCompat && $operation instanceof IOperationCompat) {
  91. // TODO: Remove this block (and the compat classes) in the first major release in 2023
  92. $entity->prepareRuleMatcherCompat($ruleMatcher, $eventName, $event);
  93. $operation->onEventCompat($eventName, $event, $ruleMatcher);
  94. } else {
  95. $logger->debug(
  96. 'Cannot handle event {name} of {event} against entity {entity} and operation {operation}',
  97. [
  98. 'app' => self::APP_ID,
  99. 'name' => $eventName,
  100. 'event' => get_class($event),
  101. 'entity' => $entityClass,
  102. 'operation' => $operationClass,
  103. ]
  104. );
  105. }
  106. $flowLogger->logEventDone($ctx);
  107. } catch (QueryException $e) {
  108. // Ignore query exceptions since they might occur when an entity/operation were setup before by an app that is disabled now
  109. }
  110. }
  111. );
  112. }, $eventNames ?? []);
  113. }
  114. }
  115. }
  116. }