Middleware.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\AppFramework;
  8. use Exception;
  9. use OCP\AppFramework\Http\Response;
  10. /**
  11. * Middleware is used to provide hooks before or after controller methods and
  12. * deal with possible exceptions raised in the controller methods.
  13. * They're modeled after Django's middleware system:
  14. * https://docs.djangoproject.com/en/dev/topics/http/middleware/
  15. * @since 6.0.0
  16. */
  17. abstract class Middleware {
  18. /**
  19. * This is being run in normal order before the controller is being
  20. * called which allows several modifications and checks
  21. *
  22. * @param Controller $controller the controller that is being called
  23. * @param string $methodName the name of the method that will be called on
  24. * the controller
  25. * @return void
  26. * @since 6.0.0
  27. */
  28. public function beforeController(Controller $controller, string $methodName) {
  29. }
  30. /**
  31. * This is being run when either the beforeController method or the
  32. * controller method itself is throwing an exception. The middleware is
  33. * asked in reverse order to handle the exception and to return a response.
  34. * If the response is null, it is assumed that the exception could not be
  35. * handled and the error will be thrown again
  36. *
  37. * @param Controller $controller the controller that is being called
  38. * @param string $methodName the name of the method that will be called on
  39. * the controller
  40. * @param Exception $exception the thrown exception
  41. * @throws Exception the passed in exception if it can't handle it
  42. * @return Response a Response object in case that the exception was handled
  43. * @since 6.0.0
  44. */
  45. public function afterException(Controller $controller, string $methodName, Exception $exception) {
  46. throw $exception;
  47. }
  48. /**
  49. * This is being run after a successful controllermethod call and allows
  50. * the manipulation of a Response object. The middleware is run in reverse order
  51. *
  52. * @param Controller $controller the controller that is being called
  53. * @param string $methodName the name of the method that will be called on
  54. * the controller
  55. * @param Response $response the generated response from the controller
  56. * @return Response a Response object
  57. * @since 6.0.0
  58. */
  59. public function afterController(Controller $controller, string $methodName, Response $response) {
  60. return $response;
  61. }
  62. /**
  63. * This is being run after the response object has been rendered and
  64. * allows the manipulation of the output. The middleware is run in reverse order
  65. *
  66. * @param Controller $controller the controller that is being called
  67. * @param string $methodName the name of the method that will be called on
  68. * the controller
  69. * @param string $output the generated output from a response
  70. * @return string the output that should be printed
  71. * @since 6.0.0
  72. */
  73. public function beforeOutput(Controller $controller, string $methodName, string $output) {
  74. return $output;
  75. }
  76. }