Middleware.php 3.7 KB

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 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. * @since 6.0.0
  44. */
  45. public function beforeController($controller, $methodName) {
  46. }
  47. /**
  48. * This is being run when either the beforeController method or the
  49. * controller method itself is throwing an exception. The middleware is
  50. * asked in reverse order to handle the exception and to return a response.
  51. * If the response is null, it is assumed that the exception could not be
  52. * handled and the error will be thrown again
  53. *
  54. * @param Controller $controller the controller that is being called
  55. * @param string $methodName the name of the method that will be called on
  56. * the controller
  57. * @param \Exception $exception the thrown exception
  58. * @throws \Exception the passed in exception if it can't handle it
  59. * @return Response a Response object in case that the exception was handled
  60. * @since 6.0.0
  61. */
  62. public function afterException($controller, $methodName, \Exception $exception) {
  63. throw $exception;
  64. }
  65. /**
  66. * This is being run after a successful controllermethod call and allows
  67. * the manipulation of a Response object. The middleware is run in reverse order
  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. * @param Response $response the generated response from the controller
  73. * @return Response a Response object
  74. * @since 6.0.0
  75. */
  76. public function afterController($controller, $methodName, Response $response) {
  77. return $response;
  78. }
  79. /**
  80. * This is being run after the response object has been rendered and
  81. * allows the manipulation of the output. The middleware is run in reverse order
  82. *
  83. * @param Controller $controller the controller that is being called
  84. * @param string $methodName the name of the method that will be called on
  85. * the controller
  86. * @param string $output the generated output from a response
  87. * @return string the output that should be printed
  88. * @since 6.0.0
  89. */
  90. public function beforeOutput($controller, $methodName, $output) {
  91. return $output;
  92. }
  93. }