Middleware.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 OCP\AppFramework\Http\Response;
  28. /**
  29. * Middleware is used to provide hooks before or after controller methods and
  30. * deal with possible exceptions raised in the controller methods.
  31. * They're modeled after Django's middleware system:
  32. * https://docs.djangoproject.com/en/dev/topics/http/middleware/
  33. * @since 6.0.0
  34. */
  35. abstract class Middleware {
  36. /**
  37. * This is being run in normal order before the controller is being
  38. * called which allows several modifications and checks
  39. *
  40. * @param Controller $controller the controller that is being called
  41. * @param string $methodName the name of the method that will be called on
  42. * the controller
  43. * @return void
  44. * @since 6.0.0
  45. */
  46. public function beforeController($controller, $methodName) {
  47. }
  48. /**
  49. * This is being run when either the beforeController method or the
  50. * controller method itself is throwing an exception. The middleware is
  51. * asked in reverse order to handle the exception and to return a response.
  52. * If the response is null, it is assumed that the exception could not be
  53. * handled and the error will be thrown again
  54. *
  55. * @param Controller $controller the controller that is being called
  56. * @param string $methodName the name of the method that will be called on
  57. * the controller
  58. * @param \Exception $exception the thrown exception
  59. * @throws \Exception the passed in exception if it can't handle it
  60. * @return Response a Response object in case that the exception was handled
  61. * @since 6.0.0
  62. */
  63. public function afterException($controller, $methodName, \Exception $exception) {
  64. throw $exception;
  65. }
  66. /**
  67. * This is being run after a successful controllermethod call and allows
  68. * the manipulation of a Response object. The middleware is run in reverse order
  69. *
  70. * @param Controller $controller the controller that is being called
  71. * @param string $methodName the name of the method that will be called on
  72. * the controller
  73. * @param Response $response the generated response from the controller
  74. * @return Response a Response object
  75. * @since 6.0.0
  76. */
  77. public function afterController($controller, $methodName, Response $response) {
  78. return $response;
  79. }
  80. /**
  81. * This is being run after the response object has been rendered and
  82. * allows the manipulation of the output. The middleware is run in reverse order
  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 string $output the generated output from a response
  88. * @return string the output that should be printed
  89. * @since 6.0.0
  90. */
  91. public function beforeOutput($controller, $methodName, $output) {
  92. return $output;
  93. }
  94. }