/administrator/components/com_widgetkit/vendor/yootheme/framework/src/Routing/Request.php

https://gitlab.com/vnsoftdev/amms · PHP · 177 lines · 69 code · 24 blank · 84 comment · 3 complexity · 53a6f48ec5571962488be2089e156dd4 MD5 · raw file

  1. <?php
  2. namespace YOOtheme\Framework\Routing;
  3. class Request
  4. {
  5. /**
  6. * @var ParameterBag
  7. */
  8. public $attributes;
  9. /**
  10. * @var ParameterBag
  11. */
  12. public $request;
  13. /**
  14. * @var ParameterBag
  15. */
  16. public $server;
  17. /**
  18. * @var ParameterBag
  19. */
  20. public $cookies;
  21. /**
  22. * @var HeaderBag
  23. */
  24. public $headers;
  25. /**
  26. * @var string
  27. */
  28. protected $baseUrl;
  29. /**
  30. * @var string
  31. */
  32. protected $basePath;
  33. /**
  34. * @var string
  35. */
  36. protected $baseRoute;
  37. /**
  38. * Constructor.
  39. *
  40. * @param string $baseUrl
  41. * @param string $basePath
  42. * @param string $baseRoute
  43. * @param array $request
  44. * @param array $server
  45. */
  46. public function __construct($baseUrl, $basePath, $baseRoute, array $request = array(), array $server = array(), array $cookies = array())
  47. {
  48. $this->baseUrl = $baseUrl;
  49. $this->basePath = $basePath;
  50. $this->baseRoute = $baseRoute;
  51. $this->attributes = new ParameterBag;
  52. $this->request = new ParameterBag($request ?: $_REQUEST);
  53. $this->cookies = new ParameterBag($cookies ?: $_COOKIE);
  54. $this->server = new ServerBag($server ?: $_SERVER);
  55. $this->headers = new HeaderBag($this->server->getHeaders());
  56. // decode json content type
  57. if (stripos($this->headers->get('CONTENT_TYPE'), 'application/json') !== false) {
  58. if ($json = json_decode(@file_get_contents('php://input'), true)) {
  59. $this->request->add($json);
  60. }
  61. }
  62. }
  63. /**
  64. * Gets the base URL.
  65. *
  66. * @return string
  67. */
  68. public function getBaseUrl()
  69. {
  70. return $this->baseUrl;
  71. }
  72. /**
  73. * Sets the base URL.
  74. *
  75. * @param string $baseUrl
  76. * @return self
  77. */
  78. public function setBaseUrl($baseUrl)
  79. {
  80. $this->baseUrl = $baseUrl;
  81. return $this;
  82. }
  83. /**
  84. * Gets the base path.
  85. *
  86. * @return string
  87. */
  88. public function getBasePath()
  89. {
  90. return $this->basePath;
  91. }
  92. /**
  93. * Sets the base path.
  94. *
  95. * @param string $basePath
  96. * @return self
  97. */
  98. public function setBasePath($basePath)
  99. {
  100. $this->basePath = $basePath;
  101. return $this;
  102. }
  103. /**
  104. * Gets the base route.
  105. *
  106. * @return string
  107. */
  108. public function getBaseRoute()
  109. {
  110. return $this->baseRoute;
  111. }
  112. /**
  113. * Sets the base route.
  114. *
  115. * @param string $baseRoute
  116. * @return self
  117. */
  118. public function setBaseRoute($baseRoute)
  119. {
  120. $this->baseRoute = $baseRoute;
  121. return $this;
  122. }
  123. /**
  124. * Gets the HTTP method.
  125. *
  126. * @return string
  127. */
  128. public function getMethod()
  129. {
  130. $method = $this->server->get('REQUEST_METHOD', 'GET');
  131. $method = $this->headers->get('X-HTTP-Method-Override', $method);
  132. return strtoupper($method);
  133. }
  134. /**
  135. * Checks if this is an XHR request.
  136. *
  137. * @return bool
  138. */
  139. public function isXhr()
  140. {
  141. return $this->server->get('X_REQUESTED_WITH') == 'XMLHttpRequest';
  142. }
  143. /**
  144. * Proxy method calls to request parameter bag.
  145. *
  146. * @param string $method
  147. * @param array $args
  148. * @return mixed
  149. */
  150. public function __call($method, $args) {
  151. return call_user_func_array(array($this->request, $method), $args);
  152. }
  153. }