1
0

Middleware.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  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. /**
  27. * Public interface of ownCloud for apps to use.
  28. * AppFramework\Middleware class
  29. */
  30. namespace OCP\AppFramework;
  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. }