|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Pimf; |
|
9
|
|
|
|
|
10
|
|
|
use Pimf\Util\Character as Str; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* URI |
|
14
|
|
|
* |
|
15
|
|
|
* @package Pimf |
|
16
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class Uri |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $pathInfo; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private static $requestUri; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The URI for the current request. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public static $uri; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The URI segments for the current request. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
public static $segments = array(); |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $pathInfo |
|
47
|
|
|
* @param string $requestUri |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function setup($pathInfo, $requestUri) |
|
50
|
|
|
{ |
|
51
|
|
|
self::$pathInfo = $pathInfo; |
|
52
|
|
|
self::$requestUri = $requestUri; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the full URI including the query string. |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function full() |
|
61
|
|
|
{ |
|
62
|
|
|
return self::$requestUri; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the URI for the current request. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function current() |
|
71
|
|
|
{ |
|
72
|
|
|
if (!is_null(static::$uri)) { |
|
73
|
|
|
return static::$uri; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//Format a given URI. |
|
77
|
|
|
$uri = trim(self::$pathInfo, '/') ?: '/'; |
|
78
|
|
|
|
|
79
|
|
|
//Set the URI segments for the request. |
|
80
|
|
|
$segments = explode('/', trim($uri, '/')); |
|
81
|
|
|
static::$segments = array_diff($segments, array('')); |
|
82
|
|
|
|
|
83
|
|
|
return static::$uri = $uri; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Determine if the current URI matches a given pattern. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $pattern |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function is($pattern) |
|
94
|
|
|
{ |
|
95
|
|
|
return Str::is($pattern, static::current()); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|