You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.3 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Donquixote <marjunebatac@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Tanghus <thomas@tanghus.net>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. /**
  30. * Public interface of ownCloud for apps to use.
  31. * AppFramework\Controller class
  32. */
  33. namespace OCP\AppFramework;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\Http\JSONResponse;
  36. use OCP\AppFramework\Http\Response;
  37. use OCP\IRequest;
  38. /**
  39. * Base class to inherit your controllers from
  40. * @since 6.0.0
  41. */
  42. abstract class Controller {
  43. /**
  44. * app name
  45. * @var string
  46. * @since 7.0.0
  47. */
  48. protected $appName;
  49. /**
  50. * current request
  51. * @var \OCP\IRequest
  52. * @since 6.0.0
  53. */
  54. protected $request;
  55. /**
  56. * @var array
  57. * @since 7.0.0
  58. */
  59. private $responders;
  60. /**
  61. * constructor of the controller
  62. * @param string $appName the name of the app
  63. * @param IRequest $request an instance of the request
  64. * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0
  65. */
  66. public function __construct($appName,
  67. IRequest $request) {
  68. $this->appName = $appName;
  69. $this->request = $request;
  70. // default responders
  71. $this->responders = [
  72. 'json' => function ($data) {
  73. if ($data instanceof DataResponse) {
  74. $response = new JSONResponse(
  75. $data->getData(),
  76. $data->getStatus()
  77. );
  78. $dataHeaders = $data->getHeaders();
  79. $headers = $response->getHeaders();
  80. // do not overwrite Content-Type if it already exists
  81. if (isset($dataHeaders['Content-Type'])) {
  82. unset($headers['Content-Type']);
  83. }
  84. $response->setHeaders(array_merge($dataHeaders, $headers));
  85. if ($data->getETag() !== null) {
  86. $response->setETag($data->getETag());
  87. }
  88. if ($data->getLastModified() !== null) {
  89. $response->setLastModified($data->getLastModified());
  90. }
  91. return $response;
  92. }
  93. return new JSONResponse($data);
  94. }
  95. ];
  96. }
  97. /**
  98. * Parses an HTTP accept header and returns the supported responder type
  99. * @param string $acceptHeader
  100. * @param string $default
  101. * @return string the responder type
  102. * @since 7.0.0
  103. * @since 9.1.0 Added default parameter
  104. */
  105. public function getResponderByHTTPHeader($acceptHeader, $default='json') {
  106. $headers = explode(',', $acceptHeader);
  107. // return the first matching responder
  108. foreach ($headers as $header) {
  109. $header = strtolower(trim($header));
  110. $responder = str_replace('application/', '', $header);
  111. if (array_key_exists($responder, $this->responders)) {
  112. return $responder;
  113. }
  114. }
  115. // no matching header return default
  116. return $default;
  117. }
  118. /**
  119. * Registers a formatter for a type
  120. * @param string $format
  121. * @param \Closure $responder
  122. * @since 7.0.0
  123. */
  124. protected function registerResponder($format, \Closure $responder) {
  125. $this->responders[$format] = $responder;
  126. }
  127. /**
  128. * Serializes and formats a response
  129. * @param mixed $response the value that was returned from a controller and
  130. * is not a Response instance
  131. * @param string $format the format for which a formatter has been registered
  132. * @throws \DomainException if format does not match a registered formatter
  133. * @return Response
  134. * @since 7.0.0
  135. */
  136. public function buildResponse($response, $format='json') {
  137. if (array_key_exists($format, $this->responders)) {
  138. $responder = $this->responders[$format];
  139. return $responder($response);
  140. }
  141. throw new \DomainException('No responder registered for format '.
  142. $format . '!');
  143. }
  144. }