|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Request; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Manipulador de URLs |
|
7
|
|
|
*/ |
|
8
|
|
|
class Url |
|
9
|
|
|
{ |
|
10
|
|
|
const HOME = ['index', 'index']; |
|
11
|
|
|
const SUFFIX = '/'; |
|
12
|
|
|
|
|
13
|
|
|
static $base; |
|
14
|
|
|
static $path; |
|
15
|
|
|
static $protocol; |
|
16
|
|
|
static $segments; |
|
17
|
|
|
static $full; |
|
18
|
|
|
|
|
19
|
|
|
public static function init() |
|
20
|
|
|
{ |
|
21
|
|
|
static::$protocol = Input::protocol(); |
|
22
|
|
|
static::$base = static::getBase(); |
|
23
|
|
|
static::$path = static::getPath(); |
|
24
|
|
|
static::$segments = static::getSegments(); |
|
25
|
|
|
static::$full = static::$base . static::$path; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Retorna no formato de URL |
|
30
|
|
|
* @param string $url |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function format($url) |
|
34
|
|
|
{ |
|
35
|
|
|
return rtrim($url, static::SUFFIX) . static::SUFFIX; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Redireciona para a URL escolhida |
|
40
|
|
|
* @param string $url URL relativa ou absoluta |
|
41
|
|
|
* @codeCoverageIgnore |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function redirect($url = '') |
|
44
|
|
|
{ |
|
45
|
|
|
if (false === strpos($url, '://')) { |
|
46
|
|
|
$url = static::$base . $url; |
|
47
|
|
|
} |
|
48
|
|
|
header('location:' . $url); |
|
49
|
|
|
die(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retorna a URL base |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
private static function getBase() |
|
57
|
|
|
{ |
|
58
|
|
|
$host = Input::server('HTTP_HOST'); |
|
59
|
|
|
if ($host) { |
|
60
|
|
|
$script = Input::server('SCRIPT_NAME'); |
|
61
|
|
|
$basePath = preg_replace('@/+$@', '', dirname($script)); |
|
62
|
|
|
return static::$protocol . '://' . $host . $basePath . '/'; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Define o final da URL |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
private static function getPath() |
|
71
|
|
|
{ |
|
72
|
|
|
$host = Input::server('HTTP_HOST'); |
|
73
|
|
|
if ($host) { |
|
74
|
|
|
$requestUri = explode('?', Input::server('REQUEST_URI')); |
|
75
|
|
|
$context = explode($host, static::$base); |
|
76
|
|
|
$uri = (explode(end($context), $requestUri[0], 2)); |
|
77
|
|
|
return end($uri); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Define os fragmentos da URL |
|
83
|
|
|
* @return string[] |
|
84
|
|
|
*/ |
|
85
|
|
|
private static function getSegments() |
|
86
|
|
|
{ |
|
87
|
|
|
return array_filter(explode('/', static::$path)) + static::HOME; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|