mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-18 17:26:28 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4975f24fee | |||
| 2952f6a3b6 | |||
| da7348ea82 | |||
| 16e326ad9f | |||
| 0002b45d18 | |||
| d9b2328e82 | |||
| 4a03005c68 | |||
| 2d57b45c7b | |||
| 52034411cf | |||
| 98cc8504d4 | |||
| 4c8ed5bb3d | |||
| 035a5b1629 | |||
| 9fed6ffb3f | |||
| 832aff0358 | |||
| 15da599e82 | |||
| 43e05ad821 | |||
| 9274acb591 | |||
| 2fd32868c2 | |||
| 5c7759ab72 | |||
| e51b72f0e0 |
@@ -20,7 +20,13 @@ class Input {
|
||||
*/
|
||||
public $file;
|
||||
|
||||
public function __construct() {
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
public function __construct(Request &$request) {
|
||||
$this->request = $request;
|
||||
$this->setGet();
|
||||
$this->setPost();
|
||||
$this->setFile();
|
||||
@@ -32,17 +38,31 @@ class Input {
|
||||
* @return array
|
||||
*/
|
||||
public function all(array $filter = null) {
|
||||
$output = $this->get->getData();
|
||||
$output = array_merge($output, $this->post->getData());
|
||||
|
||||
if($filter !== null) {
|
||||
$tmp = array();
|
||||
foreach($output as $key => $val) {
|
||||
if(in_array($key, $filter)) {
|
||||
$tmp[$key] = $val;
|
||||
$output = $_POST;
|
||||
|
||||
if($this->request->getMethod() === 'post') {
|
||||
|
||||
$contents = file_get_contents('php://input');
|
||||
|
||||
if (stripos(trim($contents), '{') === 0) {
|
||||
$output = json_decode($contents, true);
|
||||
if($output === false) {
|
||||
$output = array();
|
||||
}
|
||||
}
|
||||
return $tmp;
|
||||
}
|
||||
|
||||
$output = array_merge($_GET, $output);
|
||||
|
||||
if($filter !== null) {
|
||||
$output = array_filter($output, function ($key) use ($filter) {
|
||||
if (in_array($key, $filter)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
}
|
||||
|
||||
return $output;
|
||||
@@ -58,7 +78,7 @@ class Input {
|
||||
return ($key !== null) ? $element[$key] : $element;
|
||||
}
|
||||
|
||||
if(Request::getInstance()->getMethod() !== 'get') {
|
||||
if($this->request->getMethod() !== 'get') {
|
||||
|
||||
$element = $this->post->findFirst($index);
|
||||
|
||||
@@ -87,7 +107,7 @@ class Input {
|
||||
|
||||
if($item !== null) {
|
||||
|
||||
if(is_array($item) || $item instanceof InputFile) {
|
||||
if($item instanceof InputCollection || $item instanceof InputFile) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
@@ -111,10 +131,10 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($get as $k => $g) {
|
||||
$output[$k] = new InputItem($k, $g);
|
||||
$output->{$k} = new InputItem($k, $g);
|
||||
}
|
||||
|
||||
$this->get->{$key} = $output;
|
||||
@@ -125,12 +145,10 @@ class Input {
|
||||
public function setPost() {
|
||||
$this->post = new InputCollection();
|
||||
|
||||
$postVars = array();
|
||||
$postVars = $_POST;
|
||||
|
||||
if(isset($_SERVER['REQUEST_METHOD']) && in_array($_SERVER['REQUEST_METHOD'], ['PUT', 'PATCH', 'DELETE'])) {
|
||||
if(in_array($this->request->getMethod(), ['put', 'patch', 'delete'])) {
|
||||
parse_str(file_get_contents('php://input'), $postVars);
|
||||
} else {
|
||||
$postVars = $_POST;
|
||||
}
|
||||
|
||||
if(count($postVars)) {
|
||||
@@ -141,10 +159,10 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($post as $k=>$p) {
|
||||
$output[$k] = new InputItem($k, $p);
|
||||
foreach($post as $k => $p) {
|
||||
$output->{$k} = new InputItem($k, $p);
|
||||
}
|
||||
|
||||
$this->post->{strtolower($key)} = $output;
|
||||
@@ -172,7 +190,7 @@ class Input {
|
||||
continue;
|
||||
}
|
||||
|
||||
$output = array();
|
||||
$output = new InputCollection();
|
||||
|
||||
foreach($value['name'] as $k=>$val) {
|
||||
// Strip empty values
|
||||
@@ -183,7 +201,7 @@ class Input {
|
||||
$file->setType($value['type'][$k]);
|
||||
$file->setTmpName($value['tmp_name'][$k]);
|
||||
$file->setError($value['error'][$k]);
|
||||
$output[$k] = $file;
|
||||
$output->{$k} = $file;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ class Request {
|
||||
$this->uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : array();
|
||||
$this->method = (isset($_POST['_method'])) ? strtolower($_POST['_method']) : (isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : array());
|
||||
$this->headers = $this->getAllHeaders();
|
||||
$this->input = new Input();
|
||||
$this->input = new Input($this);
|
||||
}
|
||||
|
||||
protected function getAllHeaders() {
|
||||
@@ -96,7 +96,10 @@ class Request {
|
||||
* @return string
|
||||
*/
|
||||
public function getIp() {
|
||||
return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']);
|
||||
if(isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
|
||||
return $_SERVER['HTTP_CF_CONNECTING_IP'];
|
||||
}
|
||||
return ((isset($_SERVER['HTTP_X_FORWARDED_FOR']) && strlen($_SERVER['HTTP_X_FORWARDED_FOR'])) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -69,13 +69,19 @@ class Response {
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @throws \InvalidArgumentException;
|
||||
*/
|
||||
public function json(array $value) {
|
||||
public function json($value) {
|
||||
|
||||
if(($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
|
||||
$this->header('Content-type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,15 +86,22 @@ class RouterBase {
|
||||
$this->currentRoute = $route;
|
||||
|
||||
if($route instanceof RouterGroup && is_callable($route->getCallback())) {
|
||||
$group = $route;
|
||||
|
||||
$group->renderRoute($this->request);
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
$route->renderRoute($this->request);
|
||||
|
||||
if($route->matchRoute($this->request)) {
|
||||
|
||||
$group = $route;
|
||||
|
||||
$mergedSettings = array_merge($settings, $group->getMergeableSettings());
|
||||
|
||||
// Add ExceptionHandler
|
||||
if ($group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
|
||||
// Add ExceptionHandler
|
||||
if($group->matchRoute($this->request) && $group->getExceptionHandler() !== null) {
|
||||
$this->exceptionHandlers[] = $route;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->currentRoute = null;
|
||||
@@ -113,6 +120,8 @@ class RouterBase {
|
||||
|
||||
$originalUri = $this->request->getUri();
|
||||
|
||||
$routeNotAllowed = false;
|
||||
|
||||
try {
|
||||
|
||||
// Initialize boot-managers
|
||||
@@ -137,8 +146,6 @@ class RouterBase {
|
||||
}
|
||||
}
|
||||
|
||||
$routeNotAllowed = false;
|
||||
|
||||
$max = count($this->controllerUrlMap);
|
||||
|
||||
/* @var $route RouterEntry */
|
||||
@@ -303,15 +310,16 @@ class RouterBase {
|
||||
|
||||
public function arrayToParams(array $getParams = null, $includeEmpty = true) {
|
||||
|
||||
if(is_array($getParams)) {
|
||||
if(is_array($getParams) && count($getParams)) {
|
||||
if ($includeEmpty === false) {
|
||||
$getParams = array_filter($getParams, function ($item) {
|
||||
return (!empty($item));
|
||||
});
|
||||
}
|
||||
|
||||
return http_build_query($getParams);
|
||||
return '?' . http_build_query($getParams);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -366,8 +374,8 @@ class RouterBase {
|
||||
|
||||
$url = rtrim($url, '/') . '/';
|
||||
|
||||
if($getParams !== null && count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
@@ -389,8 +397,8 @@ class RouterBase {
|
||||
|
||||
$url = parse_url($this->request->getUri(), PHP_URL_PATH);
|
||||
|
||||
if(count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
@@ -461,8 +469,8 @@ class RouterBase {
|
||||
|
||||
$url = '/' . trim(join('/', $url), '/') . '/';
|
||||
|
||||
if($getParams !== null && count($getParams)) {
|
||||
$url .= '?' . $this->arrayToParams($getParams);
|
||||
if($getParams !== null) {
|
||||
$url .= $this->arrayToParams($getParams);
|
||||
}
|
||||
|
||||
return $url;
|
||||
@@ -475,8 +483,4 @@ class RouterBase {
|
||||
return static::$instance;
|
||||
}
|
||||
|
||||
public static function reset() {
|
||||
static::$instance = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -97,9 +97,12 @@ class RouterGroup extends RouterEntry {
|
||||
if($this->getMiddleware() !== null && isset($settings['middleware'])) {
|
||||
|
||||
if(!is_array($this->getMiddleware())) {
|
||||
$middlewares = [$this->getMiddleware(), $settings['middleware']];
|
||||
$middlewares = [
|
||||
$this->getMiddleware(),
|
||||
$settings['middleware']
|
||||
];
|
||||
} else {
|
||||
$middlewares = array_push($settings['middleware']);
|
||||
$middlewares = array_push($settings['middleware'], $this->getMiddleware());
|
||||
}
|
||||
|
||||
$settings['middleware'] = array_unique(array_reverse($middlewares));
|
||||
|
||||
+2
-3
@@ -12,7 +12,6 @@ class GroupTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testGroup() {
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
$this->result = false;
|
||||
|
||||
@@ -28,9 +27,9 @@ class GroupTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
|
||||
public function testNestedGroup() {
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/api/v1/test');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/api'], function() {
|
||||
\Pecee\SimpleRouter\SimpleRouter::group(['prefix' => '/v1'], function() {
|
||||
|
||||
@@ -7,7 +7,6 @@ require_once 'Dummy/Handler/ExceptionHandler.php';
|
||||
class MiddlewareTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testMiddlewareFound() {
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/my/test/url');
|
||||
|
||||
@@ -8,7 +8,6 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
|
||||
public function testNotFound() {
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||
@@ -31,46 +30,40 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testGet() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setMethod('get');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testPost() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::post('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testPut() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('put');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::put('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
}
|
||||
|
||||
public function testDelete() {
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('delete');
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::delete('/my/test/url', 'DummyController@start');
|
||||
\Pecee\SimpleRouter\SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
public function testMethodNotAllowed() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::getInstance()->getRequest()->setUri('/my/test/url');
|
||||
\Pecee\Http\Request::getInstance()->setMethod('post');
|
||||
|
||||
\Pecee\SimpleRouter\SimpleRouter::get('/my/test/url', 'DummyController@start');
|
||||
@@ -85,8 +78,6 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testSimpleParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1');
|
||||
|
||||
@@ -97,8 +88,6 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testMultiParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test-param1-param2');
|
||||
|
||||
@@ -109,8 +98,6 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testPathParam() {
|
||||
|
||||
\Pecee\SimpleRouter\RouterBase::reset();
|
||||
|
||||
\Pecee\Http\Request::getInstance()->setMethod('get');
|
||||
\Pecee\Http\Request::getInstance()->setUri('/test/path/param1');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user