Skip to content

Commit 7132d2f

Browse files
committed
Use unique pattern for Request and Router making an HTTP request be unique during the pipe process.
1 parent f33f885 commit 7132d2f

File tree

6 files changed

+151
-44
lines changed

6 files changed

+151
-44
lines changed

src/WaterPipe/HTTP/Request/Request.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636

3737
class Request
3838
{
39+
/**
40+
* @var Request
41+
*/
42+
private static $_instance = null;
43+
3944
/**
4045
* @var int
4146
*/
@@ -56,11 +61,26 @@ class Request
5661
*/
5762
public $uri;
5863

59-
public function __construct()
64+
/**
65+
* Request constructor.
66+
*/
67+
private function __construct()
6068
{
6169
$this->uri = new RequestUri();
6270
}
6371

72+
/**
73+
* @return Request
74+
*/
75+
public static function &getInstance(): Request
76+
{
77+
static $instance;
78+
79+
$instance[0] = self::$_instance === null ? self::$_instance = new Request() : self::$_instance;
80+
81+
return $instance[0];
82+
}
83+
6484
/**
6585
* @return int
6686
*/
@@ -114,10 +134,8 @@ public function setBody(RequestData $body): void
114134
*
115135
* @return Request
116136
*/
117-
public static function capture(): Request
137+
public static function &capture(): Request
118138
{
119-
$router = new Router();
120-
121-
return $router->build()->getRequest();
139+
return Router::getInstance()->build()->getRequest();
122140
}
123141
}

src/WaterPipe/Routing/Middleware/Middleware.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ abstract class Middleware
4444
/**
4545
* RouteAction constructor.
4646
*
47-
* @param Request $request The request associated to this route.
48-
*
4947
* @throws \Exception
5048
*/
51-
public function __construct(Request $request)
49+
public function __construct()
5250
{
53-
$this->_request = $request;
51+
$this->_request =& Request::getInstance();
5452
}
5553

5654
public abstract function beforeExecute();

