Middleware.php 3.8 KB

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