Initial Kohana install
[speedfreak] / Server / system / libraries / URI.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * URI library.
4  *
5  * $Id: URI.php 4072 2009-03-13 17:20:38Z jheathco $
6  *
7  * @package    Core
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class URI_Core extends Router {
13
14         /**
15          * Returns a singleton instance of URI.
16          *
17          * @return  object
18          */
19         public static function instance()
20         {
21                 static $instance;
22
23                 if ($instance == NULL)
24                 {
25                         // Initialize the URI instance
26                         $instance = new URI;
27                 }
28
29                 return $instance;
30         }
31
32         /**
33          * Retrieve a specific URI segment.
34          *
35          * @param   integer|string  segment number or label
36          * @param   mixed           default value returned if segment does not exist
37          * @return  string
38          */
39         public function segment($index = 1, $default = FALSE)
40         {
41                 if (is_string($index))
42                 {
43                         if (($key = array_search($index, URI::$segments)) === FALSE)
44                                 return $default;
45
46                         $index = $key + 2;
47                 }
48
49                 $index = (int) $index - 1;
50
51                 return isset(URI::$segments[$index]) ? URI::$segments[$index] : $default;
52         }
53
54         /**
55          * Retrieve a specific routed URI segment.
56          *
57          * @param   integer|string  rsegment number or label
58          * @param   mixed           default value returned if segment does not exist
59          * @return  string
60          */
61         public function rsegment($index = 1, $default = FALSE)
62         {
63                 if (is_string($index))
64                 {
65                         if (($key = array_search($index, URI::$rsegments)) === FALSE)
66                                 return $default;
67
68                         $index = $key + 2;
69                 }
70
71                 $index = (int) $index - 1;
72
73                 return isset(URI::$rsegments[$index]) ? URI::$rsegments[$index] : $default;
74         }
75
76         /**
77          * Retrieve a specific URI argument.
78          * This is the part of the segments that does not indicate controller or method
79          *
80          * @param   integer|string  argument number or label
81          * @param   mixed           default value returned if segment does not exist
82          * @return  string
83          */
84         public function argument($index = 1, $default = FALSE)
85         {
86                 if (is_string($index))
87                 {
88                         if (($key = array_search($index, URI::$arguments)) === FALSE)
89                                 return $default;
90
91                         $index = $key + 2;
92                 }
93
94                 $index = (int) $index - 1;
95
96                 return isset(URI::$arguments[$index]) ? URI::$arguments[$index] : $default;
97         }
98
99         /**
100          * Returns an array containing all the URI segments.
101          *
102          * @param   integer  segment offset
103          * @param   boolean  return an associative array
104          * @return  array
105          */
106         public function segment_array($offset = 0, $associative = FALSE)
107         {
108                 return $this->build_array(URI::$segments, $offset, $associative);
109         }
110
111         /**
112          * Returns an array containing all the re-routed URI segments.
113          *
114          * @param   integer  rsegment offset
115          * @param   boolean  return an associative array
116          * @return  array
117          */
118         public function rsegment_array($offset = 0, $associative = FALSE)
119         {
120                 return $this->build_array(URI::$rsegments, $offset, $associative);
121         }
122
123         /**
124          * Returns an array containing all the URI arguments.
125          *
126          * @param   integer  segment offset
127          * @param   boolean  return an associative array
128          * @return  array
129          */
130         public function argument_array($offset = 0, $associative = FALSE)
131         {
132                 return $this->build_array(URI::$arguments, $offset, $associative);
133         }
134
135         /**
136          * Creates a simple or associative array from an array and an offset.
137          * Used as a helper for (r)segment_array and argument_array.
138          *
139          * @param   array    array to rebuild
140          * @param   integer  offset to start from
141          * @param   boolean  create an associative array
142          * @return  array
143          */
144         public function build_array($array, $offset = 0, $associative = FALSE)
145         {
146                 // Prevent the keys from being improperly indexed
147                 array_unshift($array, 0);
148
149                 // Slice the array, preserving the keys
150                 $array = array_slice($array, $offset + 1, count($array) - 1, TRUE);
151
152                 if ($associative === FALSE)
153                         return $array;
154
155                 $associative = array();
156                 $pairs       = array_chunk($array, 2);
157
158                 foreach ($pairs as $pair)
159                 {
160                         // Add the key/value pair to the associative array
161                         $associative[$pair[0]] = isset($pair[1]) ? $pair[1] : '';
162                 }
163
164                 return $associative;
165         }
166
167         /**
168          * Returns the complete URI as a string.
169          *
170          * @return  string
171          */
172         public function string()
173         {
174                 return URI::$current_uri;
175         }
176
177         /**
178          * Magic method for converting an object to a string.
179          *
180          * @return  string
181          */
182         public function __toString()
183         {
184                 return URI::$current_uri;
185         }
186
187         /**
188          * Returns the total number of URI segments.
189          *
190          * @return  integer
191          */
192         public function total_segments()
193         {
194                 return count(URI::$segments);
195         }
196
197         /**
198          * Returns the total number of re-routed URI segments.
199          *
200          * @return  integer
201          */
202         public function total_rsegments()
203         {
204                 return count(URI::$rsegments);
205         }
206
207         /**
208          * Returns the total number of URI arguments.
209          *
210          * @return  integer
211          */
212         public function total_arguments()
213         {
214                 return count(URI::$arguments);
215         }
216
217         /**
218          * Returns the last URI segment.
219          *
220          * @param   mixed   default value returned if segment does not exist
221          * @return  string
222          */
223         public function last_segment($default = FALSE)
224         {
225                 if (($end = $this->total_segments()) < 1)
226                         return $default;
227
228                 return URI::$segments[$end - 1];
229         }
230
231         /**
232          * Returns the last re-routed URI segment.
233          *
234          * @param   mixed   default value returned if segment does not exist
235          * @return  string
236          */
237         public function last_rsegment($default = FALSE)
238         {
239                 if (($end = $this->total_segments()) < 1)
240                         return $default;
241
242                 return URI::$rsegments[$end - 1];
243         }
244
245         /**
246          * Returns the path to the current controller (not including the actual
247          * controller), as a web path.
248          *
249          * @param   boolean  return a full url, or only the path specifically
250          * @return  string
251          */
252         public function controller_path($full = TRUE)
253         {
254                 return ($full) ? url::site(URI::$controller_path) : URI::$controller_path;
255         }
256
257         /**
258          * Returns the current controller, as a web path.
259          *
260          * @param   boolean  return a full url, or only the controller specifically
261          * @return  string
262          */
263         public function controller($full = TRUE)
264         {
265                 return ($full) ? url::site(URI::$controller_path.URI::$controller) : URI::$controller;
266         }
267
268         /**
269          * Returns the current method, as a web path.
270          *
271          * @param   boolean  return a full url, or only the method specifically
272          * @return  string
273          */
274         public function method($full = TRUE)
275         {
276                 return ($full) ? url::site(URI::$controller_path.URI::$controller.'/'.URI::$method) : URI::$method;
277         }
278
279 } // End URI Class