scratch – Blame information for rev 126
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
126 | office | 1 | <?php |
2 | /** |
||
3 | * |
||
4 | * This file is part of the Aura project for PHP. |
||
5 | * |
||
6 | * @package Aura.Uri |
||
7 | * |
||
8 | * @license http://opensource.org/licenses/bsd-license.php BSD |
||
9 | * |
||
10 | */ |
||
11 | namespace Aura\Uri\Url; |
||
12 | |||
13 | use Aura\Uri\Url; |
||
14 | use Aura\Uri\Path; |
||
15 | use Aura\Uri\Query; |
||
16 | use Aura\Uri\Host; |
||
17 | use Aura\Uri\PublicSuffixList; |
||
18 | |||
19 | /** |
||
20 | * |
||
21 | * Factory to create new Url objects. |
||
22 | * |
||
23 | * @package Aura.Uri |
||
24 | * |
||
25 | */ |
||
26 | class Factory |
||
27 | { |
||
28 | /** |
||
29 | * |
||
30 | * A string representing the current URL, built from $_SERVER. |
||
31 | * |
||
32 | * @var string |
||
33 | * |
||
34 | */ |
||
35 | protected $current; |
||
36 | |||
37 | /** |
||
38 | * Public suffix list |
||
39 | * |
||
40 | * @var PublicSuffixList |
||
41 | */ |
||
42 | protected $psl; |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * Constructor. |
||
47 | * |
||
48 | * @param array $server An array copy of $_SERVER. |
||
49 | * |
||
50 | * @param PublicSuffixList $psl Public suffix list |
||
51 | */ |
||
52 | public function __construct(array $server, PublicSuffixList $psl) |
||
53 | { |
||
54 | $https = isset($server['HTTPS']) |
||
55 | && strtolower($server['HTTPS']) == 'on'; |
||
56 | |||
57 | $ssl = isset($server['SERVER_PORT']) |
||
58 | && $server['SERVER_PORT'] == 443; |
||
59 | |||
60 | if ($https || $ssl) { |
||
61 | $scheme = 'https'; |
||
62 | } else { |
||
63 | $scheme = 'http'; |
||
64 | } |
||
65 | |||
66 | if (isset($server['HTTP_HOST'])) { |
||
67 | $host = $server['HTTP_HOST']; |
||
68 | } else { |
||
69 | $host = ''; |
||
70 | } |
||
71 | |||
72 | if (isset($server['REQUEST_URI'])) { |
||
73 | $resource = $server['REQUEST_URI']; |
||
74 | } else { |
||
75 | $resource = ''; |
||
76 | } |
||
77 | |||
78 | $this->current = $scheme . '://' . $host . $resource; |
||
79 | |||
80 | $this->psl = $psl; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * |
||
85 | * Creates and returns a new Url object. |
||
86 | * |
||
87 | * If no host is specified, the parsing will fail. |
||
88 | * |
||
89 | * @param string $spec The URL string to set from. |
||
90 | * |
||
91 | * @return Url |
||
92 | * |
||
93 | */ |
||
94 | public function newInstance($spec) |
||
95 | { |
||
96 | $elem = [ |
||
97 | 'scheme' => null, |
||
98 | 'user' => null, |
||
99 | 'pass' => null, |
||
100 | 'host' => null, |
||
101 | 'port' => null, |
||
102 | 'path' => null, |
||
103 | 'query' => null, |
||
104 | 'fragment' => null, |
||
105 | ]; |
||
106 | |||
107 | $parts = $this->parse($spec); |
||
108 | |||
109 | $elem = (array) $parts + $elem; |
||
110 | |||
111 | $path = new Path([]); |
||
112 | $path->setFromString($elem['path']); |
||
113 | |||
114 | $query = new Query([]); |
||
115 | $query->setFromString($elem['query']); |
||
116 | |||
117 | $host = new Host($this->psl, []); |
||
118 | $host->setFromString($elem['host']); |
||
119 | |||
120 | return new Url( |
||
121 | $elem['scheme'], |
||
122 | $elem['user'], |
||
123 | $elem['pass'], |
||
124 | $host, |
||
125 | $elem['port'], |
||
126 | $path, |
||
127 | $query, |
||
128 | $elem['fragment'] |
||
129 | ); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * |
||
134 | * Creates and returns a new URL object based on the current URL. |
||
135 | * |
||
136 | * @return Url |
||
137 | * |
||
138 | */ |
||
139 | public function newCurrent() |
||
140 | { |
||
141 | return $this->newInstance($this->current); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Parses url |
||
146 | * |
||
147 | * @param string $spec Url to parse |
||
148 | * @return array Parsed url |
||
149 | */ |
||
150 | public function parse($spec) |
||
151 | { |
||
152 | preg_match(Url::SCHEME_PATTERN, $spec, $schemeMatches); |
||
153 | |||
154 | if (empty($schemeMatches)) { |
||
155 | $spec = 'http://' . preg_replace('#^//#', '', $spec, 1); |
||
156 | } |
||
157 | |||
158 | return parse_url($spec); |
||
159 | } |
||
160 | } |