1: <?php
2:
3: namespace PHPixie\HTTP\Messages\URI;
4:
5: 6: 7:
8: class SAPI extends \PHPixie\HTTP\Messages\URI
9: {
10: 11: 12:
13: protected $server;
14:
15: 16: 17: 18:
19: public function __construct($server)
20: {
21: $this->server = $server;
22: }
23:
24: 25: 26:
27: protected function requirePart($name)
28: {
29: switch($name) {
30: case 'scheme':
31: $this->requireScheme();
32: break;
33:
34: case 'host':
35: case 'port':
36: $this->requireHostAndPort();
37: break;
38:
39: case 'path':
40: $this->requirePath();
41: break;
42:
43: case 'query':
44: $this->requireQuery();
45: break;
46:
47: default:
48: parent::requirePart($name);
49: }
50: }
51:
52: 53: 54:
55: protected function requireScheme()
56: {
57: $scheme = 'http';
58: if(isset($this->server['HTTPS']) && $this->server['HTTPS'] !== 'off') {
59: $scheme = 'https';
60: }
61:
62: $this->parts['scheme'] = $scheme;
63: }
64:
65: 66: 67:
68: protected function requirePath()
69: {
70: $path = $this->server['REQUEST_URI'];
71: $split = strpos($path, '?');
72:
73: if ($split !== false) {
74: $path = substr($path, 0, $split);
75: }
76:
77: $this->parts['path'] = $this->normalizePath($path);
78: }
79:
80: 81: 82:
83: protected function requireQuery()
84: {
85: $query = ltrim($this->server['QUERY_STRING'], '?');
86: $this->parts['query'] = $this->normalizeQuery($query);
87: }
88:
89: 90: 91:
92: protected function requireHostAndPort()
93: {
94: $host = $this->server['HTTP_HOST'];
95: $split = strrpos($host, ':');
96:
97: if($split !== false) {
98: $port = (int) substr($host, $split+1);
99: $host = substr($host, 0, $split);
100:
101: }else{
102: $port = null;
103: }
104:
105: if(!array_key_exists('host', $this->parts))
106: {
107: $this->parts['host'] = $host;
108: }
109:
110: if(!array_key_exists('port', $this->parts))
111: {
112: $this->parts['port'] = $port;
113: }
114: }
115:
116:
117: }