OAuthCommonHttpUriUriFactory

Factory class for uniform resource indicators.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/Common/Http/Uri/UriFactory.php  
  1. class UriFactory implements UriFactoryInterface 
  2. /** 
  3. * Factory method to build a URI from a super-global $_SERVER array. 
  4. * @param array $_server 
  5. * @return UriInterface 
  6. */ 
  7. public function createFromSuperGlobalArray(array $_server) 
  8. if ($uri = $this->attemptProxyStyleParse($_server)) { 
  9. return $uri; 
  10.  
  11. $scheme = $this->detectScheme($_server); 
  12. $host = $this->detectHost($_server); 
  13. $port = $this->detectPort($_server); 
  14. $path = $this->detectPath($_server); 
  15. $query = $this->detectQuery($_server); 
  16.  
  17. return $this->createFromParts($scheme, '', $host, $port, $path, $query); 
  18.  
  19. /** 
  20. * @param string $absoluteUri 
  21. * @return UriInterface 
  22. */ 
  23. public function createFromAbsolute($absoluteUri) 
  24. return new Uri($absoluteUri); 
  25.  
  26. /** 
  27. * Factory method to build a URI from parts 
  28. * @param string $scheme 
  29. * @param string $userInfo 
  30. * @param string $host 
  31. * @param string $port 
  32. * @param string $path 
  33. * @param string $query 
  34. * @param string $fragment 
  35. * @return UriInterface 
  36. */ 
  37. public function createFromParts($scheme, $userInfo, $host, $port, $path = '', $query = '', $fragment = '') 
  38. $uri = new Uri(); 
  39. $uri->setScheme($scheme); 
  40. $uri->setUserInfo($userInfo); 
  41. $uri->setHost($host); 
  42. $uri->setPort($port); 
  43. $uri->setPath($path); 
  44. $uri->setQuery($query); 
  45. $uri->setFragment($fragment); 
  46.  
  47. return $uri; 
  48.  
  49. /** 
  50. * @param array $_server 
  51. * @return UriInterface|null 
  52. */ 
  53. private function attemptProxyStyleParse($_server) 
  54. // If the raw HTTP request message arrives with a proxy-style absolute URI in the 
  55. // initial request line, the absolute URI is stored in $_SERVER['REQUEST_URI'] and 
  56. // we only need to parse that. 
  57. if (isset($_server['REQUEST_URI']) && parse_url($_server['REQUEST_URI'], PHP_URL_SCHEME)) { 
  58. return new Uri($_server['REQUEST_URI']); 
  59.  
  60. return null; 
  61.  
  62. /** 
  63. * @param array $_server 
  64. * @return string 
  65. * @throws RuntimeException 
  66. */ 
  67. private function detectPath($_server) 
  68. if (isset($_server['REQUEST_URI'])) { 
  69. $uri = $_server['REQUEST_URI']; 
  70. } elseif (isset($_server['REDIRECT_URL'])) { 
  71. $uri = $_server['REDIRECT_URL']; 
  72. } else { 
  73. throw new RuntimeException('Could not detect URI path from superglobal'); 
  74.  
  75. $queryStr = strpos($uri, '?'); 
  76. if ($queryStr !== false) { 
  77. $uri = substr($uri, 0, $queryStr); 
  78.  
  79. return $uri; 
  80.  
  81. /** 
  82. * @param array $_server 
  83. * @return string 
  84. */ 
  85. private function detectHost(array $_server) 
  86. $host = isset($_server['HTTP_HOST']) ? $_server['HTTP_HOST'] : ''; 
  87.  
  88. if (strstr($host, ':')) { 
  89. $host = parse_url($host, PHP_URL_HOST); 
  90.  
  91. return $host; 
  92.  
  93. /** 
  94. * @param array $_server 
  95. * @return string 
  96. */ 
  97. private function detectPort(array $_server) 
  98. return isset($_server['SERVER_PORT']) ? $_server['SERVER_PORT'] : 80; 
  99.  
  100. /** 
  101. * @param array $_server 
  102. * @return string 
  103. */ 
  104. private function detectQuery(array $_server) 
  105. return isset($_server['QUERY_STRING']) ? $_server['QUERY_STRING'] : ''; 
  106.  
  107. /** 
  108. * Determine URI scheme component from superglobal array 
  109. * When using ISAPI with IIS, the value will be "off" if the request was 
  110. * not made through the HTTPS protocol. As a result, we filter the 
  111. * value to a bool. 
  112. * @param array $_server A super-global $_SERVER array 
  113. * @return string Returns http or https depending on the URI scheme 
  114. */ 
  115. private function detectScheme(array $_server) 
  116. if (isset($_server['HTTPS']) && filter_var($_server['HTTPS'], FILTER_VALIDATE_BOOLEAN)) { 
  117. return 'https'; 
  118. } else { 
  119. return 'http';