Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <vincent@nextcloud.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. namespace OCP\AppFramework;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\AppFramework\Http\Response;
  33. use OCP\IRequest;
  34. /**
  35. * Base class to inherit your controllers from
  36. * @since 6.0.0
  37. */
  38. abstract class Controller {
  39. /**
  40. * app name
  41. * @var string
  42. * @since 7.0.0
  43. */
  44. protected $appName;
  45. /**
  46. * current request
  47. * @var \OCP\IRequest
  48. * @since 6.0.0
  49. */
  50. protected $request;
  51. /**
  52. * @var array
  53. * @since 7.0.0
  54. */
  55. private $responders;
  56. /**
  57. * constructor of the controller
  58. * @param string $appName the name of the app
  59. * @param IRequest $request an instance of the request
  60. * @since 6.0.0 - parameter $appName was added in 7.0.0 - parameter $app was removed in 7.0.0
  61. */
  62. public function __construct($appName,
  63. IRequest $request) {
  64. $this->appName = $appName;
  65. $this->request = $request;
  66. // default responders
  67. $this->responders = [
  68. 'json' => function ($data) {
  69. if ($data instanceof DataResponse) {
  70. $response = new JSONResponse(
  71. $data->getData(),
  72. $data->getStatus()
  73. );
  74. $dataHeaders = $data->getHeaders();
  75. $headers = $response->getHeaders();
  76. // do not overwrite Content-Type if it already exists
  77. if (isset($dataHeaders['Content-Type'])) {
  78. unset($headers['Content-Type']);
  79. }
  80. $response->setHeaders(array_merge($dataHeaders, $headers));
  81. if ($data->getETag() !== null) {
  82. $response->setETag($data->getETag());
  83. }
  84. if ($data->getLastModified() !== null) {
  85. $response->setLastModified($data->getLastModified());
  86. }
  87. if ($data->isThrottled()) {
  88. $response->throttle($data->getThrottleMetadata());
  89. }
  90. return $response;
  91. }
  92. return new JSONResponse($data);
  93. }
  94. ];
  95. }
  96. /**
  97. * Parses an HTTP accept header and returns the supported responder type
  98. * @param string $acceptHeader
  99. * @param string $default
  100. * @return string the responder type
  101. * @since 7.0.0
  102. * @since 9.1.0 Added default parameter
  103. */
  104. public function getResponderByHTTPHeader($acceptHeader, $default = 'json') {
  105. $headers = explode(',', $acceptHeader);
  106. // return the first matching responder
  107. foreach ($headers as $header) {
  108. $header = strtolower(trim($header));
  109. $responder = str_replace('application/', '', $header);
  110. if (array_key_exists($responder, $this->responders)) {
  111. return $responder;
  112. }
  113. }
  114. // no matching header return default
  115. return $default;
  116. }
  117. /**
  118. * Registers a formatter for a type
  119. * @param string $format
  120. * @param \Closure $responder
  121. * @since 7.0.0
  122. */
  123. protected function registerResponder($format, \Closure $responder) {
  124. $this->responders[$format] = $responder;
  125. }
  126. /**
  127. * Serializes and formats a response
  128. * @param mixed $response the value that was returned from a controller and
  129. * is not a Response instance
  130. * @param string $format the format for which a formatter has been registered
  131. * @throws \DomainException if format does not match a registered formatter
  132. * @return Response
  133. * @since 7.0.0
  134. */
  135. public function buildResponse($response, $format = 'json') {
  136. if (array_key_exists($format, $this->responders)) {
  137. $responder = $this->responders[$format];
  138. return $responder($response);
  139. }
  140. throw new \DomainException('No responder registered for format '.
  141. $format . '!');
  142. }
  143. }