src/WaterPipe/Routing/Route.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/**
4+
* WaterPipe - URL routing framework for PHP
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
* @category Library
25+
* @package WaterPipe
26+
* @author Axel Nana <[email protected]>
27+
* @copyright 2018 Aliens Group, Inc.
28+
* @license MIT <https://github.com/ElementaryFramework/WaterPipe/blob/master/LICENSE>
29+
* @version 0.0.1
30+
* @link http://waterpipe.na2axl.tk
31+
*/
32+
33+
namespace ElementaryFramework\WaterPipe\Routing;
34+
35+
use ElementaryFramework\WaterPipe\HTTP\Request\Request;
36+
use ElementaryFramework\WaterPipe\HTTP\Response\Response;
37+
38+
abstract class Route
39+
{
40+
/**
41+
* @var string
42+
*/
43+
protected $_uri = null;
44+
45+
/**
46+
* @var Request
47+
*/
48+
protected $_request;
49+
50+
/**
51+
* @var Response
52+
*/
53+
protected $_response;
54+
55+
/**
56+
* Route constructor.
57+
*
58+
* @throws \Exception
59+
*/
60+
public function __construct()
61+
{
62+
$this->_request =& Request::capture();
63+
$this->_response = new Response();
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function getUri(): string
70+
{
71+
return $this->_uri;
72+
}
73+
74+
public abstract function request();
75+
76+
public abstract function get();
77+
78+
public abstract function post();
79+
80+
public abstract function put();
81+
82+
public abstract function delete();
83+
}

src/WaterPipe/Routing/RouteAction.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ abstract class RouteAction
5050
/**
5151
* RouteAction constructor.
5252
*
53-
* @param Request $request The request associated to this route.
54-
*
5553
* @throws \Exception
5654
*/
57-
public function __construct(Request $request)
55+
public function __construct()
5856
{
59-
$this->_request = $request;
57+
$this->_request =& Request::getInstance();
6058
$this->_response = new Response();
6159
}
6260

src/WaterPipe/Routing/Router.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,28 @@
3939

4040
class Router
4141
{
42+
/**
43+
* @var Router
44+
*/
45+
private static $_instance = null;
46+
4247
/**
4348
* @var Request
4449
*/
4550
private $_request;
4651

47-
public function __construct()
52+
private function __construct()
53+
{
54+
$this->_request =& Request::getInstance();
55+
}
56+
57+
public static function &getInstance()
4858
{
49-
$this->_request = new Request();
59+
static $instance;
60+
61+
$instance[0] = self::$_instance === null ? self::$_instance = new Router() : self::$_instance;
62+
63+
return $instance[0];
5064
}
5165

5266
public function build()
@@ -131,7 +145,7 @@ private function _detectMethod()
131145
/**
132146
* @return Request
133147
*/
134-
public function getRequest(): Request
148+
public function &getRequest(): Request
135149
{
136150
return $this->_request;
137151
}

src/WaterPipe/WaterPipe.php

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use ElementaryFramework\WaterPipe\HTTP\Request\RequestUri;
3838
use ElementaryFramework\WaterPipe\HTTP\Response\Response;
3939
use ElementaryFramework\WaterPipe\Routing\Middleware\Middleware;
40+
use ElementaryFramework\WaterPipe\Routing\Route;
4041
use ElementaryFramework\WaterPipe\Routing\RouteAction;
4142

4243
class WaterPipe
@@ -76,9 +77,15 @@ public function __construct()
7677
$this->_errorsRegistry = array();
7778
}
7879

79-
public function use(Middleware $middleware)
80+
public function use($plugin)
8081
{
81-
array_push($this->_middlewareRegistry, $middleware);
82+
if ($plugin instanceof Middleware) {
83+
array_push($this->_middlewareRegistry, $plugin);
84+
} elseif ($plugin instanceof Route) {
85+
foreach (array("request", "get", "post", "put", "delete") as $method) {
86+
call_user_func_array(array($this, $method), array($plugin->getUri(), array($plugin, $method)));
87+
}
88+
}
8289
}
8390

8491
public function request(string $uri, $action)
@@ -116,33 +123,17 @@ public function delete(string $uri, $action)
116123
* @throws \Exception
117124
*/
118125
public function run()
119-
{
120-
$this->exec(Request::capture());
121-
}
122-
123-
/**
124-
* Execute a request with the current water pipe.
125-
*
126-
* This method have to be called AFTER the
127-
* definition of all routes. No routes will
128-
* be considered after the call of this method.
129-
*
130-
* @param Request $request The request to execute.
131-
*
132-
* @throws \Exception
133-
*/
134-
public function exec(Request $request)
135126
{
136127
$this->_isRunning = true;
137128

138-
$this->_executeRequest($request);
129+
$this->_executeRequest();
139130
}
140131

141-
private function _executeRequest(Request $request)
132+
private function _executeRequest()
142133
{
143134
$registry = null;
144135

145-
switch ($request->getMethod()) {
136+
switch (Request::getInstance()->getMethod()) {
146137
case RequestMethod::UNKNOWN:
147138
// TODO: 500 Internal Server Error. Unable to determine the request method.
148139
throw new \Exception("500 Error");
@@ -168,10 +159,10 @@ private function _executeRequest(Request $request)
168159
throw new \Exception("Cannot handle a request of this type");
169160
}
170161

171-
$runner = $this->_getActionForRoutes($registry, $request);
162+
$runner = $this->_getActionForRoutes($registry);
172163

173164
if ($runner === null) {
174-
$runner = $this->_getActionForRoutes($this->_requestRegistry, $request);
165+
$runner = $this->_getActionForRoutes($this->_requestRegistry);
175166
}
176167

177168
if ($runner === null) {
@@ -181,24 +172,29 @@ private function _executeRequest(Request $request)
181172

182173
// NOTE: No code will normally not be executed after this block...
183174
if (is_callable($runner) || is_array($runner)) {
184-
call_user_func_array($runner, array($request, new Response()));
175+
call_user_func_array($runner, array(Request::getInstance(), new Response()));
185176
} elseif (is_subclass_of($runner, RouteAction::class)) {
186177
if (is_string($runner)) {
187-
$runner = new $runner($request);
178+
$runner = new $runner;
188179
}
189180
$runner->execute();
190181
} else {
191182
throw new \Exception("Malformed route action");
192183
}
193184
}
194185

195-
private function _getActionForRoutes(array $routes, Request $request)
186+
/**
187+
* @param array $routes
188+
* @return callable
189+
* @throws Exceptions\RequestUriBuilderException
190+
*/
191+
private function _getActionForRoutes(array $routes)
196192
{
197193
$runner = null;
198194

199195
foreach ($routes as $pattern => $action) {
200-
if (RequestUri::isMatch($pattern, $request->uri->getUri())) {
201-
$request->uri->setPattern($pattern)->build();
196+
if (RequestUri::isMatch($pattern, Request::getInstance()->uri->getUri())) {
197+
Request::getInstance()->uri->setPattern($pattern)->build();
202198
$runner = $action;
203199
break;
204200
}

0 commit comments

Comments
 (0)