middlewaredispatcher.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\AppFramework\Middleware;
  23. use OCP\AppFramework\Controller;
  24. use OCP\AppFramework\Http\Response;
  25. use OCP\AppFramework\MiddleWare;
  26. /**
  27. * This class is used to store and run all the middleware in correct order
  28. */
  29. class MiddlewareDispatcher {
  30. /**
  31. * @var array array containing all the middlewares
  32. */
  33. private $middlewares;
  34. /**
  35. * @var int counter which tells us what middlware was executed once an
  36. * exception occurs
  37. */
  38. private $middlewareCounter;
  39. /**
  40. * Constructor
  41. */
  42. public function __construct(){
  43. $this->middlewares = array();
  44. $this->middlewareCounter = 0;
  45. }
  46. /**
  47. * Adds a new middleware
  48. * @param Middleware $middleware the middleware which will be added
  49. */
  50. public function registerMiddleware(Middleware $middleWare){
  51. array_push($this->middlewares, $middleWare);
  52. }
  53. /**
  54. * returns an array with all middleware elements
  55. * @return array the middlewares
  56. */
  57. public function getMiddlewares(){
  58. return $this->middlewares;
  59. }
  60. /**
  61. * This is being run in normal order before the controller is being
  62. * called which allows several modifications and checks
  63. *
  64. * @param Controller $controller the controller that is being called
  65. * @param string $methodName the name of the method that will be called on
  66. * the controller
  67. */
  68. public function beforeController(Controller $controller, $methodName){
  69. // we need to count so that we know which middlewares we have to ask in
  70. // case theres an exception
  71. for($i=0; $i<count($this->middlewares); $i++){
  72. $this->middlewareCounter++;
  73. $middleware = $this->middlewares[$i];
  74. $middleware->beforeController($controller, $methodName);
  75. }
  76. }
  77. /**
  78. * This is being run when either the beforeController method or the
  79. * controller method itself is throwing an exception. The middleware is asked
  80. * in reverse order to handle the exception and to return a response.
  81. * If the response is null, it is assumed that the exception could not be
  82. * handled and the error will be thrown again
  83. *
  84. * @param Controller $controller the controller that is being called
  85. * @param string $methodName the name of the method that will be called on
  86. * the controller
  87. * @param \Exception $exception the thrown exception
  88. * @return Response a Response object if the middleware can handle the
  89. * exception
  90. * @throws \Exception the passed in exception if it cant handle it
  91. */
  92. public function afterException(Controller $controller, $methodName, \Exception $exception){
  93. for($i=$this->middlewareCounter-1; $i>=0; $i--){
  94. $middleware = $this->middlewares[$i];
  95. try {
  96. return $middleware->afterException($controller, $methodName, $exception);
  97. } catch(\Exception $exception){
  98. continue;
  99. }
  100. }
  101. throw $exception;
  102. }
  103. /**
  104. * This is being run after a successful controllermethod call and allows
  105. * the manipulation of a Response object. The middleware is run in reverse order
  106. *
  107. * @param Controller $controller the controller that is being called
  108. * @param string $methodName the name of the method that will be called on
  109. * the controller
  110. * @param Response $response the generated response from the controller
  111. * @return Response a Response object
  112. */
  113. public function afterController(Controller $controller, $methodName, Response $response){
  114. for($i=count($this->middlewares)-1; $i>=0; $i--){
  115. $middleware = $this->middlewares[$i];
  116. $response = $middleware->afterController($controller, $methodName, $response);
  117. }
  118. return $response;
  119. }
  120. /**
  121. * This is being run after the response object has been rendered and
  122. * allows the manipulation of the output. The middleware is run in reverse order
  123. *
  124. * @param Controller $controller the controller that is being called
  125. * @param string $methodName the name of the method that will be called on
  126. * the controller
  127. * @param string $output the generated output from a response
  128. * @return string the output that should be printed
  129. */
  130. public function beforeOutput(Controller $controller, $methodName, $output){
  131. for($i=count($this->middlewares)-1; $i>=0; $i--){
  132. $middleware = $this->middlewares[$i];
  133. $output = $middleware->beforeOutput($controller, $methodName, $output);
  134. }
  135. return $output;
  136. }
  137. }