Runtime.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Http;
  36. /**
  37. * Class \Hoa\Http\Runtime.
  38. *
  39. * Runtime informations.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. class Runtime
  45. {
  46. /**
  47. * Get HTTP method.
  48. *
  49. * @return string
  50. */
  51. public static function getMethod()
  52. {
  53. if ('cli' === php_sapi_name()) {
  54. return 'get';
  55. }
  56. return strtolower($_SERVER['REQUEST_METHOD']);
  57. }
  58. /**
  59. * Get URI.
  60. *
  61. * @return string
  62. */
  63. public static function getUri()
  64. {
  65. return $_SERVER['REQUEST_URI'];
  66. }
  67. /**
  68. * Get data.
  69. *
  70. * @param bool $extended Whether we want a larger support of
  71. * content-type for example.
  72. * @return mixed
  73. */
  74. public static function getData($extended = true)
  75. {
  76. switch (static::getMethod()) {
  77. case Request::METHOD_GET:
  78. return $_GET;
  79. case Request::METHOD_POST:
  80. $contentType = static::getHeader('Content-Type');
  81. switch ($contentType) {
  82. case 'application/x-www-form-urlencoded':
  83. return $_POST;
  84. case 'application/json':
  85. $input = file_get_contents('php://input');
  86. if (true !== $extended ||
  87. true !== function_exists('json_decode')) {
  88. return $input;
  89. }
  90. $json = json_decode($input, true);
  91. if (JSON_ERROR_NONE !== json_last_error()) {
  92. return $input;
  93. }
  94. return $json;
  95. default:
  96. return file_get_contents('php://input');
  97. }
  98. break;
  99. case Request::METHOD_PUT:
  100. case Request::METHOD_PATCH:
  101. return file_get_contents('php://input');
  102. default:
  103. return null;
  104. }
  105. }
  106. /**
  107. * Whether there is data or not.
  108. *
  109. * @return bool
  110. */
  111. public static function hasData()
  112. {
  113. if (Request::METHOD_GET === static::getMethod()) {
  114. return !empty($_GET);
  115. }
  116. return 0 < intval(static::getHeader('Content-Length'));
  117. }
  118. /**
  119. * Get all headers.
  120. *
  121. * @return array
  122. */
  123. public static function getHeaders()
  124. {
  125. static $_headers = [];
  126. if (!empty($_headers)) {
  127. return $_headers;
  128. }
  129. if (true === function_exists('apache_request_headers')) {
  130. foreach (apache_request_headers() as $header => $value) {
  131. $_headers[strtolower($header)] = $value;
  132. }
  133. } else {
  134. if (isset($_SERVER['CONTENT_TYPE'])) {
  135. $_headers['content-type'] = $_SERVER['CONTENT_TYPE'];
  136. }
  137. if (isset($_SERVER['CONTENT_LENGTH'])) {
  138. $_headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
  139. }
  140. foreach ($_SERVER as $key => $value) {
  141. if ('HTTP_' === substr($key, 0, 5)) {
  142. $_headers[strtolower(str_replace('_', '-', substr($key, 5)))]
  143. = $value;
  144. }
  145. }
  146. }
  147. return $_headers;
  148. }
  149. /**
  150. * Get a specific header.
  151. *
  152. * @param string $header Header name.
  153. * @return string
  154. */
  155. public static function getHeader($header)
  156. {
  157. $headers = static::getHeaders();
  158. $header = strtolower($header);
  159. if (true !== array_key_exists($header, $headers)) {
  160. return null;
  161. }
  162. return $headers[$header];
  163. }
  164. }