You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Middleware.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Stefan Weil <sw@weilnetz.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Thomas Tanghus <thomas@tanghus.net>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCP\AppFramework;
  27. use Exception;
  28. use OCP\AppFramework\Http\Response;
  29. /**
  30. * Middleware is used to provide hooks before or after controller methods and
  31. * deal with possible exceptions raised in the controller methods.
  32. * They're modeled after Django's middleware system:
  33. * https://docs.djangoproject.com/en/dev/topics/http/middleware/
  34. * @since 6.0.0
  35. */
  36. abstract class Middleware {
  37. /**
  38. * This is being run in normal order before the controller is being
  39. * called which allows several modifications and checks
  40. *
  41. * @param Controller $controller the controller that is being called
  42. * @param string $methodName the name of the method that will be called on
  43. * the controller
  44. * @return void
  45. * @since 6.0.0
  46. */
  47. public function beforeController(Controller $controller, string $methodName) {
  48. }
  49. /**
  50. * This is being run when either the beforeController method or the
  51. * controller method itself is throwing an exception. The middleware is
  52. * asked in reverse order to handle the exception and to return a response.
  53. * If the response is null, it is assumed that the exception could not be
  54. * handled and the error will be thrown again
  55. *
  56. * @param Controller $controller the controller that is being called
  57. * @param string $methodName the name of the method that will be called on
  58. * the controller
  59. * @param Exception $exception the thrown exception
  60. * @throws Exception the passed in exception if it can't handle it
  61. * @return Response a Response object in case that the exception was handled
  62. * @since 6.0.0
  63. */
  64. public function afterException(Controller $controller, string $methodName, Exception $exception) {
  65. throw $exception;
  66. }
  67. /**
  68. * This is being run after a successful controllermethod call and allows
  69. * the manipulation of a Response object. The middleware is run in reverse order
  70. *
  71. * @param Controller $controller the controller that is being called
  72. * @param string $methodName the name of the method that will be called on
  73. * the controller
  74. * @param Response $response the generated response from the controller
  75. * @return Response a Response object
  76. * @since 6.0.0
  77. */
  78. public function afterController(Controller $controller, string $methodName, Response $response) {
  79. return $response;
  80. }
  81. /**
  82. * This is being run after the response object has been rendered and
  83. * allows the manipulation of the output. The middleware is run in reverse order
  84. *
  85. * @param Controller $controller the controller that is being called
  86. * @param string $methodName the name of the method that will be called on
  87. * the controller
  88. * @param string $output the generated output from a response
  89. * @return string the output that should be printed
  90. * @since 6.0.0
  91. */
  92. public function beforeOutput(Controller $controller, string $methodName, string $output) {
  93. return $output;
  94. }
  95. }