Skip to content

Commit a694f0e

Browse files
author
Milad Rahimi
committed
some cleaning
1 parent ede208a commit a694f0e

File tree

4 files changed

+97
-91
lines changed

4 files changed

+97
-91
lines changed

composer.json

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
11
{
2-
"name": "miladrahimi/phprouter",
3-
"description": "PhpRouter is a powerful and standalone HTTP URL router for PHP projects.",
4-
"keywords": ["Router", "URL Router", "HTTP Router", "Routing", "URL", "HTTP", "Route"],
5-
"homepage": "https://github.com/miladrahimi/phprouter",
6-
"type": "library",
7-
"license": "MIT",
8-
"authors": [
9-
{
10-
"name": "Milad Rahimi",
11-
"email": "[email protected]",
12-
"homepage": "https://miladrahimi.com",
13-
"role": "Developer"
14-
}
15-
],
16-
"support": {
17-
"email": "[email protected]",
18-
"source": "https://github.com/miladrahimi/phprouter/issues"
19-
},
20-
"require": {
21-
"php": ">=7.1",
22-
"ext-json": "*",
23-
"zendframework/zend-diactoros": "^2.1"
24-
},
25-
"require-dev": {
26-
"phpunit/phpunit": "^7"
27-
},
28-
"autoload": {
29-
"psr-4": {
30-
"MiladRahimi\\PhpRouter\\": "src/",
31-
"MiladRahimi\\PhpRouter\\Tests\\": "tests/"
32-
}
2+
"name": "miladrahimi/phprouter",
3+
"description": "PhpRouter is a powerful and standalone HTTP URL router for PHP projects.",
4+
"keywords": [
5+
"Router",
6+
"URL Router",
7+
"HTTP Router",
8+
"Routing",
9+
"URL",
10+
"HTTP",
11+
"Route"
12+
],
13+
"homepage": "https://github.com/miladrahimi/phprouter",
14+
"type": "library",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Milad Rahimi",
19+
"email": "[email protected]",
20+
"homepage": "https://miladrahimi.com",
21+
"role": "Developer"
3322
}
23+
],
24+
"support": {
25+
"email": "[email protected]",
26+
"source": "https://github.com/miladrahimi/phprouter/issues"
27+
},
28+
"require": {
29+
"php": ">=7.1",
30+
"ext-json": "*",
31+
"ext-mbstring": "*",
32+
"zendframework/zend-diactoros": "^2.1"
33+
},
34+
"require-dev": {
35+
"phpunit/phpunit": "^7"
36+
},
37+
"autoload": {
38+
"psr-4": {
39+
"MiladRahimi\\PhpRouter\\": "src/",
40+
"MiladRahimi\\PhpRouter\\Tests\\": "tests/"
41+
}
42+
}
3443
}

examples/simple/index.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use MiladRahimi\PhpRouter\Router;
4+
use Psr\Http\Message\RequestInterface;
5+
use Zend\Diactoros\Response\HtmlResponse;
6+
use Zend\Diactoros\Response\TextResponse;
7+
8+
require('../../vendor/autoload.php');
9+
10+
$router = new Router();
11+
12+
$router->get('/', function () {
13+
return new HtmlResponse('<p>Homepage!</p>');
14+
});
15+
16+
17+
$router->get('/search', function (\Zend\Diactoros\ServerRequest $request) {
18+
return new TextResponse(json_encode($request->getAttribute()));
19+
});
20+
21+
$router->dispatch();

src/Router.php

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: Milad Rahimi <[email protected]>
5-
* Date: 6/5/2018 AD
6-
* Time: 12:23
7-
*/
82

93
namespace MiladRahimi\PhpRouter;
104

@@ -43,6 +37,21 @@ class Router
4337
*/
4438
private $parameters = [];
4539

40+
/**
41+
* @var ServerRequestInterface
42+
*/
43+
private $request;
44+
45+
/**
46+
* @var ResponseInterface
47+
*/
48+
private $response;
49+
50+
/**
51+
* @var PublisherInterface
52+
*/
53+
private $publisher;
54+
4655
/**
4756
* @var string|null
4857
*/
@@ -73,28 +82,16 @@ class Router
7382
*/
7483
private $currentRouteName = null;
7584

76-
/**
77-
* @var ServerRequestInterface
78-
*/
79-
private $serverRequest;
80-
81-
/**
82-
* @var ResponseInterface
83-
*/
84-
private $response;
85-
86-
/**
87-
* @var PublisherInterface
88-
*/
89-
private $publisher;
90-
9185
/**
9286
* Router constructor.
87+
*
88+
* @param PublisherInterface|null $publisher
9389
*/
94-
public function __construct()
90+
public function __construct(PublisherInterface $publisher = null)
9591
{
96-
$this->initializeRequestAndResponse();
97-
$this->publisher = new Publisher();
92+
$this->publisher = $publisher ?: new Publisher();
93+
$this->response = new Response();
94+
$this->request = ServerRequestFactory::fromGlobals();
9895
}
9996

