Application.php 4.9 KB

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