1
0

MiddlewareDispatcher.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Thomas Tanghus <thomas@tanghus.net>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\AppFramework\Middleware;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\Response;
  33. use OCP\AppFramework\Middleware;
  34. /**
  35. * This class is used to store and run all the middleware in correct order
  36. */
  37. class MiddlewareDispatcher {
  38. /**
  39. * @var Middleware[] array containing all the middlewares
  40. */
  41. private array $middlewares;
  42. /**
  43. * @var int counter which tells us what middleware was executed once an
  44. * exception occurs
  45. */
  46. private int $middlewareCounter;
  47. /**
  48. * Constructor
  49. */
  50. public function __construct() {
  51. $this->middlewares = [];
  52. $this->middlewareCounter = 0;
  53. }
  54. /**
  55. * Adds a new middleware
  56. * @param Middleware $middleWare the middleware which will be added
  57. */
  58. public function registerMiddleware(Middleware $middleWare): void {
  59. $this->middlewares[] = $middleWare;
  60. }
  61. /**
  62. * returns an array with all middleware elements
  63. * @return Middleware[] the middlewares
  64. */
  65. public function getMiddlewares(): array {
  66. return $this->middlewares;
  67. }
  68. /**
  69. * This is being run in normal order before the controller is being
  70. * called which allows several modifications and checks
  71. *
  72. * @param Controller $controller the controller that is being called
  73. * @param string $methodName the name of the method that will be called on
  74. * the controller
  75. */
  76. public function beforeController(Controller $controller, string $methodName): void {
  77. // we need to count so that we know which middlewares we have to ask in
  78. // case there is an exception
  79. $middlewareCount = \count($this->middlewares);
  80. for ($i = 0; $i < $middlewareCount; $i++) {
  81. $this->middlewareCounter++;
  82. $middleware = $this->middlewares[$i];
  83. $middleware->beforeController($controller, $methodName);
  84. }
  85. }
  86. /**
  87. * This is being run when either the beforeController method or the
  88. * controller method itself is throwing an exception. The middleware is asked
  89. * in reverse order to handle the exception and to return a response.
  90. * If the response is null, it is assumed that the exception could not be
  91. * handled and the error will be thrown again
  92. *
  93. * @param Controller $controller the controller that is being called
  94. * @param string $methodName the name of the method that will be called on
  95. * the controller
  96. * @param \Exception $exception the thrown exception
  97. * @return Response a Response object if the middleware can handle the
  98. * exception
  99. * @throws \Exception the passed in exception if it can't handle it
  100. */
  101. public function afterException(Controller $controller, string $methodName, \Exception $exception): Response {
  102. for ($i = $this->middlewareCounter - 1; $i >= 0; $i--) {
  103. $middleware = $this->middlewares[$i];
  104. try {
  105. return $middleware->afterException($controller, $methodName, $exception);
  106. } catch (\Exception $exception) {
  107. continue;
  108. }
  109. }
  110. throw $exception;
  111. }
  112. /**
  113. * This is being run after a successful controllermethod call and allows
  114. * the manipulation of a Response object. The middleware is run in reverse order
  115. *
  116. * @param Controller $controller the controller that is being called
  117. * @param string $methodName the name of the method that will be called on
  118. * the controller
  119. * @param Response $response the generated response from the controller
  120. * @return Response a Response object
  121. */
  122. public function afterController(Controller $controller, string $methodName, Response $response): Response {
  123. for ($i = \count($this->middlewares) - 1; $i >= 0; $i--) {
  124. $middleware = $this->middlewares[$i];
  125. $response = $middleware->afterController($controller, $methodName, $response);
  126. }
  127. return $response;
  128. }
  129. /**
  130. * This is being run after the response object has been rendered and
  131. * allows the manipulation of the output. The middleware is run in reverse order
  132. *
  133. * @param Controller $controller the controller that is being called
  134. * @param string $methodName the name of the method that will be called on
  135. * the controller
  136. * @param string $output the generated output from a response
  137. * @return string the output that should be printed
  138. */
  139. public function beforeOutput(Controller $controller, string $methodName, string $output): string {
  140. for ($i = \count($this->middlewares) - 1; $i >= 0; $i--) {
  141. $middleware = $this->middlewares[$i];
  142. $output = $middleware->beforeOutput($controller, $methodName, $output);
  143. }
  144. return $output;
  145. }
  146. }