1
0

Controller.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Donquixote <marjunebatac@gmail.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  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 = array(
  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. return $response;
  86. }
  87. return new JSONResponse($data);
  88. }
  89. );
  90. }
  91. /**
  92. * Parses an HTTP accept header and returns the supported responder type
  93. * @param string $acceptHeader
  94. * @param string $default
  95. * @return string the responder type
  96. * @since 7.0.0
  97. * @since 9.1.0 Added default parameter
  98. */
  99. public function getResponderByHTTPHeader($acceptHeader, $default='json') {
  100. $headers = explode(',', $acceptHeader);
  101. // return the first matching responder
  102. foreach ($headers as $header) {
  103. $header = strtolower(trim($header));
  104. $responder = str_replace('application/', '', $header);
  105. if (array_key_exists($responder, $this->responders)) {
  106. return $responder;
  107. }
  108. }
  109. // no matching header return default
  110. return $default;
  111. }
  112. /**
  113. * Registers a formatter for a type
  114. * @param string $format
  115. * @param \Closure $responder
  116. * @since 7.0.0
  117. */
  118. protected function registerResponder($format, \Closure $responder) {
  119. $this->responders[$format] = $responder;
  120. }
  121. /**
  122. * Serializes and formats a response
  123. * @param mixed $response the value that was returned from a controller and
  124. * is not a Response instance
  125. * @param string $format the format for which a formatter has been registered
  126. * @throws \DomainException if format does not match a registered formatter
  127. * @return Response
  128. * @since 7.0.0
  129. */
  130. public function buildResponse($response, $format='json') {
  131. if(array_key_exists($format, $this->responders)) {
  132. $responder = $this->responders[$format];
  133. return $responder($response);
  134. }
  135. throw new \DomainException('No responder registered for format '.
  136. $format . '!');
  137. }
  138. }