Skip to content

Commit 3374f9a

Browse files
committed
Tidied Request by extracting URI parsing. Added ApplicationInterface (this should have already been there).
1 parent e75dd60 commit 3374f9a

2 files changed

Lines changed: 78 additions & 16 deletions

File tree

src/Darya/Http/Request.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace Darya\Service;
3+
4+
use Darya\Service\ContainerInterface;
5+
use Darya\Service\ProviderInterface;
6+
7+
/**
8+
* Darya's application interface.
9+
*
10+
* A service container that registers and boots service providers.
11+
*
12+
* @author Chris Andrew <chris@hexus.io>
13+
*/
14+
interface ApplicationInterface extends ContainerInterface {
15+
16+
/**
17+
* Register a service provider with the application.
18+
*
19+
* @param \Darya\Service\ProviderInterface $provider
20+
*/
21+
public function provide(ProviderInterface $provider);
22+
23+
/**
24+
* Boot all registered service providers.
25+
*/
26+
public function boot();
27+
28+
}

0 commit comments

Comments
 (0)