Application.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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\EventDispatcher\Event;
  39. use OCP\EventDispatcher\IEventDispatcher;
  40. use OCP\WorkflowEngine\Events\LoadSettingsScriptsEvent;
  41. use OCP\WorkflowEngine\IEntity;
  42. use OCP\WorkflowEngine\IOperation;
  43. use Psr\Container\ContainerExceptionInterface;
  44. use Psr\Container\ContainerInterface;
  45. use Psr\Log\LoggerInterface;
  46. class Application extends App implements IBootstrap {
  47. public const APP_ID = 'workflowengine';
  48. public function __construct() {
  49. parent::__construct(self::APP_ID);
  50. }
  51. public function register(IRegistrationContext $context): void {
  52. $context->registerServiceAlias('RequestTimeController', RequestTime::class);
  53. $context->registerEventListener(
  54. LoadSettingsScriptsEvent::class,
  55. LoadAdditionalSettingsScriptsListener::class,
  56. -100
  57. );
  58. }
  59. public function boot(IBootContext $context): void {
  60. $context->injectFn(Closure::fromCallable([$this, 'registerRuleListeners']));
  61. }
  62. private function registerRuleListeners(IEventDispatcher $dispatcher,
  63. ContainerInterface $container,
  64. LoggerInterface $logger): void {
  65. /** @var Manager $manager */
  66. $manager = $container->get(Manager::class);
  67. $configuredEvents = $manager->getAllConfiguredEvents();
  68. foreach ($configuredEvents as $operationClass => $events) {
  69. foreach ($events as $entityClass => $eventNames) {
  70. array_map(function (string $eventName) use ($manager, $container, $dispatcher, $logger, $operationClass, $entityClass) {
  71. $dispatcher->addListener(
  72. $eventName,
  73. function ($event) use ($manager, $container, $eventName, $logger, $operationClass, $entityClass) {
  74. $ruleMatcher = $manager->getRuleMatcher();
  75. try {
  76. /** @var IEntity $entity */
  77. $entity = $container->get($entityClass);
  78. /** @var IOperation $operation */
  79. $operation = $container->get($operationClass);
  80. $ruleMatcher->setEventName($eventName);
  81. $ruleMatcher->setEntity($entity);
  82. $ruleMatcher->setOperation($operation);
  83. $ctx = new LogContext();
  84. $ctx
  85. ->setOperation($operation)
  86. ->setEntity($entity)
  87. ->setEventName($eventName);
  88. /** @var Logger $flowLogger */
  89. $flowLogger = $container->get(Logger::class);
  90. $flowLogger->logEventInit($ctx);
  91. if ($event instanceof Event) {
  92. $entity->prepareRuleMatcher($ruleMatcher, $eventName, $event);
  93. $operation->onEvent($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 (ContainerExceptionInterface $e) {
  108. // Ignore query exceptions since they might occur when an entity/operation were set up before by an app that is disabled now
  109. }
  110. }
  111. );
  112. }, $eventNames ?? []);
  113. }
  114. }
  115. }
  116. }