10097
/**
@@ -103,15 +100,15 @@ public function __construct()
103100
* @param array $attributes
104101
* @param Closure $routes
105102
*/
106-
public function group($attributes = [], Closure $routes)
103+
public function group(array $attributes, Closure $routes)
107104
{
108105
// Backup previous group properties
106+
$oldName = $this->currentName;
109107
$oldMiddleware = $this->currentMiddleware;
110108
$oldPrefix = $this->currentPrefix;
111109
$oldDomain = $this->currentDomain;
112-
$oldName = $this->currentName;
113110

114-
// There shouldn't be any name!
111+
// Empty current name for next steps
115112
$this->currentName = null;
116113

117114
// Set given middleware for the group
@@ -195,10 +192,10 @@ public function map(
195192
*/
196193
public function dispatch()
197194
{
198-
$method = $this->serverRequest->getMethod();
199-
$scheme = $this->serverRequest->getUri()->getScheme();
200-
$domain = substr($this->serverRequest->getUri()->getHost(), strlen($scheme . '://'));
201-
$uri = $this->serverRequest->getUri()->getPath();
195+
$method = $this->request->getMethod();
196+
$scheme = $this->request->getUri()->getScheme();
197+
$domain = substr($this->request->getUri()->getHost(), strlen($scheme . '://'));
198+
$uri = $this->request->getUri()->getPath();
202199

203200
$routes = array_merge($this->routes[$method] ?? [], $this->routes['*'] ?? []);
204201

@@ -250,27 +247,6 @@ private function run(array $routeAttributes, array $routeParameters)
250247
}
251248
}
252249

253-
/**
254-
* Initialize http request and response
255-
*/
256-
private function initializeRequestAndResponse()
257-
{
258-
$this->response = new Response();
259-
$this->serverRequest = ServerRequestFactory::fromGlobals();
260-
261-
foreach (array_merge($_GET ?? [], $_POST ?? []) as $name => $value) {
262-
$this->serverRequest = $this->serverRequest->withAttribute($name, $value);
263-
}
264-
265-
if (is_array($bodyParameters = json_decode(file_get_contents('php://input'), true))) {
266-
$this->serverRequest = $this->serverRequest->withParsedBody($bodyParameters);
267-
268-
foreach ($bodyParameters as $name => $value) {
269-
$this->serverRequest = $this->serverRequest->withAttribute($name, $value);
270-
}
271-
}
272-
}
273-
274250
/**
275251
* Run the controller through the middleware
276252
*
@@ -298,7 +274,7 @@ private function runControllerThroughMiddleware(array $middleware, Closure $cont
298274
$middleware[$i] = new $middleware[$i];
299275
}
300276

301-
return $middleware[$i]->handle($this->serverRequest, $next);
277+
return $middleware[$i]->handle($this->request, $next);
302278
}
303279

304280
/**
@@ -387,7 +363,7 @@ function (ReflectionParameter $parameter) use ($parameters) {
387363
($parameter->getType() && $parameter->getType()->getName() == ServerRequestInterface::class) ||
388364
$parameter->getName() == 'request'
389365
) {
390-
return $this->serverRequest;
366+
return $this->request;
391367
}
392368

393369
if (
@@ -675,19 +651,19 @@ public function publish($httpResponse)
675651
*
676652
* @return ServerRequestInterface
677653
*/
678-
public function getServerRequest(): ServerRequestInterface
654+
public function getRequest(): ServerRequestInterface
679655
{
680-
return $this->serverRequest;
656+
return $this->request;
681657
}
682658

683659
/**
684660
* Set my own http request instance
685661
*
686-
* @param ServerRequestInterface $serverRequest
662+
* @param ServerRequestInterface $request
687663
*/
688-
public function setServerRequest(ServerRequestInterface $serverRequest)
664+
public function setRequest(ServerRequestInterface $request)
689665
{
690-
$this->serverRequest = $serverRequest;
666+
$this->request = $request;
691667
}
692668

693669
/**

tests/RoutingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function test_multiple_routes()
115115
$this->mockRequest(HttpMethods::POST, 'http://example.com/666');
116116

117117
// Reset the server request to consider the new request
118-
$router->setServerRequest(ServerRequestFactory::fromGlobals());
118+
$router->setRequest(ServerRequestFactory::fromGlobals());
119119

120120
$router->dispatch();
121121

0 commit comments

Comments
 (0)