1
0

Application.php 4.4 KB

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