mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-12 19:12:11 +00:00
Development
- Ensure that request-method is always lowercase. - Fixed spaces instead of tabs to comply with PSR-2.
This commit is contained in:
@@ -1,46 +1,47 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Pecee\Controllers;
|
namespace Pecee\Controllers;
|
||||||
|
|
||||||
interface IRestController {
|
interface IRestController
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function index();
|
function index();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function show($id);
|
function show($id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function store();
|
function store();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function create();
|
function create();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* View
|
* View
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function edit($id);
|
function edit($id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function update($id);
|
function update($id);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $id
|
* @param mixed $id
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function destroy($id);
|
function destroy($id);
|
||||||
|
|
||||||
}
|
}
|
||||||
+58
-55
@@ -3,66 +3,69 @@ namespace Pecee;
|
|||||||
|
|
||||||
class CsrfToken
|
class CsrfToken
|
||||||
{
|
{
|
||||||
const CSRF_KEY = 'XSRF-TOKEN';
|
const CSRF_KEY = 'XSRF-TOKEN';
|
||||||
|
|
||||||
protected $token;
|
protected $token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate random identifier for CSRF token
|
* Generate random identifier for CSRF token
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function generateToken()
|
public static function generateToken()
|
||||||
{
|
{
|
||||||
if (function_exists('mcrypt_create_iv')) {
|
if (function_exists('mcrypt_create_iv')) {
|
||||||
return bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
|
return bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
|
||||||
}
|
}
|
||||||
return bin2hex(openssl_random_pseudo_bytes(32));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return bin2hex(openssl_random_pseudo_bytes(32));
|
||||||
* Validate valid CSRF token
|
}
|
||||||
*
|
|
||||||
* @param string $token
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function validate($token)
|
|
||||||
{
|
|
||||||
if ($token !== null && $this->getToken() !== null) {
|
|
||||||
return hash_equals($token, $this->getToken());
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set csrf token cookie
|
* Validate valid CSRF token
|
||||||
*
|
*
|
||||||
* @param $token
|
* @param string $token
|
||||||
*/
|
* @return bool
|
||||||
public function setToken($token)
|
*/
|
||||||
{
|
public function validate($token)
|
||||||
setcookie(static::CSRF_KEY, $token, time() + 60 * 120, '/');
|
{
|
||||||
}
|
if ($token !== null && $this->getToken() !== null) {
|
||||||
|
return hash_equals($token, $this->getToken());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return false;
|
||||||
* Get csrf token
|
}
|
||||||
* @return string|null
|
|
||||||
*/
|
|
||||||
public function getToken()
|
|
||||||
{
|
|
||||||
if ($this->hasToken()) {
|
|
||||||
return $_COOKIE[static::CSRF_KEY];
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the csrf token has been defined
|
* Set csrf token cookie
|
||||||
* @return bool
|
*
|
||||||
*/
|
* @param $token
|
||||||
public function hasToken()
|
*/
|
||||||
{
|
public function setToken($token)
|
||||||
return isset($_COOKIE[static::CSRF_KEY]);
|
{
|
||||||
}
|
setcookie(static::CSRF_KEY, $token, time() + 60 * 120, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get csrf token
|
||||||
|
* @return string|null
|
||||||
|
*/
|
||||||
|
public function getToken()
|
||||||
|
{
|
||||||
|
if ($this->hasToken()) {
|
||||||
|
return $_COOKIE[static::CSRF_KEY];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the csrf token has been defined
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasToken()
|
||||||
|
{
|
||||||
|
return isset($_COOKIE[static::CSRF_KEY]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,12 +6,12 @@ use Pecee\SimpleRouter\Route\ILoadableRoute;
|
|||||||
|
|
||||||
interface IExceptionHandler
|
interface IExceptionHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param ILoadableRoute $route
|
* @param ILoadableRoute $route
|
||||||
* @param \Exception $error
|
* @param \Exception $error
|
||||||
* @return Request|null
|
* @return Request|null
|
||||||
*/
|
*/
|
||||||
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error);
|
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -4,12 +4,12 @@ namespace Pecee\Http\Input;
|
|||||||
interface IInputItem
|
interface IInputItem
|
||||||
{
|
{
|
||||||
|
|
||||||
public function getIndex();
|
public function getIndex();
|
||||||
|
|
||||||
public function getName();
|
public function getName();
|
||||||
|
|
||||||
public function getValue();
|
public function getValue();
|
||||||
|
|
||||||
public function __toString();
|
public function __toString();
|
||||||
|
|
||||||
}
|
}
|
||||||
+165
-192
@@ -5,247 +5,220 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class Input
|
class Input
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $get = [];
|
public $get = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $post = [];
|
public $post = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $file = [];
|
public $file = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Request
|
* @var Request
|
||||||
*/
|
*/
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
protected $invalidContentType = false;
|
public function __construct(Request $request)
|
||||||
|
{
|
||||||
|
$this->request = $request;
|
||||||
|
|
||||||
protected $invalidContentTypes = [
|
$this->parseInputs();
|
||||||
'text/plain',
|
}
|
||||||
'application/x-www-form-urlencoded',
|
|
||||||
];
|
|
||||||
|
|
||||||
public function __construct(Request $request)
|
protected function parseInputs()
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
/* Parse get requests */
|
||||||
|
if (count($_GET) > 0) {
|
||||||
|
$this->get = $this->handleGetPost($_GET);
|
||||||
|
}
|
||||||
|
|
||||||
if ($request->getMethod() !== 'get') {
|
/* Parse post requests */
|
||||||
|
$postVars = $_POST;
|
||||||
|
|
||||||
$requestContentType = $request->getHeader('http-content-type');
|
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
|
||||||
|
parse_str(file_get_contents('php://input'), $postVars);
|
||||||
|
}
|
||||||
|
|
||||||
$max = count($this->invalidContentTypes) - 1;
|
if (count($postVars) > 0) {
|
||||||
|
$this->post = $this->handleGetPost($postVars);
|
||||||
|
}
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
/* Parse get requests */
|
||||||
|
|
||||||
$contentType = $this->invalidContentType[$i];
|
if (count($_FILES) > 0) {
|
||||||
|
|
||||||
if (stripos($requestContentType, $contentType) === 0) {
|
$max = count($_FILES) - 1;
|
||||||
$this->invalidContentType = true;
|
$keys = array_keys($_FILES);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->invalidContentType === false) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
$this->parseInputs();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
$key = $keys[$i];
|
||||||
|
$value = $_FILES[$key];
|
||||||
|
|
||||||
protected function parseInputs()
|
// Handle array input
|
||||||
{
|
if (is_array($value['name']) === false) {
|
||||||
/* Parse get requests */
|
$values['index'] = $key;
|
||||||
if (count($_GET) > 0) {
|
$this->file[$key] = InputFile::createFromArray(array_merge($value, $values));
|
||||||
$this->get = $this->handleGetPost($_GET);
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parse post requests */
|
$subMax = count($value['name']) - 1;
|
||||||
$postVars = $_POST;
|
$keys = array_keys($value['name']);
|
||||||
|
$output = [];
|
||||||
|
|
||||||
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete']) === true) {
|
for ($i = $subMax; $i >= 0; $i--) {
|
||||||
parse_str(file_get_contents('php://input'), $postVars);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($postVars) > 0) {
|
$output[$keys[$i]] = InputFile::createFromArray([
|
||||||
$this->post = $this->handleGetPost($postVars);
|
'index' => $key,
|
||||||
}
|
'error' => $value['error'][$keys[$i]],
|
||||||
|
'tmp_name' => $value['tmp_name'][$keys[$i]],
|
||||||
|
'type' => $value['type'][$keys[$i]],
|
||||||
|
'size' => $value['size'][$keys[$i]],
|
||||||
|
'name' => $value['name'][$keys[$i]],
|
||||||
|
]);
|
||||||
|
|
||||||
/* Parse get requests */
|
}
|
||||||
|
|
||||||
if (count($_FILES) > 0) {
|
$this->file[$key] = $output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$max = count($_FILES) - 1;
|
protected function handleGetPost($array)
|
||||||
$keys = array_keys($_FILES);
|
{
|
||||||
|
$tmp = [];
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
$max = count($array) - 1;
|
||||||
|
$keys = array_keys($array);
|
||||||
|
|
||||||
$key = $keys[$i];
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
$value = $_FILES[$key];
|
|
||||||
|
|
||||||
// Handle array input
|
$key = $keys[$i];
|
||||||
if (is_array($value['name']) === false) {
|
$value = $array[$key];
|
||||||
$values['index'] = $key;
|
|
||||||
$this->file[$key] = InputFile::createFromArray(array_merge($value, $values));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$subMax = count($value['name']) - 1;
|
// Handle array input
|
||||||
$keys = array_keys($value['name']);
|
if (is_array($value) === false) {
|
||||||
$output = [];
|
$tmp[$key] = new InputItem($key, $value);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for ($i = $subMax; $i >= 0; $i--) {
|
$subMax = count($value) - 1;
|
||||||
|
$keys = array_keys($value);
|
||||||
|
$output = [];
|
||||||
|
|
||||||
$output[$keys[$i]] = InputFile::createFromArray([
|
for ($i = $subMax; $i >= 0; $i--) {
|
||||||
'index' => $key,
|
$output[$keys[$i]] = new InputItem($key, $value[$keys[$i]]);
|
||||||
'error' => $value['error'][$keys[$i]],
|
}
|
||||||
'tmp_name' => $value['tmp_name'][$keys[$i]],
|
|
||||||
'type' => $value['type'][$keys[$i]],
|
|
||||||
'size' => $value['size'][$keys[$i]],
|
|
||||||
'name' => $value['name'][$keys[$i]],
|
|
||||||
]);
|
|
||||||
|
|
||||||
}
|
$tmp[$key] = $output;
|
||||||
|
}
|
||||||
|
|
||||||
$this->file[$key] = $output;
|
return $tmp;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function handleGetPost($array)
|
public function findPost($index, $default = null)
|
||||||
{
|
{
|
||||||
$tmp = [];
|
return isset($this->post[$index]) ? $this->post[$index] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
$max = count($array) - 1;
|
public function findFile($index, $default = null)
|
||||||
$keys = array_keys($array);
|
{
|
||||||
|
return isset($this->file[$index]) ? $this->file[$index] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
public function findGet($index, $default = null)
|
||||||
|
{
|
||||||
|
return isset($this->get[$index]) ? $this->get[$index] : $default;
|
||||||
|
}
|
||||||
|
|
||||||
$key = $keys[$i];
|
public function getObject($index, $default = null, $method = null)
|
||||||
$value = $array[$key];
|
{
|
||||||
|
$element = null;
|
||||||
|
|
||||||
// Handle array input
|
if ($method === null || strtolower($method) === 'get') {
|
||||||
if (is_array($value) === false) {
|
$element = $this->findGet($index);
|
||||||
$tmp[$key] = new InputItem($key, $value);
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$subMax = count($value) - 1;
|
if ($element === null && $method === null || strtolower($method) === 'post') {
|
||||||
$keys = array_keys($value);
|
$element = $this->findPost($index);
|
||||||
$output = [];
|
}
|
||||||
|
|
||||||
for ($i = $subMax; $i >= 0; $i--) {
|
if ($element === null && $method === null || strtolower($method) === 'file') {
|
||||||
$output[$keys[$i]] = new InputItem($key, $value[$keys[$i]]);
|
$element = $this->findFile($index);
|
||||||
}
|
}
|
||||||
|
|
||||||
$tmp[$key] = $output;
|
return ($element === null) ? $default : $element;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $tmp;
|
/**
|
||||||
}
|
* Get input element value matching index
|
||||||
|
*
|
||||||
|
* @param string $index
|
||||||
|
* @param string|null $default
|
||||||
|
* @param string|null $method
|
||||||
|
* @return InputItem|string
|
||||||
|
*/
|
||||||
|
public function get($index, $default = null, $method = null)
|
||||||
|
{
|
||||||
|
$input = $this->getObject($index, $default, $method);
|
||||||
|
|
||||||
public function findPost($index, $default = null)
|
if ($input instanceof InputItem) {
|
||||||
{
|
return (trim($input->getValue()) === '') ? $default : $input->getValue();
|
||||||
return isset($this->post[$index]) ? $this->post[$index] : $default;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public function findFile($index, $default = null)
|
return $input;
|
||||||
{
|
}
|
||||||
return isset($this->file[$index]) ? $this->file[$index] : $default;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function findGet($index, $default = null)
|
public function exists($index)
|
||||||
{
|
{
|
||||||
return isset($this->get[$index]) ? $this->get[$index] : $default;
|
return ($this->getObject($index) !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getObject($index, $default = null, $method = null)
|
/**
|
||||||
{
|
* Get all get/post items
|
||||||
$element = null;
|
* @param array|null $filter Only take items in filter
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function all(array $filter = null)
|
||||||
|
{
|
||||||
|
if ($this->invalidContentType === true) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
if ($method === null || strtolower($method) === 'get') {
|
$output = $_POST;
|
||||||
$element = $this->findGet($index);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($element === null && $method === null || strtolower($method) === 'post') {
|
if ($this->request->getMethod() === 'post') {
|
||||||
$element = $this->findPost($index);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($element === null && $method === null || strtolower($method) === 'file') {
|
$contents = file_get_contents('php://input');
|
||||||
$element = $this->findFile($index);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ($element === null) ? $default : $element;
|
if (stripos(trim($contents), '{') === 0) {
|
||||||
}
|
$output = json_decode($contents, true);
|
||||||
|
if ($output === false) {
|
||||||
|
$output = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$output = array_merge($_GET, $output);
|
||||||
* Get input element value matching index
|
|
||||||
*
|
|
||||||
* @param string $index
|
|
||||||
* @param string|null $default
|
|
||||||
* @param string|null $method
|
|
||||||
* @return InputItem|string
|
|
||||||
*/
|
|
||||||
public function get($index, $default = null, $method = null)
|
|
||||||
{
|
|
||||||
$input = $this->getObject($index, $default, $method);
|
|
||||||
|
|
||||||
if ($input instanceof InputItem) {
|
if ($filter !== null) {
|
||||||
return (trim($input->getValue()) === '') ? $default : $input->getValue();
|
$output = array_filter($output, function ($key) use ($filter) {
|
||||||
}
|
if (in_array($key, $filter)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
return $input;
|
return false;
|
||||||
}
|
}, ARRAY_FILTER_USE_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
public function exists($index)
|
return $output;
|
||||||
{
|
}
|
||||||
return ($this->getObject($index) !== null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all get/post items
|
|
||||||
* @param array|null $filter Only take items in filter
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function all(array $filter = null)
|
|
||||||
{
|
|
||||||
if ($this->invalidContentType === true) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
$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 = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+155
-155
@@ -3,188 +3,188 @@ namespace Pecee\Http\Input;
|
|||||||
|
|
||||||
class InputFile implements IInputItem
|
class InputFile implements IInputItem
|
||||||
{
|
{
|
||||||
public $index;
|
public $index;
|
||||||
public $name;
|
public $name;
|
||||||
public $size;
|
public $size;
|
||||||
public $type;
|
public $type;
|
||||||
public $error;
|
public $error;
|
||||||
public $tmpName;
|
public $tmpName;
|
||||||
|
|
||||||
public function __construct($index)
|
public function __construct($index)
|
||||||
{
|
{
|
||||||
$this->index = $index;
|
$this->index = $index;
|
||||||
|
|
||||||
// Make the name human friendly, by replace _ with space
|
// Make the name human friendly, by replace _ with space
|
||||||
$this->name = ucfirst(str_replace('_', ' ', $this->index));
|
$this->name = ucfirst(str_replace('_', ' ', $this->index));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getIndex()
|
public function getIndex()
|
||||||
{
|
{
|
||||||
return $this->index;
|
return $this->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getSize()
|
public function getSize()
|
||||||
{
|
{
|
||||||
return $this->size;
|
return $this->size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getType()
|
public function getType()
|
||||||
{
|
{
|
||||||
return $this->type;
|
return $this->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getError()
|
public function getError()
|
||||||
{
|
{
|
||||||
return $this->error;
|
return $this->error;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getMime()
|
public function getMime()
|
||||||
{
|
{
|
||||||
return $this->getType();
|
return $this->getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTmpName()
|
public function getTmpName()
|
||||||
{
|
{
|
||||||
return $this->tmpName;
|
return $this->tmpName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getExtension()
|
public function getExtension()
|
||||||
{
|
{
|
||||||
return pathinfo($this->getName(), PATHINFO_EXTENSION);
|
return pathinfo($this->getName(), PATHINFO_EXTENSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function move($destination)
|
public function move($destination)
|
||||||
{
|
{
|
||||||
return move_uploaded_file($this->tmpName, $destination);
|
return move_uploaded_file($this->tmpName, $destination);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getContents()
|
public function getContents()
|
||||||
{
|
{
|
||||||
return file_get_contents($this->tmpName);
|
return file_get_contents($this->tmpName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set file temp. name
|
* Set file temp. name
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setTmpName($name)
|
public function setTmpName($name)
|
||||||
{
|
{
|
||||||
$this->tmpName = $name;
|
$this->tmpName = $name;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set file size
|
* Set file size
|
||||||
* @param int $size
|
* @param int $size
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setSize($size)
|
public function setSize($size)
|
||||||
{
|
{
|
||||||
$this->size = $size;
|
$this->size = $size;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set type
|
* Set type
|
||||||
* @param string $type
|
* @param string $type
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setType($type)
|
public function setType($type)
|
||||||
{
|
{
|
||||||
$this->type = $type;
|
$this->type = $type;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set error
|
* Set error
|
||||||
* @param int $error
|
* @param int $error
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setError($error)
|
public function setError($error)
|
||||||
{
|
{
|
||||||
$this->error = (int)$error;
|
$this->error = (int)$error;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getValue()
|
public function getValue()
|
||||||
{
|
{
|
||||||
return $this->getTmpName();
|
return $this->getTmpName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasError()
|
public function hasError()
|
||||||
{
|
{
|
||||||
return ($this->getError() !== 0);
|
return ($this->getError() !== 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create from array
|
* Create from array
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function createFromArray(array $values)
|
public static function createFromArray(array $values)
|
||||||
{
|
{
|
||||||
if (!isset($values['index'])) {
|
if (!isset($values['index'])) {
|
||||||
throw new \InvalidArgumentException('Index key is required');
|
throw new \InvalidArgumentException('Index key is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
$input = new static($values['index']);
|
$input = new static($values['index']);
|
||||||
$input->setError(isset($values['error']) ? $values['error'] : null);
|
$input->setError(isset($values['error']) ? $values['error'] : null);
|
||||||
$input->setName(isset($values['name']) ? $values['name'] : null);
|
$input->setName(isset($values['name']) ? $values['name'] : null);
|
||||||
$input->setSize(isset($values['size']) ? $values['size'] : null);
|
$input->setSize(isset($values['size']) ? $values['size'] : null);
|
||||||
$input->setType(isset($values['type']) ? $values['type'] : null);
|
$input->setType(isset($values['type']) ? $values['type'] : null);
|
||||||
$input->setTmpName(isset($values['tmp_name']) ? $values['tmp_name'] : null);
|
$input->setTmpName(isset($values['tmp_name']) ? $values['tmp_name'] : null);
|
||||||
|
|
||||||
return $input;
|
return $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'tmp_name' => $this->tmpName,
|
'tmp_name' => $this->tmpName,
|
||||||
'type' => $this->type,
|
'type' => $this->type,
|
||||||
'size' => $this->size,
|
'size' => $this->size,
|
||||||
'name' => $this->name,
|
'name' => $this->name,
|
||||||
'error' => $this->error,
|
'error' => $this->error,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString()
|
public function __toString()
|
||||||
{
|
{
|
||||||
return $this->getValue();
|
return $this->getValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,68 +3,70 @@ namespace Pecee\Http\Input;
|
|||||||
|
|
||||||
class InputItem implements IInputItem
|
class InputItem implements IInputItem
|
||||||
{
|
{
|
||||||
public $index;
|
public $index;
|
||||||
public $name;
|
public $name;
|
||||||
public $value;
|
public $value;
|
||||||
|
|
||||||
public function __construct($index, $value = null)
|
public function __construct($index, $value = null)
|
||||||
{
|
{
|
||||||
$this->index = $index;
|
$this->index = $index;
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
|
|
||||||
// Make the name human friendly, by replace _ with space
|
// Make the name human friendly, by replace _ with space
|
||||||
$this->name = ucfirst(str_replace('_', ' ', $this->index));
|
$this->name = ucfirst(str_replace('_', ' ', $this->index));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getIndex()
|
public function getIndex()
|
||||||
{
|
{
|
||||||
return $this->index;
|
return $this->index;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getValue()
|
public function getValue()
|
||||||
{
|
{
|
||||||
return $this->value;
|
return $this->value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set input name
|
* Set input name
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Set input value
|
}
|
||||||
* @param string $value
|
|
||||||
* @return static $this
|
|
||||||
*/
|
|
||||||
public function setValue($value)
|
|
||||||
{
|
|
||||||
$this->value = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __toString()
|
/**
|
||||||
{
|
* Set input value
|
||||||
return (string)$this->value;
|
* @param string $value
|
||||||
}
|
* @return static $this
|
||||||
|
*/
|
||||||
|
public function setValue($value)
|
||||||
|
{
|
||||||
|
$this->value = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return (string)$this->value;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,92 +8,93 @@ use Pecee\SimpleRouter\Route\ILoadableRoute;
|
|||||||
|
|
||||||
class BaseCsrfVerifier implements IMiddleware
|
class BaseCsrfVerifier implements IMiddleware
|
||||||
{
|
{
|
||||||
const POST_KEY = 'csrf-token';
|
const POST_KEY = 'csrf-token';
|
||||||
const HEADER_KEY = 'X-CSRF-TOKEN';
|
const HEADER_KEY = 'X-CSRF-TOKEN';
|
||||||
|
|
||||||
protected $except;
|
protected $except;
|
||||||
protected $csrfToken;
|
protected $csrfToken;
|
||||||
protected $token;
|
protected $token;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->csrfToken = new CsrfToken();
|
$this->csrfToken = new CsrfToken();
|
||||||
|
|
||||||
// Generate or get the CSRF-Token from Cookie.
|
// Generate or get the CSRF-Token from Cookie.
|
||||||
$this->token = ($this->hasToken() === false) ? $this->generateToken() : $this->csrfToken->getToken();
|
$this->token = ($this->hasToken() === false) ? $this->generateToken() : $this->csrfToken->getToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the url matches the urls in the except property
|
* Check if the url matches the urls in the except property
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function skip(Request $request)
|
protected function skip(Request $request)
|
||||||
{
|
{
|
||||||
if ($this->except === null || is_array($this->except) === false) {
|
if ($this->except === null || is_array($this->except) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$max = count($this->except) - 1;
|
$max = count($this->except) - 1;
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
$url = $this->except[$i];
|
$url = $this->except[$i];
|
||||||
|
|
||||||
$url = rtrim($url, '/');
|
$url = rtrim($url, '/');
|
||||||
if ($url[strlen($url) - 1] === '*') {
|
if ($url[strlen($url) - 1] === '*') {
|
||||||
$url = rtrim($url, '*');
|
$url = rtrim($url, '*');
|
||||||
$skip = (stripos($request->getUri(), $url) === 0);
|
$skip = (stripos($request->getUri(), $url) === 0);
|
||||||
} else {
|
} else {
|
||||||
$skip = ($url === rtrim($request->getUri(), '/'));
|
$skip = ($url === rtrim($request->getUri(), '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($skip === true) {
|
if ($skip === true) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request, ILoadableRoute &$route = null)
|
public function handle(Request $request, ILoadableRoute &$route = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (in_array($request->getMethod(), ['post', 'put', 'delete']) === true && $this->skip($request) === false) {
|
if (in_array($request->getMethod(), ['post', 'put', 'delete']) === true && $this->skip($request) === false) {
|
||||||
|
|
||||||
$token = $request->getInput()->get(static::POST_KEY, null, 'post');
|
$token = $request->getInput()->get(static::POST_KEY, null, 'post');
|
||||||
|
|
||||||
// If the token is not posted, check headers for valid x-csrf-token
|
// If the token is not posted, check headers for valid x-csrf-token
|
||||||
if ($token === null) {
|
if ($token === null) {
|
||||||
$token = $request->getHeader(static::HEADER_KEY);
|
$token = $request->getHeader(static::HEADER_KEY);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->csrfToken->validate($token) === false) {
|
if ($this->csrfToken->validate($token) === false) {
|
||||||
throw new TokenMismatchException('Invalid csrf-token.');
|
throw new TokenMismatchException('Invalid csrf-token.');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generateToken()
|
public function generateToken()
|
||||||
{
|
{
|
||||||
$token = $this->csrfToken->generateToken();
|
$token = $this->csrfToken->generateToken();
|
||||||
$this->csrfToken->setToken($token);
|
$this->csrfToken->setToken($token);
|
||||||
return $token;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function hasToken()
|
return $token;
|
||||||
{
|
}
|
||||||
if ($this->token != null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->csrfToken->hasToken();
|
public function hasToken()
|
||||||
}
|
{
|
||||||
|
if ($this->token != null) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public function getToken()
|
return $this->csrfToken->hasToken();
|
||||||
{
|
}
|
||||||
return $this->token;
|
|
||||||
}
|
public function getToken()
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,11 +6,11 @@ use Pecee\SimpleRouter\Route\ILoadableRoute;
|
|||||||
|
|
||||||
interface IMiddleware
|
interface IMiddleware
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param ILoadableRoute $route
|
* @param ILoadableRoute $route
|
||||||
* @return Request|null
|
* @return Request|null
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, ILoadableRoute &$route);
|
public function handle(Request $request, ILoadableRoute &$route);
|
||||||
|
|
||||||
}
|
}
|
||||||
+188
-188
@@ -5,226 +5,226 @@ use Pecee\Http\Input\Input;
|
|||||||
|
|
||||||
class Request
|
class Request
|
||||||
{
|
{
|
||||||
protected $data = [];
|
protected $data = [];
|
||||||
protected $headers;
|
protected $headers;
|
||||||
protected $host;
|
protected $host;
|
||||||
protected $uri;
|
protected $uri;
|
||||||
protected $method;
|
protected $method;
|
||||||
protected $input;
|
protected $input;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->parseHeaders();
|
$this->parseHeaders();
|
||||||
$this->host = $this->getHeader('http-host');;
|
$this->host = $this->getHeader('http-host');;
|
||||||
$this->uri = $this->getHeader('request-uri');
|
$this->uri = $this->getHeader('request-uri');
|
||||||
$this->input = new Input($this);
|
$this->input = new Input($this);
|
||||||
$this->method = $this->input->get('_method', strtolower($this->getHeader('request-method')));
|
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseHeaders()
|
protected function parseHeaders()
|
||||||
{
|
{
|
||||||
$this->headers = [];
|
$this->headers = [];
|
||||||
|
|
||||||
$max = count($_SERVER) - 1;
|
$max = count($_SERVER) - 1;
|
||||||
$keys = array_keys($_SERVER);
|
$keys = array_keys($_SERVER);
|
||||||
|
|
||||||
for($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
$key = $keys[$i];
|
$key = $keys[$i];
|
||||||
$value = $_SERVER[$key];
|
$value = $_SERVER[$key];
|
||||||
|
|
||||||
$this->headers[strtolower($key)] = $value;
|
$this->headers[strtolower($key)] = $value;
|
||||||
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;
|
$this->headers[strtolower(str_replace('_', '-', $key))] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isSecure()
|
public function isSecure()
|
||||||
{
|
{
|
||||||
if ($this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443) {
|
if ($this->getHeader('http-x-forwarded-proto') === 'https' || $this->getHeader('https') !== null || $this->getHeader('server-port') === 443) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUri()
|
public function getUri()
|
||||||
{
|
{
|
||||||
return $this->uri;
|
return $this->uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getHost()
|
public function getHost()
|
||||||
{
|
{
|
||||||
return $this->host;
|
return $this->host;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
{
|
{
|
||||||
return $this->method;
|
return $this->method;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get http basic auth user
|
* Get http basic auth user
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getUser()
|
public function getUser()
|
||||||
{
|
{
|
||||||
return $this->getHeader('php-auth-user');
|
return $this->getHeader('php-auth-user');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get http basic auth password
|
* Get http basic auth password
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getPassword()
|
public function getPassword()
|
||||||
{
|
{
|
||||||
return $this->getHeader('php-auth-pw');
|
return $this->getHeader('php-auth-pw');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all headers
|
* Get all headers
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getHeaders()
|
public function getHeaders()
|
||||||
{
|
{
|
||||||
return $this->headers;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get id address
|
* Get id address
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getIp()
|
public function getIp()
|
||||||
{
|
{
|
||||||
if ($this->getHeader('http-cf-connecting-ip') !== null) {
|
if ($this->getHeader('http-cf-connecting-ip') !== null) {
|
||||||
return $this->getHeader('http-cf-connecting-ip');
|
return $this->getHeader('http-cf-connecting-ip');
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->getHeader('http-x-forwarded-for') !== null && strlen($this->getHeader('http-x-forwarded-for'))) {
|
if ($this->getHeader('http-x-forwarded-for') !== null && strlen($this->getHeader('http-x-forwarded-for'))) {
|
||||||
return $this->getHeader('http-x-forwarded_for');
|
return $this->getHeader('http-x-forwarded_for');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->getHeader('remote-addr');
|
return $this->getHeader('remote-addr');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get referer
|
* Get referer
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getReferer()
|
public function getReferer()
|
||||||
{
|
{
|
||||||
return $this->getHeader('http-referer');
|
return $this->getHeader('http-referer');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get user agent
|
* Get user agent
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getUserAgent()
|
public function getUserAgent()
|
||||||
{
|
{
|
||||||
return $this->getHeader('http-user-agent');
|
return $this->getHeader('http-user-agent');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get header value by name
|
* Get header value by name
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param object|null $defaultValue
|
* @param object|null $defaultValue
|
||||||
*
|
*
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getHeader($name, $defaultValue = null)
|
public function getHeader($name, $defaultValue = null)
|
||||||
{
|
{
|
||||||
if (isset($this->headers[strtolower($name)])) {
|
if (isset($this->headers[strtolower($name)])) {
|
||||||
return $this->headers[strtolower($name)];
|
return $this->headers[strtolower($name)];
|
||||||
}
|
}
|
||||||
|
|
||||||
$max = count($_SERVER) - 1;
|
$max = count($_SERVER) - 1;
|
||||||
$keys = array_keys($_SERVER);
|
$keys = array_keys($_SERVER);
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
|
|
||||||
$key = $keys[$i];
|
$key = $keys[$i];
|
||||||
$name = $_SERVER[$key];
|
$name = $_SERVER[$key];
|
||||||
|
|
||||||
if ($key === $name) {
|
if ($key === $name) {
|
||||||
return $name;
|
return $name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $defaultValue;
|
return $defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get input class
|
* Get input class
|
||||||
* @return Input
|
* @return Input
|
||||||
*/
|
*/
|
||||||
public function getInput()
|
public function getInput()
|
||||||
{
|
{
|
||||||
return $this->input;
|
return $this->input;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is format accepted
|
* Is format accepted
|
||||||
*
|
*
|
||||||
* @param string $format
|
* @param string $format
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function isFormatAccepted($format)
|
public function isFormatAccepted($format)
|
||||||
{
|
{
|
||||||
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
|
return ($this->getHeader('http-accept') !== null && stripos($this->getHeader('http-accept'), $format) > -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get accept formats
|
* Get accept formats
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getAcceptFormats()
|
public function getAcceptFormats()
|
||||||
{
|
{
|
||||||
return explode(',', $this->getHeader('http-accept'));
|
return explode(',', $this->getHeader('http-accept'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $uri
|
* @param string $uri
|
||||||
*/
|
*/
|
||||||
public function setUri($uri)
|
public function setUri($uri)
|
||||||
{
|
{
|
||||||
$this->uri = $uri;
|
$this->uri = $uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $host
|
* @param string $host
|
||||||
*/
|
*/
|
||||||
public function setHost($host)
|
public function setHost($host)
|
||||||
{
|
{
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $method
|
* @param string $method
|
||||||
*/
|
*/
|
||||||
public function setMethod($method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __set($name, $value = null)
|
public function __set($name, $value = null)
|
||||||
{
|
{
|
||||||
$this->data[$name] = $value;
|
$this->data[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __get($name)
|
public function __get($name)
|
||||||
{
|
{
|
||||||
return isset($this->data[$name]) ? $this->data[$name] : null;
|
return isset($this->data[$name]) ? $this->data[$name] : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+99
-95
@@ -3,116 +3,120 @@ namespace Pecee\Http;
|
|||||||
|
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
protected $request;
|
protected $request;
|
||||||
|
|
||||||
public function __construct(Request $request)
|
public function __construct(Request $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the http status code
|
* Set the http status code
|
||||||
*
|
*
|
||||||
* @param int $code
|
* @param int $code
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function httpCode($code)
|
public function httpCode($code)
|
||||||
{
|
{
|
||||||
http_response_code($code);
|
http_response_code($code);
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Redirect the response
|
}
|
||||||
*
|
|
||||||
* @param string $url
|
|
||||||
* @param int $httpCode
|
|
||||||
*/
|
|
||||||
public function redirect($url, $httpCode = null)
|
|
||||||
{
|
|
||||||
if ($httpCode !== null) {
|
|
||||||
$this->httpCode($httpCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->header('location: ' . $url);
|
/**
|
||||||
die();
|
* Redirect the response
|
||||||
}
|
*
|
||||||
|
* @param string $url
|
||||||
|
* @param int $httpCode
|
||||||
|
*/
|
||||||
|
public function redirect($url, $httpCode = null)
|
||||||
|
{
|
||||||
|
if ($httpCode !== null) {
|
||||||
|
$this->httpCode($httpCode);
|
||||||
|
}
|
||||||
|
|
||||||
public function refresh()
|
$this->header('location: ' . $url);
|
||||||
{
|
die();
|
||||||
$this->redirect($this->request->getUri());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public function refresh()
|
||||||
* Add http authorisation
|
{
|
||||||
* @param string $name
|
$this->redirect($this->request->getUri());
|
||||||
* @return static
|
}
|
||||||
*/
|
|
||||||
public function auth($name = '')
|
|
||||||
{
|
|
||||||
$this->headers([
|
|
||||||
'WWW-Authenticate: Basic realm="' . $name . '"',
|
|
||||||
'HTTP/1.0 401 Unauthorized'
|
|
||||||
]);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function cache($eTag, $lastModified = 2592000)
|
/**
|
||||||
{
|
* Add http authorisation
|
||||||
|
* @param string $name
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function auth($name = '')
|
||||||
|
{
|
||||||
|
$this->headers([
|
||||||
|
'WWW-Authenticate: Basic realm="' . $name . '"',
|
||||||
|
'HTTP/1.0 401 Unauthorized',
|
||||||
|
]);
|
||||||
|
|
||||||
$this->headers([
|
return $this;
|
||||||
'Cache-Control: public',
|
}
|
||||||
'Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT',
|
|
||||||
'Etag: ' . $eTag
|
|
||||||
]);
|
|
||||||
|
|
||||||
$httpModified = $this->request->getHeader('http-if-modified-since');
|
public function cache($eTag, $lastModified = 2592000)
|
||||||
$httpIfNoneMatch = $this->request->getHeader('http-if-none-match');
|
{
|
||||||
|
|
||||||
if ($httpModified !== null && strtotime($httpModified) == $lastModified || $httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) {
|
$this->headers([
|
||||||
|
'Cache-Control: public',
|
||||||
|
'Last-Modified: ' . gmdate("D, d M Y H:i:s", $lastModified) . ' GMT',
|
||||||
|
'Etag: ' . $eTag,
|
||||||
|
]);
|
||||||
|
|
||||||
$this->header('HTTP/1.1 304 Not Modified');
|
$httpModified = $this->request->getHeader('http-if-modified-since');
|
||||||
|
$httpIfNoneMatch = $this->request->getHeader('http-if-none-match');
|
||||||
|
|
||||||
exit();
|
if ($httpModified !== null && strtotime($httpModified) == $lastModified || $httpIfNoneMatch !== null && $httpIfNoneMatch === $eTag) {
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
$this->header('HTTP/1.1 304 Not Modified');
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
exit();
|
||||||
* Json encode array
|
}
|
||||||
* @param array $value
|
|
||||||
*/
|
|
||||||
public function json(array $value)
|
|
||||||
{
|
|
||||||
$this->header('Content-Type: application/json');
|
|
||||||
echo json_encode($value);
|
|
||||||
die();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Add header to response
|
}
|
||||||
* @param string $value
|
|
||||||
* @return static
|
|
||||||
*/
|
|
||||||
public function header($value)
|
|
||||||
{
|
|
||||||
header($value);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add multiple headers to response
|
* Json encode array
|
||||||
* @param array $headers
|
* @param array $value
|
||||||
* @return static
|
*/
|
||||||
*/
|
public function json(array $value)
|
||||||
public function headers(array $headers)
|
{
|
||||||
{
|
$this->header('Content-Type: application/json');
|
||||||
$max = count($headers);
|
echo json_encode($value);
|
||||||
for($i = 0; $i < $max; $i++) {
|
die();
|
||||||
$this->header($headers[$i]);
|
}
|
||||||
}
|
|
||||||
return $this;
|
/**
|
||||||
}
|
* Add header to response
|
||||||
|
* @param string $value
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function header($value)
|
||||||
|
{
|
||||||
|
header($value);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add multiple headers to response
|
||||||
|
* @param array $headers
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function headers(array $headers)
|
||||||
|
{
|
||||||
|
$max = count($headers);
|
||||||
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
$this->header($headers[$i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,11 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
interface IRouterBootManager
|
interface IRouterBootManager
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Called when router loads it's routes
|
* Called when router loads it's routes
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
public function boot(Request $request);
|
public function boot(Request $request);
|
||||||
}
|
}
|
||||||
@@ -3,34 +3,34 @@ namespace Pecee\SimpleRouter\Route;
|
|||||||
|
|
||||||
interface IControllerRoute extends IRoute
|
interface IControllerRoute extends IRoute
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Get controller class-name
|
* Get controller class-name
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getController();
|
public function getController();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set controller class-name
|
* Set controller class-name
|
||||||
*
|
*
|
||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setController($controller);
|
public function setController($controller);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return active method
|
* Return active method
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getMethod();
|
public function getMethod();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set active method
|
* Set active method
|
||||||
*
|
*
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setMethod($method);
|
public function setMethod($method);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,56 +5,56 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
interface IGroupRoute extends IRoute
|
interface IGroupRoute extends IRoute
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Method called to check if a domain matches
|
* Method called to check if a domain matches
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function matchDomain(Request $request);
|
public function matchDomain(Request $request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set exception-handlers for group
|
* Set exception-handlers for group
|
||||||
*
|
*
|
||||||
* @param array $handlers
|
* @param array $handlers
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setExceptionHandlers(array $handlers);
|
public function setExceptionHandlers(array $handlers);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get exception-handlers for group
|
* Get exception-handlers for group
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getExceptionHandlers();
|
public function getExceptionHandlers();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get domains for domain.
|
* Get domains for domain.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getDomains();
|
public function getDomains();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set allowed domains for group.
|
* Set allowed domains for group.
|
||||||
*
|
*
|
||||||
* @param array $domains
|
* @param array $domains
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setDomains(array $domains);
|
public function setDomains(array $domains);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set prefix that child-routes will inherit.
|
* Set prefix that child-routes will inherit.
|
||||||
*
|
*
|
||||||
* @param string $prefix
|
* @param string $prefix
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function setPrefix($prefix);
|
public function setPrefix($prefix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get prefix.
|
* Get prefix.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getPrefix();
|
public function getPrefix();
|
||||||
}
|
}
|
||||||
@@ -5,50 +5,50 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
interface ILoadableRoute extends IRoute
|
interface ILoadableRoute extends IRoute
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Find url that matches method, parameters or name.
|
* Find url that matches method, parameters or name.
|
||||||
* Used when calling the url() helper.
|
* Used when calling the url() helper.
|
||||||
*
|
*
|
||||||
* @param string|null $method
|
* @param string|null $method
|
||||||
* @param array|null $parameters
|
* @param array|null $parameters
|
||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function findUrl($method = null, $parameters = null, $name = null);
|
public function findUrl($method = null, $parameters = null, $name = null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads and renders middlewares-classes
|
* Loads and renders middlewares-classes
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param ILoadableRoute $route
|
* @param ILoadableRoute $route
|
||||||
*/
|
*/
|
||||||
public function loadMiddleware(Request $request, ILoadableRoute &$route);
|
public function loadMiddleware(Request $request, ILoadableRoute &$route);
|
||||||
|
|
||||||
public function getUrl();
|
public function getUrl();
|
||||||
|
|
||||||
public function setUrl($url);
|
public function setUrl($url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the provided name for the router.
|
* Returns the provided name for the router.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName();
|
public function getName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if route has given name.
|
* Check if route has given name.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasName($name);
|
public function hasName($name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setName($name);
|
public function setName($name);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,195 +5,195 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
interface IRoute
|
interface IRoute
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Method called to check if a domain matches
|
* Method called to check if a domain matches
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function matchRoute(Request $request);
|
public function matchRoute(Request $request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when route is matched.
|
* Called when route is matched.
|
||||||
* Returns class to be rendered.
|
* Returns class to be rendered.
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
public function renderRoute(Request $request);
|
public function renderRoute(Request $request);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns callback name/identifier for the current route based on the callback.
|
* Returns callback name/identifier for the current route based on the callback.
|
||||||
* Useful if you need to get a unique identifier for the loaded route, for instance
|
* Useful if you need to get a unique identifier for the loaded route, for instance
|
||||||
* when using translations etc.
|
* when using translations etc.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getIdentifier();
|
public function getIdentifier();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set allowed request methods
|
* Set allowed request methods
|
||||||
*
|
*
|
||||||
* @param array $methods
|
* @param array $methods
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setRequestMethods(array $methods);
|
public function setRequestMethods(array $methods);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get allowed request methods
|
* Get allowed request methods
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getRequestMethods();
|
public function getRequestMethods();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return IRoute|null
|
* @return IRoute|null
|
||||||
*/
|
*/
|
||||||
public function getParent();
|
public function getParent();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the group for the route.
|
* Get the group for the route.
|
||||||
*
|
*
|
||||||
* @return IGroupRoute|null
|
* @return IGroupRoute|null
|
||||||
*/
|
*/
|
||||||
public function getGroup();
|
public function getGroup();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set group
|
* Set group
|
||||||
*
|
*
|
||||||
* @param IGroupRoute $group
|
* @param IGroupRoute $group
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setGroup(IGroupRoute $group);
|
public function setGroup(IGroupRoute $group);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set parent route
|
* Set parent route
|
||||||
*
|
*
|
||||||
* @param IRoute $parent
|
* @param IRoute $parent
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setParent(IRoute $parent);
|
public function setParent(IRoute $parent);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set callback
|
* Set callback
|
||||||
*
|
*
|
||||||
* @param string $callback
|
* @param string $callback
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setCallback($callback);
|
public function setCallback($callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCallback();
|
public function getCallback();
|
||||||
|
|
||||||
public function getMethod();
|
public function getMethod();
|
||||||
|
|
||||||
public function getClass();
|
public function getClass();
|
||||||
|
|
||||||
public function setMethod($method);
|
public function setMethod($method);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setNamespace($namespace);
|
public function setNamespace($namespace);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getNamespace();
|
public function getNamespace();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $namespace
|
* @param string $namespace
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setDefaultNamespace($namespace);
|
public function setDefaultNamespace($namespace);
|
||||||
|
|
||||||
public function getDefaultNamespace();
|
public function getDefaultNamespace();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get regular expression match used for matching route (if defined).
|
* Get regular expression match used for matching route (if defined).
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getMatch();
|
public function getMatch();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add regular expression match for the entire route.
|
* Add regular expression match for the entire route.
|
||||||
*
|
*
|
||||||
* @param string $regex
|
* @param string $regex
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setMatch($regex);
|
public function setMatch($regex);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get parameter names.
|
* Get parameter names.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getWhere();
|
public function getWhere();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set parameter names.
|
* Set parameter names.
|
||||||
*
|
*
|
||||||
* @param array $options
|
* @param array $options
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setWhere(array $options);
|
public function setWhere(array $options);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get parameters
|
* Get parameters
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getParameters();
|
public function getParameters();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get parameters
|
* Get parameters
|
||||||
*
|
*
|
||||||
* @param array $parameters
|
* @param array $parameters
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setParameters(array $parameters);
|
public function setParameters(array $parameters);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge with information from another route.
|
* Merge with information from another route.
|
||||||
*
|
*
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @param bool $merge
|
* @param bool $merge
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setSettings(array $settings, $merge = false);
|
public function setSettings(array $settings, $merge = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export route settings to array so they can be merged with another route.
|
* Export route settings to array so they can be merged with another route.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function toArray();
|
public function toArray();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get middlewares array
|
* Get middlewares array
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getMiddlewares();
|
public function getMiddlewares();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set middleware class-name
|
* Set middleware class-name
|
||||||
*
|
*
|
||||||
* @param string $middleware
|
* @param string $middleware
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setMiddleware($middleware);
|
public function setMiddleware($middleware);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set middlewares array
|
* Set middlewares array
|
||||||
*
|
*
|
||||||
* @param array $middlewares
|
* @param array $middlewares
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setMiddlewares(array $middlewares);
|
public function setMiddlewares(array $middlewares);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,198 +7,198 @@ use Pecee\SimpleRouter\Exceptions\HttpException;
|
|||||||
|
|
||||||
abstract class LoadableRoute extends Route implements ILoadableRoute
|
abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||||
{
|
{
|
||||||
const PARAMETERS_REGEX_MATCH = '%s([\w\-\_]*?)\%s{0,1}%s';
|
const PARAMETERS_REGEX_MATCH = '%s([\w\-\_]*?)\%s{0,1}%s';
|
||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $name;
|
protected $name;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads and renders middlewares-classes
|
* Loads and renders middlewares-classes
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param ILoadableRoute $route
|
* @param ILoadableRoute $route
|
||||||
* @throws HttpException
|
* @throws HttpException
|
||||||
*/
|
*/
|
||||||
public function loadMiddleware(Request $request, ILoadableRoute &$route)
|
public function loadMiddleware(Request $request, ILoadableRoute &$route)
|
||||||
{
|
{
|
||||||
if (count($this->getMiddlewares()) > 0) {
|
if (count($this->getMiddlewares()) > 0) {
|
||||||
|
|
||||||
$max = count($this->getMiddlewares());
|
$max = count($this->getMiddlewares());
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
|
||||||
$middleware = $this->getMiddlewares()[$i];
|
$middleware = $this->getMiddlewares()[$i];
|
||||||
|
|
||||||
$middleware = $this->loadClass($middleware);
|
$middleware = $this->loadClass($middleware);
|
||||||
if (!($middleware instanceof IMiddleware)) {
|
if (!($middleware instanceof IMiddleware)) {
|
||||||
throw new HttpException($middleware . ' must be instance of Middleware');
|
throw new HttpException($middleware . ' must be instance of Middleware');
|
||||||
}
|
}
|
||||||
|
|
||||||
$middleware->handle($request, $route);
|
$middleware->handle($request, $route);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set url
|
* Set url
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setUrl($url)
|
public function setUrl($url)
|
||||||
{
|
{
|
||||||
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
|
$this->url = ($url === '/') ? '/' : '/' . trim($url, '/') . '/';
|
||||||
|
|
||||||
if(strpos($this->url, $this->paramModifiers[0]) !== false) {
|
if (strpos($this->url, $this->paramModifiers[0]) !== false) {
|
||||||
|
|
||||||
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
|
$regex = sprintf(static::PARAMETERS_REGEX_MATCH, $this->paramModifiers[0], $this->paramOptionalSymbol, $this->paramModifiers[1]);
|
||||||
|
|
||||||
if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) {
|
if (preg_match_all('/' . $regex . '/is', $this->url, $matches)) {
|
||||||
|
|
||||||
$max = count($matches[1]);
|
$max = count($matches[1]);
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
for ($i = 0; $i < $max; $i++) {
|
||||||
$this->parameters[$matches[1][$i]] = null;
|
$this->parameters[$matches[1][$i]] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUrl()
|
public function getUrl()
|
||||||
{
|
{
|
||||||
return $this->url;
|
return $this->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find url that matches method, parameters or name.
|
* Find url that matches method, parameters or name.
|
||||||
* Used when calling the url() helper.
|
* Used when calling the url() helper.
|
||||||
*
|
*
|
||||||
* @param string|null $method
|
* @param string|null $method
|
||||||
* @param array|null $parameters
|
* @param array|null $parameters
|
||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function findUrl($method = null, $parameters = null, $name = null)
|
public function findUrl($method = null, $parameters = null, $name = null)
|
||||||
{
|
{
|
||||||
$url = '';
|
$url = '';
|
||||||
|
|
||||||
$parameters = (array)$parameters;
|
$parameters = (array)$parameters;
|
||||||
|
|
||||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
$url .= $this->getUrl();
|
$url .= $this->getUrl();
|
||||||
|
|
||||||
$params = array_merge($this->getParameters(), $parameters);
|
$params = array_merge($this->getParameters(), $parameters);
|
||||||
|
|
||||||
/* Url that contains parameters that aren't recognized */
|
/* Url that contains parameters that aren't recognized */
|
||||||
$unknownParams = [];
|
$unknownParams = [];
|
||||||
|
|
||||||
/* Create the param string - {} */
|
/* Create the param string - {} */
|
||||||
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
||||||
|
|
||||||
/* Create the param string with the optional symbol - {?} */
|
/* Create the param string with the optional symbol - {?} */
|
||||||
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
||||||
|
|
||||||
/* Let's parse the values of any {} parameter in the url */
|
/* Let's parse the values of any {} parameter in the url */
|
||||||
|
|
||||||
$max = count($params) - 1;
|
$max = count($params) - 1;
|
||||||
$keys = array_keys($params);
|
$keys = array_keys($params);
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
$param = $keys[$i];
|
$param = $keys[$i];
|
||||||
$value = $params[$param];
|
$value = $params[$param];
|
||||||
|
|
||||||
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
$value = (isset($parameters[$param])) ? $parameters[$param] : $value;
|
||||||
|
|
||||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||||
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
||||||
} else {
|
} else {
|
||||||
$unknownParams[$param] = $value;
|
$unknownParams[$param] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$url .= join('/', $unknownParams);
|
$url .= join('/', $unknownParams);
|
||||||
|
|
||||||
return rtrim($url, '/') . '/';
|
return rtrim($url, '/') . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the provided name for the router.
|
* Returns the provided name for the router.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if route has given name.
|
* Check if route has given name.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasName($name)
|
public function hasName($name)
|
||||||
{
|
{
|
||||||
return (strtolower($this->name) === strtolower($name));
|
return (strtolower($this->name) === strtolower($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||||
* Alias for LoadableRoute::setName().
|
* Alias for LoadableRoute::setName().
|
||||||
*
|
*
|
||||||
* @see LoadableRoute::setName()
|
* @see LoadableRoute::setName()
|
||||||
* @param string|array $name
|
* @param string|array $name
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function name($name)
|
public function name($name)
|
||||||
{
|
{
|
||||||
return $this->setName($name);
|
return $this->setName($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
* Sets the router name, which makes it easier to obtain the url or router at a later point.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge with information from another route.
|
* Merge with information from another route.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @param bool $merge
|
* @param bool $merge
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setSettings(array $values, $merge = false)
|
public function setSettings(array $values, $merge = false)
|
||||||
{
|
{
|
||||||
if (isset($values['as'])) {
|
if (isset($values['as'])) {
|
||||||
if ($this->name !== null && $merge !== false) {
|
if ($this->name !== null && $merge !== false) {
|
||||||
$this->setName($values['as'] . '.' . $this->name);
|
$this->setName($values['as'] . '.' . $this->name);
|
||||||
} else {
|
} else {
|
||||||
$this->setName($values['as']);
|
$this->setName($values['as']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['prefix'])) {
|
if (isset($values['prefix'])) {
|
||||||
$this->setUrl($values['prefix'] . $this->getUrl());
|
$this->setUrl($values['prefix'] . $this->getUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::setSettings($values, $merge);
|
parent::setSettings($values, $merge);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -6,201 +6,201 @@ use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
|||||||
|
|
||||||
class RouteController extends LoadableRoute implements IControllerRoute
|
class RouteController extends LoadableRoute implements IControllerRoute
|
||||||
{
|
{
|
||||||
protected $defaultMethod = 'index';
|
protected $defaultMethod = 'index';
|
||||||
protected $controller;
|
protected $controller;
|
||||||
protected $method;
|
protected $method;
|
||||||
protected $names = [];
|
protected $names = [];
|
||||||
|
|
||||||
public function __construct($url, $controller)
|
public function __construct($url, $controller)
|
||||||
{
|
{
|
||||||
$this->setUrl($url);
|
$this->setUrl($url);
|
||||||
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if route has given name.
|
* Check if route has given name.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasName($name)
|
public function hasName($name)
|
||||||
{
|
{
|
||||||
if ($this->name === null) {
|
if ($this->name === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove method/type */
|
/* Remove method/type */
|
||||||
if (stripos($name, '.') !== false) {
|
if (stripos($name, '.') !== false) {
|
||||||
$method = substr($name, strrpos($name, '.') + 1);
|
$method = substr($name, strrpos($name, '.') + 1);
|
||||||
$newName = substr($name, 0, strrpos($name, '.'));
|
$newName = substr($name, 0, strrpos($name, '.'));
|
||||||
|
|
||||||
if (strtolower($this->name) === strtolower($newName) && in_array($method, $this->names)) {
|
if (strtolower($this->name) === strtolower($newName) && in_array($method, $this->names)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::hasName($name);
|
return parent::hasName($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findUrl($method = null, $parameters = null, $name = null)
|
public function findUrl($method = null, $parameters = null, $name = null)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (stripos($name, '.') !== false) {
|
if (stripos($name, '.') !== false) {
|
||||||
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names);
|
$found = array_search(substr($name, strrpos($name, '.') + 1), $this->names);
|
||||||
if ($found !== false) {
|
if ($found !== false) {
|
||||||
$method = $found;
|
$method = $found;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = '';
|
$url = '';
|
||||||
|
|
||||||
$parameters = (array)$parameters;
|
$parameters = (array)$parameters;
|
||||||
|
|
||||||
/* Remove requestType from method-name, if it exists */
|
/* Remove requestType from method-name, if it exists */
|
||||||
if ($method !== null) {
|
if ($method !== null) {
|
||||||
|
|
||||||
$max = count(static::$requestTypes);
|
$max = count(static::$requestTypes);
|
||||||
|
|
||||||
for ($i = 0; $i < $max; $i++) {
|
for ($i = 0; $i < $max; $i++) {
|
||||||
|
|
||||||
$requestType = static::$requestTypes[$i];
|
$requestType = static::$requestTypes[$i];
|
||||||
|
|
||||||
if (stripos($method, $requestType) === 0) {
|
if (stripos($method, $requestType) === 0) {
|
||||||
$method = substr($method, strlen($requestType));
|
$method = substr($method, strlen($requestType));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$method .= '/';
|
$method .= '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
|
$url .= '/' . trim($this->getUrl(), '/') . '/' . strtolower($method) . join('/', $parameters);
|
||||||
|
|
||||||
return '/' . trim($url, '/') . '/';
|
return '/' . trim($url, '/') . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderRoute(Request $request)
|
public function renderRoute(Request $request)
|
||||||
{
|
{
|
||||||
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
||||||
|
|
||||||
// When the callback is a function
|
// When the callback is a function
|
||||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||||
} else {
|
} else {
|
||||||
// When the callback is a method
|
// When the callback is a method
|
||||||
$controller = explode('@', $this->getCallback());
|
$controller = explode('@', $this->getCallback());
|
||||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||||
|
|
||||||
$class = $this->loadClass($className);
|
$class = $this->loadClass($className);
|
||||||
$method = $request->getMethod() . ucfirst($controller[1]);
|
$method = $request->getMethod() . ucfirst($controller[1]);
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func_array([$class, $method], $this->getParameters());
|
call_user_func_array([$class, $method], $this->getParameters());
|
||||||
|
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function matchRoute(Request $request)
|
public function matchRoute(Request $request)
|
||||||
{
|
{
|
||||||
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
||||||
$url = rtrim($url, '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
|
|
||||||
if (strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) {
|
if (strtolower($url) == strtolower($this->url) || stripos($url, $this->url) === 0) {
|
||||||
|
|
||||||
$strippedUrl = trim(str_ireplace($this->url, '/', $url), '/');
|
$strippedUrl = trim(str_ireplace($this->url, '/', $url), '/');
|
||||||
|
|
||||||
$path = explode('/', $strippedUrl);
|
$path = explode('/', $strippedUrl);
|
||||||
|
|
||||||
if (count($path) > 0) {
|
if (count($path) > 0) {
|
||||||
|
|
||||||
$method = (!isset($path[0]) || trim($path[0]) === '') ? $this->defaultMethod : $path[0];
|
$method = (!isset($path[0]) || trim($path[0]) === '') ? $this->defaultMethod : $path[0];
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
|
|
||||||
array_shift($path);
|
array_shift($path);
|
||||||
$this->parameters = $path;
|
$this->parameters = $path;
|
||||||
|
|
||||||
// Set callback
|
// Set callback
|
||||||
$this->setCallback($this->controller . '@' . $this->method);
|
$this->setCallback($this->controller . '@' . $this->method);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get controller class-name.
|
* Get controller class-name.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getController()
|
public function getController()
|
||||||
{
|
{
|
||||||
return $this->controller;
|
return $this->controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get controller class-name.
|
* Get controller class-name.
|
||||||
*
|
*
|
||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setController($controller)
|
public function setController($controller)
|
||||||
{
|
{
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return active method
|
* Return active method
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getMethod()
|
public function getMethod()
|
||||||
{
|
{
|
||||||
return $this->method;
|
return $this->method;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set active method
|
* Set active method
|
||||||
*
|
*
|
||||||
* @param string $method
|
* @param string $method
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setMethod($method)
|
public function setMethod($method)
|
||||||
{
|
{
|
||||||
$this->method = $method;
|
$this->method = $method;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge with information from another route.
|
* Merge with information from another route.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @param bool $merge
|
* @param bool $merge
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setSettings(array $values, $merge = false)
|
public function setSettings(array $values, $merge = false)
|
||||||
{
|
{
|
||||||
if (isset($values['names'])) {
|
if (isset($values['names'])) {
|
||||||
$this->names = $values['names'];
|
$this->names = $values['names'];
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::setSettings($values, $merge);
|
parent::setSettings($values, $merge);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,177 +5,177 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class RouteGroup extends Route implements IGroupRoute
|
class RouteGroup extends Route implements IGroupRoute
|
||||||
{
|
{
|
||||||
protected $prefix;
|
protected $prefix;
|
||||||
protected $name;
|
protected $name;
|
||||||
protected $domains = [];
|
protected $domains = [];
|
||||||
protected $exceptionHandlers = [];
|
protected $exceptionHandlers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to check if a domain matches
|
* Method called to check if a domain matches
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function matchDomain(Request $request)
|
public function matchDomain(Request $request)
|
||||||
{
|
{
|
||||||
if (count($this->domains) > 0) {
|
if (count($this->domains) > 0) {
|
||||||
|
|
||||||
$max = count($this->domains) - 1;
|
$max = count($this->domains) - 1;
|
||||||
|
|
||||||
for ($i = $max; $i >= 0; $i--) {
|
for ($i = $max; $i >= 0; $i--) {
|
||||||
|
|
||||||
$domain = $this->domains[$i];
|
$domain = $this->domains[$i];
|
||||||
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
|
$parameters = $this->parseParameters($domain, $request->getHost(), '.*');
|
||||||
|
|
||||||
if ($parameters !== null) {
|
if ($parameters !== null) {
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called to check if route matches
|
* Method called to check if route matches
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function matchRoute(Request $request)
|
public function matchRoute(Request $request)
|
||||||
{
|
{
|
||||||
// Skip if prefix doesn't match
|
// Skip if prefix doesn't match
|
||||||
if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
|
if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->matchDomain($request);
|
return $this->matchDomain($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set exception-handlers for group
|
* Set exception-handlers for group
|
||||||
*
|
*
|
||||||
* @param array $handlers
|
* @param array $handlers
|
||||||
* @return static $this
|
* @return static $this
|
||||||
*/
|
*/
|
||||||
public function setExceptionHandlers(array $handlers)
|
public function setExceptionHandlers(array $handlers)
|
||||||
{
|
{
|
||||||
$this->exceptionHandlers = $handlers;
|
$this->exceptionHandlers = $handlers;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get exception-handlers for group
|
* Get exception-handlers for group
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getExceptionHandlers()
|
public function getExceptionHandlers()
|
||||||
{
|
{
|
||||||
return $this->exceptionHandlers;
|
return $this->exceptionHandlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get allowed domains for domain.
|
* Get allowed domains for domain.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getDomains()
|
public function getDomains()
|
||||||
{
|
{
|
||||||
return $this->domains;
|
return $this->domains;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set allowed domains for group.
|
* Set allowed domains for group.
|
||||||
*
|
*
|
||||||
* @param array $domains
|
* @param array $domains
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function setDomains(array $domains)
|
public function setDomains(array $domains)
|
||||||
{
|
{
|
||||||
$this->domains = $domains;
|
$this->domains = $domains;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $prefix
|
* @param string $prefix
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setPrefix($prefix)
|
public function setPrefix($prefix)
|
||||||
{
|
{
|
||||||
$this->prefix = '/' . trim($prefix, '/');
|
$this->prefix = '/' . trim($prefix, '/');
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set prefix that child-routes will inherit.
|
* Set prefix that child-routes will inherit.
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getPrefix()
|
public function getPrefix()
|
||||||
{
|
{
|
||||||
return $this->prefix;
|
return $this->prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge with information from another route.
|
* Merge with information from another route.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @param bool $merge
|
* @param bool $merge
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setSettings(array $values, $merge = false)
|
public function setSettings(array $values, $merge = false)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isset($values['prefix'])) {
|
if (isset($values['prefix'])) {
|
||||||
$this->setPrefix($values['prefix'] . $this->prefix);
|
$this->setPrefix($values['prefix'] . $this->prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['exceptionHandler'])) {
|
if (isset($values['exceptionHandler'])) {
|
||||||
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
$this->setExceptionHandlers((array)$values['exceptionHandler']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['domain'])) {
|
if (isset($values['domain'])) {
|
||||||
$this->setDomains((array)$values['domain']);
|
$this->setDomains((array)$values['domain']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($values['as'])) {
|
if (isset($values['as'])) {
|
||||||
if ($this->name !== null && $merge !== false) {
|
if ($this->name !== null && $merge !== false) {
|
||||||
$this->name = $values['as'] . '.' . $this->name;
|
$this->name = $values['as'] . '.' . $this->name;
|
||||||
} else {
|
} else {
|
||||||
$this->name = $values['as'];
|
$this->name = $values['as'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::setSettings($values, $merge);
|
parent::setSettings($values, $merge);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Export route settings to array so they can be merged with another route.
|
* Export route settings to array so they can be merged with another route.
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function toArray()
|
public function toArray()
|
||||||
{
|
{
|
||||||
$values = [];
|
$values = [];
|
||||||
|
|
||||||
if ($this->prefix !== null) {
|
if ($this->prefix !== null) {
|
||||||
$values['prefix'] = $this->getPrefix();
|
$values['prefix'] = $this->getPrefix();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->name !== null) {
|
if ($this->name !== null) {
|
||||||
$values['as'] = $this->name;
|
$values['as'] = $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
return array_merge($values, parent::toArray());
|
return array_merge($values, parent::toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -6,198 +6,198 @@ use Pecee\SimpleRouter\Exceptions\NotFoundHttpException;
|
|||||||
|
|
||||||
class RouteResource extends LoadableRoute implements IControllerRoute
|
class RouteResource extends LoadableRoute implements IControllerRoute
|
||||||
{
|
{
|
||||||
protected $urls = [
|
protected $urls = [
|
||||||
'index' => '',
|
'index' => '',
|
||||||
'create' => 'create',
|
'create' => 'create',
|
||||||
'store' => '',
|
'store' => '',
|
||||||
'show' => '',
|
'show' => '',
|
||||||
'edit' => 'edit',
|
'edit' => 'edit',
|
||||||
'update' => '',
|
'update' => '',
|
||||||
'destroy' => '',
|
'destroy' => '',
|
||||||
];
|
];
|
||||||
protected $names = [];
|
protected $names = [];
|
||||||
protected $controller;
|
protected $controller;
|
||||||
|
|
||||||
public function __construct($url, $controller)
|
public function __construct($url, $controller)
|
||||||
{
|
{
|
||||||
$this->setUrl($url);
|
$this->setUrl($url);
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
$this->setName(trim(str_replace('/', '.', $url), '/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if route has given name.
|
* Check if route has given name.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasName($name)
|
public function hasName($name)
|
||||||
{
|
{
|
||||||
if ($this->name === null) {
|
if ($this->name === null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strtolower($this->name) === strtolower($name)) {
|
if (strtolower($this->name) === strtolower($name)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Remove method/type */
|
/* Remove method/type */
|
||||||
if (stripos($name, '.') !== false) {
|
if (stripos($name, '.') !== false) {
|
||||||
$name = substr($name, 0, strrpos($name, '.'));
|
$name = substr($name, 0, strrpos($name, '.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (strtolower($this->name) === strtolower($name));
|
return (strtolower($this->name) === strtolower($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findUrl($method = null, $parameters = null, $name = null)
|
public function findUrl($method = null, $parameters = null, $name = null)
|
||||||
{
|
{
|
||||||
$method = array_search($name, $this->names);
|
$method = array_search($name, $this->names);
|
||||||
if ($method !== false) {
|
if ($method !== false) {
|
||||||
return rtrim($this->url . $this->urls[$method], '/') . '/';
|
return rtrim($this->url . $this->urls[$method], '/') . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->url;
|
return $this->url;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function renderRoute(Request $request)
|
public function renderRoute(Request $request)
|
||||||
{
|
{
|
||||||
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
if ($this->getCallback() !== null && is_callable($this->getCallback())) {
|
||||||
// When the callback is a function
|
// When the callback is a function
|
||||||
call_user_func_array($this->getCallback(), $this->getParameters());
|
call_user_func_array($this->getCallback(), $this->getParameters());
|
||||||
} else {
|
} else {
|
||||||
// When the callback is a method
|
// When the callback is a method
|
||||||
$controller = explode('@', $this->getCallback());
|
$controller = explode('@', $this->getCallback());
|
||||||
$className = $this->getNamespace() . '\\' . $controller[0];
|
$className = $this->getNamespace() . '\\' . $controller[0];
|
||||||
$class = $this->loadClass($className);
|
$class = $this->loadClass($className);
|
||||||
$method = strtolower($controller[1]);
|
$method = strtolower($controller[1]);
|
||||||
|
|
||||||
if (!method_exists($class, $method)) {
|
if (!method_exists($class, $method)) {
|
||||||
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
throw new NotFoundHttpException(sprintf('Method %s does not exist in class %s', $method, $className), 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
call_user_func_array([$class, $method], $this->getParameters());
|
call_user_func_array([$class, $method], $this->getParameters());
|
||||||
|
|
||||||
return $class;
|
return $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function call($method, $parameters)
|
protected function call($method, $parameters)
|
||||||
{
|
{
|
||||||
$this->setCallback($this->controller . '@' . $method);
|
$this->setCallback($this->controller . '@' . $method);
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function matchRoute(Request $request)
|
public function matchRoute(Request $request)
|
||||||
{
|
{
|
||||||
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
||||||
$url = rtrim($url, '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
|
|
||||||
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
$route = rtrim($this->url, '/') . '/{id?}/{action?}';
|
||||||
|
|
||||||
$parameters = $this->parseParameters($route, $url);
|
$parameters = $this->parseParameters($route, $url);
|
||||||
|
|
||||||
if ($parameters !== null) {
|
if ($parameters !== null) {
|
||||||
|
|
||||||
$parameters = array_merge($this->parameters, (array)$parameters);
|
$parameters = array_merge($this->parameters, (array)$parameters);
|
||||||
|
|
||||||
$action = isset($parameters['action']) ? $parameters['action'] : null;
|
$action = isset($parameters['action']) ? $parameters['action'] : null;
|
||||||
unset($parameters['action']);
|
unset($parameters['action']);
|
||||||
|
|
||||||
$method = $request->getMethod();
|
$method = $request->getMethod();
|
||||||
|
|
||||||
// Delete
|
// Delete
|
||||||
if (isset($parameters['id']) && $method === static::REQUEST_TYPE_DELETE) {
|
if (isset($parameters['id']) && $method === static::REQUEST_TYPE_DELETE) {
|
||||||
return $this->call('destroy', $parameters);
|
return $this->call('destroy', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update
|
// Update
|
||||||
if (isset($parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT])) {
|
if (isset($parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT])) {
|
||||||
return $this->call('update', $parameters);
|
return $this->call('update', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit
|
// Edit
|
||||||
if (isset($parameters['id']) && strtolower($action) === 'edit' && $method === static::REQUEST_TYPE_GET) {
|
if (isset($parameters['id']) && strtolower($action) === 'edit' && $method === static::REQUEST_TYPE_GET) {
|
||||||
return $this->call('edit', $parameters);
|
return $this->call('edit', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
if (strtolower($action) === 'create' && $method === static::REQUEST_TYPE_GET) {
|
if (strtolower($action) === 'create' && $method === static::REQUEST_TYPE_GET) {
|
||||||
return $this->call('create', $parameters);
|
return $this->call('create', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
if ($method === static::REQUEST_TYPE_POST) {
|
if ($method === static::REQUEST_TYPE_POST) {
|
||||||
return $this->call('store', $parameters);
|
return $this->call('store', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Show
|
// Show
|
||||||
if (isset($parameters['id']) && $method === static::REQUEST_TYPE_GET) {
|
if (isset($parameters['id']) && $method === static::REQUEST_TYPE_GET) {
|
||||||
return $this->call('show', $parameters);
|
return $this->call('show', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index
|
// Index
|
||||||
return $this->call('index', $parameters);
|
return $this->call('index', $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getController()
|
public function getController()
|
||||||
{
|
{
|
||||||
return $this->controller;
|
return $this->controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setController($controller)
|
public function setController($controller)
|
||||||
{
|
{
|
||||||
$this->controller = $controller;
|
$this->controller = $controller;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setName($name)
|
public function setName($name)
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
|
||||||
$this->names = [
|
$this->names = [
|
||||||
'index' => $this->name . '.index',
|
'index' => $this->name . '.index',
|
||||||
'create' => $this->name . '.create',
|
'create' => $this->name . '.create',
|
||||||
'store' => $this->name . '.store',
|
'store' => $this->name . '.store',
|
||||||
'show' => $this->name . '.show',
|
'show' => $this->name . '.show',
|
||||||
'edit' => $this->name . '.edit',
|
'edit' => $this->name . '.edit',
|
||||||
'update' => $this->name . '.update',
|
'update' => $this->name . '.update',
|
||||||
'destroy' => $this->name . '.destroy',
|
'destroy' => $this->name . '.destroy',
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Merge with information from another route.
|
* Merge with information from another route.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @param bool $merge
|
* @param bool $merge
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public function setSettings(array $values, $merge = false)
|
public function setSettings(array $values, $merge = false)
|
||||||
{
|
{
|
||||||
if (isset($values['names'])) {
|
if (isset($values['names'])) {
|
||||||
$this->names = $values['names'];
|
$this->names = $values['names'];
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::setSettings($values, $merge);
|
parent::setSettings($values, $merge);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,45 +5,45 @@ use Pecee\Http\Request;
|
|||||||
|
|
||||||
class RouteUrl extends LoadableRoute
|
class RouteUrl extends LoadableRoute
|
||||||
{
|
{
|
||||||
public function __construct($url, $callback)
|
public function __construct($url, $callback)
|
||||||
{
|
{
|
||||||
$this->setUrl($url);
|
$this->setUrl($url);
|
||||||
$this->setCallback($callback);
|
$this->setCallback($callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function matchRoute(Request $request)
|
public function matchRoute(Request $request)
|
||||||
{
|
{
|
||||||
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
$url = parse_url(urldecode($request->getUri()), PHP_URL_PATH);
|
||||||
$url = rtrim($url, '/') . '/';
|
$url = rtrim($url, '/') . '/';
|
||||||
|
|
||||||
// Match on custom defined regular expression
|
// Match on custom defined regular expression
|
||||||
if ($this->regex !== null) {
|
if ($this->regex !== null) {
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
if (preg_match($this->regex, $request->getHost() . $url, $parameters)) {
|
if (preg_match($this->regex, $request->getHost() . $url, $parameters)) {
|
||||||
/* Remove global match */
|
/* Remove global match */
|
||||||
if(count($parameters) > 1) {
|
if (count($parameters) > 1) {
|
||||||
array_shift($parameters);
|
array_shift($parameters);
|
||||||
$this->parameters = $parameters;
|
$this->parameters = $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make regular expression based on route
|
// Make regular expression based on route
|
||||||
$route = rtrim($this->url, '/') . '/';
|
$route = rtrim($this->url, '/') . '/';
|
||||||
|
|
||||||
$parameters = $this->parseParameters($route, $url);
|
$parameters = $this->parseParameters($route, $url);
|
||||||
|
|
||||||
if ($parameters !== null) {
|
if ($parameters !== null) {
|
||||||
$this->parameters = array_merge($this->parameters, $parameters);
|
$this->parameters = array_merge($this->parameters, $parameters);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+479
-477
File diff suppressed because it is too large
Load Diff
@@ -21,354 +21,354 @@ use Pecee\SimpleRouter\Route\RouteUrl;
|
|||||||
|
|
||||||
class SimpleRouter
|
class SimpleRouter
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Default namespace added to all routes
|
* Default namespace added to all routes
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected static $defaultNamespace;
|
protected static $defaultNamespace;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The response object
|
* The response object
|
||||||
* @var Response
|
* @var Response
|
||||||
*/
|
*/
|
||||||
protected static $response;
|
protected static $response;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start/route request
|
* Start/route request
|
||||||
*
|
*
|
||||||
* @throws HttpException
|
* @throws HttpException
|
||||||
* @throws NotFoundHttpException
|
* @throws NotFoundHttpException
|
||||||
*/
|
*/
|
||||||
public static function start()
|
public static function start()
|
||||||
{
|
{
|
||||||
static::router()->routeRequest();
|
static::router()->routeRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set default namespace which will be prepended to all routes.
|
* Set default namespace which will be prepended to all routes.
|
||||||
*
|
*
|
||||||
* @param string $defaultNamespace
|
* @param string $defaultNamespace
|
||||||
*/
|
*/
|
||||||
public static function setDefaultNamespace($defaultNamespace)
|
public static function setDefaultNamespace($defaultNamespace)
|
||||||
{
|
{
|
||||||
static::$defaultNamespace = $defaultNamespace;
|
static::$defaultNamespace = $defaultNamespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base CSRF verifier
|
* Base CSRF verifier
|
||||||
*
|
*
|
||||||
* @param BaseCsrfVerifier $baseCsrfVerifier
|
* @param BaseCsrfVerifier $baseCsrfVerifier
|
||||||
*/
|
*/
|
||||||
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier)
|
public static function csrfVerifier(BaseCsrfVerifier $baseCsrfVerifier)
|
||||||
{
|
{
|
||||||
static::router()->setCsrfVerifier($baseCsrfVerifier);
|
static::router()->setCsrfVerifier($baseCsrfVerifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Boot managers allows you to alter the routes before the routing occurs.
|
* Boot managers allows you to alter the routes before the routing occurs.
|
||||||
* Perfect if you want to load pretty-urls from a file or database.
|
* Perfect if you want to load pretty-urls from a file or database.
|
||||||
*
|
*
|
||||||
* @param IRouterBootManager $bootManager
|
* @param IRouterBootManager $bootManager
|
||||||
*/
|
*/
|
||||||
public static function addBootManager(IRouterBootManager $bootManager)
|
public static function addBootManager(IRouterBootManager $bootManager)
|
||||||
{
|
{
|
||||||
static::router()->addBootManager($bootManager);
|
static::router()->addBootManager($bootManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on GET request method.
|
* Route the given url to your callback on GET request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function get($url, $callback, array $settings = null)
|
public static function get($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['get'], $url, $callback, $settings);
|
return static::match(['get'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on POST request method.
|
* Route the given url to your callback on POST request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function post($url, $callback, array $settings = null)
|
public static function post($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['post'], $url, $callback, $settings);
|
return static::match(['post'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on PUT request method.
|
* Route the given url to your callback on PUT request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function put($url, $callback, array $settings = null)
|
public static function put($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['put'], $url, $callback, $settings);
|
return static::match(['put'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on PATCH request method.
|
* Route the given url to your callback on PATCH request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function patch($url, $callback, array $settings = null)
|
public static function patch($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['patch'], $url, $callback, $settings);
|
return static::match(['patch'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on OPTIONS request method.
|
* Route the given url to your callback on OPTIONS request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function options($url, $callback, array $settings = null)
|
public static function options($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['options'], $url, $callback, $settings);
|
return static::match(['options'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route the given url to your callback on DELETE request method.
|
* Route the given url to your callback on DELETE request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function delete($url, $callback, array $settings = null)
|
public static function delete($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['delete'], $url, $callback, $settings);
|
return static::match(['delete'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Groups allows for encapsulating routes with special settings.
|
* Groups allows for encapsulating routes with special settings.
|
||||||
*
|
*
|
||||||
* @param array $settings
|
* @param array $settings
|
||||||
* @param \Closure $callback
|
* @param \Closure $callback
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
* @return RouteGroup
|
* @return RouteGroup
|
||||||
*/
|
*/
|
||||||
public static function group(array $settings = [], \Closure $callback)
|
public static function group(array $settings = [], \Closure $callback)
|
||||||
{
|
{
|
||||||
$group = new RouteGroup();
|
$group = new RouteGroup();
|
||||||
$group->setCallback($callback);
|
$group->setCallback($callback);
|
||||||
$group->setSettings($settings);
|
$group->setSettings($settings);
|
||||||
|
|
||||||
if (is_callable($callback) === false) {
|
if (is_callable($callback) === false) {
|
||||||
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
throw new \InvalidArgumentException('Invalid callback provided. Only functions or methods supported');
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($group);
|
static::router()->addRoute($group);
|
||||||
|
|
||||||
return $group;
|
return $group;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Alias for the form method
|
* Alias for the form method
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
* @see SimpleRouter::form
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function basic($url, $callback, array $settings = null)
|
public static function basic($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['get', 'post'], $url, $callback, $settings);
|
return static::match(['get', 'post'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This type will route the given url to your callback on the provided request methods.
|
* This type will route the given url to your callback on the provided request methods.
|
||||||
* Route the given url to your callback on POST and GET request method.
|
* Route the given url to your callback on POST and GET request method.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @see SimpleRouter::form
|
* @see SimpleRouter::form
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function form($url, $callback, array $settings = null)
|
public static function form($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
return static::match(['get', 'post'], $url, $callback, $settings);
|
return static::match(['get', 'post'], $url, $callback, $settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This type will route the given url to your callback on the provided request methods.
|
* This type will route the given url to your callback on the provided request methods.
|
||||||
*
|
*
|
||||||
* @param array $requestMethods
|
* @param array $requestMethods
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function match(array $requestMethods, $url, $callback, array $settings = null)
|
public static function match(array $requestMethods, $url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteUrl($url, $callback);
|
$route = new RouteUrl($url, $callback);
|
||||||
$route->setRequestMethods($requestMethods);
|
$route->setRequestMethods($requestMethods);
|
||||||
$route = static::addDefaultNamespace($route);
|
$route = static::addDefaultNamespace($route);
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($route);
|
static::router()->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This type will route the given url to your callback and allow any type of request method
|
* This type will route the given url to your callback and allow any type of request method
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string|\Closure $callback
|
* @param string|\Closure $callback
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteUrl
|
* @return RouteUrl
|
||||||
*/
|
*/
|
||||||
public static function all($url, $callback, array $settings = null)
|
public static function all($url, $callback, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteUrl($url, $callback);
|
$route = new RouteUrl($url, $callback);
|
||||||
$route = static::addDefaultNamespace($route);
|
$route = static::addDefaultNamespace($route);
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($route);
|
static::router()->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This route will route request from the given url to the controller.
|
* This route will route request from the given url to the controller.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteController
|
* @return RouteController
|
||||||
*/
|
*/
|
||||||
public static function controller($url, $controller, array $settings = null)
|
public static function controller($url, $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteController($url, $controller);
|
$route = new RouteController($url, $controller);
|
||||||
$route = static::addDefaultNamespace($route);
|
$route = static::addDefaultNamespace($route);
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($route);
|
static::router()->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This type will route all REST-supported requests to different methods in the provided controller.
|
* This type will route all REST-supported requests to different methods in the provided controller.
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param string $controller
|
* @param string $controller
|
||||||
* @param array|null $settings
|
* @param array|null $settings
|
||||||
* @return RouteResource
|
* @return RouteResource
|
||||||
*/
|
*/
|
||||||
public static function resource($url, $controller, array $settings = null)
|
public static function resource($url, $controller, array $settings = null)
|
||||||
{
|
{
|
||||||
$route = new RouteResource($url, $controller);
|
$route = new RouteResource($url, $controller);
|
||||||
|
|
||||||
if ($settings !== null) {
|
if ($settings !== null) {
|
||||||
$route->setSettings($settings);
|
$route->setSettings($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
static::router()->addRoute($route);
|
static::router()->addRoute($route);
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get url for a route by using either name/alias, class or method name.
|
* Get url for a route by using either name/alias, class or method name.
|
||||||
*
|
*
|
||||||
* The name parameter supports the following values:
|
* The name parameter supports the following values:
|
||||||
* - Route name
|
* - Route name
|
||||||
* - Controller/resource name (with or without method)
|
* - Controller/resource name (with or without method)
|
||||||
* - Controller class name
|
* - Controller class name
|
||||||
*
|
*
|
||||||
* When searching for controller/resource by name, you can use this syntax "route.name@method".
|
* When searching for controller/resource by name, you can use this syntax "route.name@method".
|
||||||
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
|
* You can also use the same syntax when searching for a specific controller-class "MyController@home".
|
||||||
* If no arguments is specified, it will return the url for the current loaded route.
|
* If no arguments is specified, it will return the url for the current loaded route.
|
||||||
*
|
*
|
||||||
* @param string|null $name
|
* @param string|null $name
|
||||||
* @param string|array|null $parameters
|
* @param string|array|null $parameters
|
||||||
* @param array|null $getParams
|
* @param array|null $getParams
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
public static function getUrl($name = null, $parameters = null, $getParams = null)
|
||||||
{
|
{
|
||||||
return static::router()->getUrl($name, $parameters, $getParams);
|
return static::router()->getUrl($name, $parameters, $getParams);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the request
|
* Get the request
|
||||||
*
|
*
|
||||||
* @return \Pecee\Http\Request
|
* @return \Pecee\Http\Request
|
||||||
*/
|
*/
|
||||||
public static function request()
|
public static function request()
|
||||||
{
|
{
|
||||||
return static::router()->getRequest();
|
return static::router()->getRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the response object
|
* Get the response object
|
||||||
*
|
*
|
||||||
* @return Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public static function response()
|
public static function response()
|
||||||
{
|
{
|
||||||
if (static::$response === null) {
|
if (static::$response === null) {
|
||||||
static::$response = new Response(static::request());
|
static::$response = new Response(static::request());
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::$response;
|
return static::$response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the router instance
|
* Returns the router instance
|
||||||
*
|
*
|
||||||
* @return Router
|
* @return Router
|
||||||
*/
|
*/
|
||||||
public static function router()
|
public static function router()
|
||||||
{
|
{
|
||||||
return Router::getInstance();
|
return Router::getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepends the default namespace to all new routes added.
|
* Prepends the default namespace to all new routes added.
|
||||||
*
|
*
|
||||||
* @param IRoute $route
|
* @param IRoute $route
|
||||||
* @return IRoute
|
* @return IRoute
|
||||||
*/
|
*/
|
||||||
protected static function addDefaultNamespace(IRoute $route)
|
protected static function addDefaultNamespace(IRoute $route)
|
||||||
{
|
{
|
||||||
if (static::$defaultNamespace !== null) {
|
if (static::$defaultNamespace !== null) {
|
||||||
$namespace = static::$defaultNamespace;
|
$namespace = static::$defaultNamespace;
|
||||||
|
|
||||||
if ($route->getNamespace() !== null) {
|
if ($route->getNamespace() !== null) {
|
||||||
$namespace .= '\\' . $route->getNamespace();
|
$namespace .= '\\' . $route->getNamespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
$route->setDefaultNamespace($namespace);
|
$route->setDefaultNamespace($namespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user