@@ -79,11 +79,11 @@ public static function createFromGlobals(SessionInterface $session = null) {
7979 $ method = isset ($ _SERVER ['REQUEST_METHOD ' ]) ? strtolower ($ _SERVER ['REQUEST_METHOD ' ]) : 'get ' ;
8080
8181 $ request = new static ($ uri , $ method , array (
82- 'get ' => $ _GET ?: array () ,
83- 'post ' => $ _POST ?: array () ,
84- 'cookie ' => $ _COOKIE ?: array () ,
85- 'file ' => $ _FILES ?: array () ,
86- 'server ' => $ _SERVER ?: array () ,
82+ 'get ' => $ _GET ,
83+ 'post ' => $ _POST ,
84+ 'cookie ' => $ _COOKIE ,
85+ 'file ' => $ _FILES ,
86+ 'server ' => $ _SERVER ,
8787 'header ' => static ::headersFromGlobals ($ _SERVER )
8888 ));
8989
@@ -116,17 +116,51 @@ public static function headersFromGlobals(array $server) {
116116 }
117117
118118 /**
119- * Create a new request.
119+ * Parse the given URI and return its components.
120120 *
121- * Expects $data to have keys such as 'get', 'post', 'cookie', 'file',
122- * 'server', 'header'.
121+ * Any components not satisfied will be null instead of non-existent, so you
122+ * can safely expect the keys 'scheme', 'host', 'port', 'user', 'pass',
123+ * 'query' and 'fragment' to exist.
124+ *
125+ * @param string $uri
126+ * @return array
127+ */
128+ protected static function parseUri ($ uri ) {
129+ return array_merge (array (
130+ 'scheme ' => null ,
131+ 'host ' => null ,
132+ 'port ' => null ,
133+ 'user ' => null ,
134+ 'pass ' => null ,
135+ 'query ' => null ,
136+ 'fragment ' => null
137+ ), parse_url ($ uri ));
138+ }
139+
140+ /**
141+ * Parse the given query and return its key value pairs.
142+ *
143+ * @param string $query
144+ * @return array
145+ */
146+ protected static function parseQuery ($ query ) {
147+ $ values = array ();
148+ parse_str ($ query , $ values );
149+
150+ return $ values ;
151+ }
152+
153+ /**
154+ * Create a new request.
155+ *
156+ * Expects $data to have keys such as 'get', 'post', 'cookie', 'file',
157+ * 'server', 'header' and the same general structure as PHP superglobals.
123158 *
124159 * @param string $uri
125160 * @param string $method
126161 * @param array $data
127162 */
128163 public function __construct ($ uri , $ method = 'get ' , $ data = array ()) {
129-
130164 foreach ($ data as $ type => $ values ) {
131165 $ type = strtolower ($ type );
132166
@@ -137,13 +171,13 @@ public function __construct($uri, $method = 'get', $data = array()) {
137171 $ this ->data [$ type ] = $ values ;
138172 }
139173
140- $ components = parse_url ($ uri );
141- $ path = isset ( $ components ['path ' ]) ? $ components [ ' path ' ] : '' ;
174+ $ components = static :: parseUri ($ uri );
175+ $ path = $ components ['path ' ];
142176
143- if ( isset ( $ components [ ' query ' ])) {
144- parse_str ( $ components [ ' query ' ], $ get );
145- $ this -> data [ ' get ' ] = array_merge ( $ this -> data [ ' get ' ], $ get );
146- }
177+ $ this -> data [ ' get ' ] = array_merge (
178+ $ this -> data [ ' get ' ],
179+ static :: parseQuery ( $ components [ ' query ' ])
180+ );
147181
148182 $ this ->uri = $ uri ;
149183 $ this ->path = $ path ;
@@ -155,7 +189,7 @@ public function __construct($uri, $method = 'get', $data = array()) {
155189 }
156190
157191 /**
158- * Retrieve request data of the given type using the given key.
192+ * Retrieve request data of the given type using the given key.
159193 *
160194 * If no key is set, all request data of the given type will be returned. If
161195 * neither are set, all request data will be returned.
0 commit comments