MiddlewareDispatcher.php 5.2 KB

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