middleware.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Public interface of ownCloud for apps to use.
  24. * AppFramework\Middleware class
  25. */
  26. namespace OCP\AppFramework;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\Response;
  29. /**
  30. * Middleware is used to provide hooks before or after controller methods and
  31. * deal with possible exceptions raised in the controller methods.
  32. * They're modeled after Django's middleware system:
  33. * https://docs.djangoproject.com/en/dev/topics/http/middleware/
  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. */
  44. public function beforeController($controller, $methodName){
  45. }
  46. /**
  47. * This is being run when either the beforeController method or the
  48. * controller method itself is throwing an exception. The middleware is
  49. * asked in reverse order to handle the exception and to return a response.
  50. * If the response is null, it is assumed that the exception could not be
  51. * handled and the error will be thrown again
  52. *
  53. * @param Controller $controller the controller that is being called
  54. * @param string $methodName the name of the method that will be called on
  55. * the controller
  56. * @param \Exception $exception the thrown exception
  57. * @throws \Exception the passed in exception if it cant handle it
  58. * @return Response a Response object in case that the exception was handled
  59. */
  60. public function afterException($controller, $methodName, \Exception $exception){
  61. throw $exception;
  62. }
  63. /**
  64. * This is being run after a successful controllermethod call and allows
  65. * the manipulation of a Response object. The middleware is run in reverse order
  66. *
  67. * @param Controller $controller the controller that is being called
  68. * @param string $methodName the name of the method that will be called on
  69. * the controller
  70. * @param Response $response the generated response from the controller
  71. * @return Response a Response object
  72. */
  73. public function afterController($controller, $methodName, Response $response){
  74. return $response;
  75. }
  76. /**
  77. * This is being run after the response object has been rendered and
  78. * allows the manipulation of the output. The middleware is run in reverse order
  79. *
  80. * @param Controller $controller the controller that is being called
  81. * @param string $methodName the name of the method that will be called on
  82. * the controller
  83. * @param string $output the generated output from a response
  84. * @return string the output that should be printed
  85. */
  86. public function beforeOutput($controller, $methodName, $output){
  87. return $output;
  88. }
  89. }