PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/server/UriPath.class.php

http://floe.googlecode.com/
PHP | 233 lines | 123 code | 26 blank | 84 comment | 19 complexity | 93ebe20dbc60fc82d4ba6f48a792af8e MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /**
  3. * This file is part of Floe, a graceful PHP framework.
  4. * Copyright (C) 2005-2010 Mark Rickerby <http://maetl.net>
  5. *
  6. * See the LICENSE file distributed with this software for full copyright, disclaimer
  7. * of liability, and the specific limitations that govern the use of this software.
  8. *
  9. * $Id: UriPath.class.php 349 2010-02-12 02:16:09Z coretxt $
  10. * @package server
  11. */
  12. /**
  13. * Parses the incoming request URI and breaks it into useful chunks.
  14. *
  15. * @author maetl
  16. * @package server
  17. */
  18. class UriPath {
  19. private $_raw;
  20. private $_parsed;
  21. private $_segments;
  22. private $_parameters;
  23. private $_resource;
  24. private $_identity;
  25. private $_extension;
  26. private $_query;
  27. private $_aspect;
  28. function UriPath($path) {
  29. $this->_raw = trim($path);
  30. $this->_segments = array();
  31. $this->_parameters = array();
  32. $this->parse();
  33. }
  34. /** @private */
  35. private function parse() {
  36. $this->_parsed = parse_url($this->_raw);
  37. if (strlen($this->_raw) > 1) {
  38. if (isset($this->_parsed['query'])) {
  39. parse_str(strip_tags($this->_parsed['query']), $parameters);
  40. $this->_parameters = $parameters;
  41. }
  42. $path = $this->explodeSegmentPath($this->_parsed['path']);
  43. $this->addResource(array_pop($path));
  44. while ($segment = array_pop($path)) {
  45. $this->_segments[] = urldecode($segment);
  46. }
  47. $this->_segments = array_reverse($this->_segments);
  48. }
  49. }
  50. /** @ignore */
  51. private function explodeSegmentPath($path) {
  52. if (substr($path, -1) == '/') $path = substr($path, 0, -1);
  53. return explode('/', substr($path, 1));
  54. }
  55. /** @private */
  56. private function addResource($resource) {
  57. if (strstr($resource, ";")) {
  58. $aspect = explode(";", $resource);
  59. $this->_aspect = $aspect[1];
  60. $resource = $aspect[0];
  61. }
  62. if (strstr($resource, ".")) {
  63. $identity = explode(".", $resource);
  64. $this->_identity = $identity[0];
  65. $this->_extension = $identity[1];
  66. }
  67. $this->_resource = urldecode($resource);
  68. $this->_segments[] = urldecode($resource);
  69. }
  70. /**
  71. * <p>Gives the resource name part.</p>
  72. * <p>Eg: <code>resource.ext#fragment</code></p>
  73. */
  74. function resource() {
  75. if (isset($this->_parsed['fragment'])) {
  76. return $this->_resource . "#" . $this->_parsed['fragment'];
  77. } else {
  78. return $this->_resource;
  79. }
  80. }
  81. /**
  82. * <p>Gives the fragment identifier.</p>
  83. * <p>Eg: <code>#fragment</code></p>
  84. */
  85. function fragment() {
  86. if (isset($this->_parsed['fragment'])) {
  87. return $this->_parsed['fragment'];
  88. }
  89. }
  90. /**
  91. * <p>Gives the identity of the requested resource.</p>
  92. */
  93. function identity() {
  94. if (isset($this->_identity)) {
  95. return $this->_identity;
  96. } else {
  97. return $this->_resource;
  98. }
  99. }
  100. /**
  101. * Gives the file extension part of the requested resource.
  102. */
  103. function ext() {
  104. if (isset($this->_extension)) {
  105. return $this->_extension;
  106. }
  107. }
  108. /**
  109. * Alias for ext.
  110. */
  111. function extension() {
  112. return $this->ext();
  113. }
  114. /**
  115. * @todo implement multiple schemes
  116. */
  117. function scheme() {
  118. return 'http';
  119. }
  120. /**
  121. * Returns the HTTP host of this uri.
  122. */
  123. function host() {
  124. return $_SERVER['HTTP_HOST'];
  125. }
  126. function path() {
  127. return $this->_parsed['path'];
  128. }
  129. function isEmpty() {
  130. return (strlen($this->_resource) == 0);
  131. }
  132. /**
  133. * Returns the raw querystring.
  134. */
  135. function query() {
  136. if (isset($this->_parsed['query'])) {
  137. return $this->_parsed['query'];
  138. }
  139. }
  140. /**
  141. * Returns a hash of parameters from the querystring.
  142. */
  143. function parameters() {
  144. return $this->_parameters;
  145. }
  146. /**
  147. * Returns the value of given parameter.
  148. */
  149. function parameter($key) {
  150. if (isset($this->_parameters[$key])) return $this->_parameters[$key];
  151. }
  152. /**
  153. * Returns the given segment of the URL, starting from 1.
  154. *
  155. * Eg: /content/topic/id gives:
  156. * $uri->segment(1) => content
  157. * $uri->segment(2) => topic
  158. * $uri->segment(3) => id
  159. */
  160. function segment($index) {
  161. if (isset($this->_segments[$index])) return $this->_segments[$index];
  162. }
  163. /**
  164. * Returns the base segment of the URI path.
  165. *
  166. * Eg: /content/topic/id gives:
  167. * $uri->baseSegment => content
  168. */
  169. function baseSegment() {
  170. if (isset($this->_segments[0])) return $this->_segments[0];
  171. }
  172. /**
  173. * Returns a hash of path segments
  174. */
  175. function segments() {
  176. return $this->_segments;
  177. }
  178. /**
  179. * Returns number of segments in this url path.
  180. */
  181. function segmentsCount() {
  182. return count($this->_segments);
  183. }
  184. /**
  185. * Returns array of segments appearing after a given index.
  186. *
  187. * Eg: /content/topic/id gives:
  188. * $uri->segmentsFrom(0) => array("content", "topic", "id")
  189. */
  190. function segmentsFrom($index) {
  191. return array_slice($this->_segments, $index);
  192. }
  193. /**
  194. * Gives the aspect string of the requested URI
  195. * Eg: <code>/path/to/resource;aspect</code> returns <code>aspect</code>
  196. */
  197. function aspect() {
  198. if (isset($this->_aspect)) return $this->_aspect;
  199. }
  200. /**
  201. * Returns the full url.
  202. */
  203. function url() {
  204. return $this->host() . $this->_raw;
  205. }
  206. }
  207. ?>