mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 16:57:53 +00:00
Compare commits
10 Commits
2.6.0
...
v2-development
| Author | SHA1 | Date | |
|---|---|---|---|
| c1c25e19ff | |||
| dc7faf52c0 | |||
| 539e905b45 | |||
| 5977efc942 | |||
| bf069c2d42 | |||
| 523d49359b | |||
| 7cc2652bcd | |||
| e26d55a810 | |||
| 8f3ce68a5e | |||
| d25351f4f9 |
@@ -267,29 +267,21 @@ class Input
|
||||
*/
|
||||
public function all(array $filter = null)
|
||||
{
|
||||
$output = $_POST;
|
||||
$output = $_GET;
|
||||
|
||||
if ($this->request->getMethod() === 'post') {
|
||||
|
||||
$contents = file_get_contents('php://input');
|
||||
|
||||
if (strpos(trim($contents), '{') === 0) {
|
||||
$output = json_decode($contents, true);
|
||||
if ($output === false) {
|
||||
$output = [];
|
||||
$post = json_decode($contents, true);
|
||||
if ($post !== false) {
|
||||
$output = array_merge($output, $post);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$output = array_merge($_GET, $output);
|
||||
|
||||
if ($filter !== null) {
|
||||
$output = array_filter($output, function ($key) use ($filter) {
|
||||
return (in_array($key, $filter) === true);
|
||||
}, ARRAY_FILTER_USE_KEY);
|
||||
}
|
||||
|
||||
return $output;
|
||||
return ($filter !== null) ? array_intersect_key($output, array_flip($filter)) : $output;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -127,7 +127,7 @@ class InputFile implements IInputItem
|
||||
*/
|
||||
public function getExtension()
|
||||
{
|
||||
return pathinfo($this->getName(), PATHINFO_EXTENSION);
|
||||
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,7 @@ class Request
|
||||
$this->host = $this->getHeader('http-host');
|
||||
$this->uri = $this->getHeader('request-uri');
|
||||
$this->input = new Input($this);
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method'), 'post'));
|
||||
$this->method = strtolower($this->input->get('_method', $this->getHeader('request-method')));
|
||||
}
|
||||
|
||||
protected function parseHeaders()
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Pecee\Http;
|
||||
|
||||
class Response
|
||||
@@ -82,14 +83,20 @@ class Response
|
||||
}
|
||||
|
||||
/**
|
||||
* Json encode array
|
||||
* @param array $value
|
||||
* Json encode
|
||||
* @param array|\JsonSerializable $value
|
||||
* @param int $options JSON options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR.
|
||||
* @param int $dept JSON debt.
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function json(array $value)
|
||||
public function json($value, $options = null, $dept = 512)
|
||||
{
|
||||
$this->header('Content-Type: application/json');
|
||||
echo json_encode($value);
|
||||
die();
|
||||
if (($value instanceof \JsonSerializable) === false && is_array($value) === false) {
|
||||
throw new \InvalidArgumentException('Invalid type for parameter "value". Must be of type array or object implementing the \JsonSerializable interface.');
|
||||
}
|
||||
$this->header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode($value, $options, $dept);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -107,48 +107,48 @@ abstract class LoadableRoute extends Route implements ILoadableRoute
|
||||
*/
|
||||
public function findUrl($method = null, $parameters = null, $name = null)
|
||||
{
|
||||
$url = '';
|
||||
|
||||
$parameters = (array)$parameters;
|
||||
$url = $this->getUrl();
|
||||
|
||||
if ($this->getGroup() !== null && count($this->getGroup()->getDomains()) > 0) {
|
||||
$url .= '//' . $this->getGroup()->getDomains()[0];
|
||||
$url = '//' . $this->getGroup()->getDomains()[0] . $url;
|
||||
}
|
||||
|
||||
$url .= $this->getUrl();
|
||||
|
||||
$params = array_merge($this->getParameters(), $parameters);
|
||||
|
||||
/* Url that contains parameters that aren't recognized */
|
||||
/* Contains parameters that aren't recognized and will be appended at the end of the url */
|
||||
$unknownParams = [];
|
||||
|
||||
/* Create the param string - {} */
|
||||
/* Create the param string - {parameter} */
|
||||
$param1 = $this->paramModifiers[0] . '%s' . $this->paramModifiers[1];
|
||||
|
||||
/* Create the param string with the optional symbol - {?} */
|
||||
/* Create the param string with the optional symbol - {parameter?} */
|
||||
$param2 = $this->paramModifiers[0] . '%s' . $this->paramOptionalSymbol . $this->paramModifiers[1];
|
||||
|
||||
/* Let's parse the values of any {} parameter in the url */
|
||||
/* Replace any {parameter} in the url with the correct value */
|
||||
|
||||
$params = $this->getParameters();
|
||||
$max = count($params) - 1;
|
||||
$keys = array_keys($params);
|
||||
|
||||
for ($i = $max; $i >= 0; $i--) {
|
||||
$param = $keys[$i];
|
||||
$value = $params[$param];
|
||||
$value = $value = ($parameters !== null && array_key_exists($param, $parameters)) ? $parameters[$param] : $params[$param];
|
||||
|
||||
$value = isset($parameters[$param]) ? $parameters[$param] : $value;
|
||||
/* If parameter is specifically set to null - use the original-defined value */
|
||||
if ($value === null && isset($this->originalParameters[$param])) {
|
||||
$value = $this->originalParameters[$param];
|
||||
}
|
||||
|
||||
if (stripos($url, $param1) !== false || stripos($url, $param) !== false) {
|
||||
/* Add parameter to the correct position */
|
||||
$url = str_ireplace([sprintf($param1, $param), sprintf($param2, $param)], $value, $url);
|
||||
} else {
|
||||
$unknownParams[$param] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/** @noinspection AliasFunctionsUsageInspection */
|
||||
$url .= join('/', $unknownParams);
|
||||
|
||||
|
||||
|
||||
return rtrim($url, '/') . '/';
|
||||
}
|
||||
|
||||
|
||||
@@ -95,11 +95,7 @@ abstract class Route implements IRoute
|
||||
|
||||
if (preg_match_all('/' . $regex . '/is', $route, $parameters)) {
|
||||
|
||||
$parameterNamesRegex = [];
|
||||
$parameterNames = $parameters[1];
|
||||
$parameterRequired = [];
|
||||
|
||||
$urlParts = preg_split('/\{[^}]+\}/is', rtrim($route, '/'));
|
||||
$urlParts = preg_split('/((\-?\/?)\{[^}]+\})/is', rtrim($route, '/'));
|
||||
|
||||
foreach ($urlParts as $key => $t) {
|
||||
|
||||
@@ -109,7 +105,7 @@ abstract class Route implements IRoute
|
||||
|
||||
$name = $parameters[1][$key];
|
||||
$regex = isset($this->where[$name]) ? $this->where[$name] : $parameterRegex;
|
||||
$regex = sprintf('(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
|
||||
$regex = sprintf('\-?\/?(?P<%s>%s)', $name, $regex) . $parameters[2][$key];
|
||||
|
||||
}
|
||||
|
||||
@@ -128,7 +124,7 @@ abstract class Route implements IRoute
|
||||
|
||||
/* Only take matched parameters with name */
|
||||
foreach ($parameters[1] as $name) {
|
||||
$values[$name] = $matches[$name];
|
||||
$values[$name] = (isset($matches[$name]) && $matches[$name] !== '') ? $matches[$name] : null;
|
||||
}
|
||||
|
||||
return $values;
|
||||
@@ -406,7 +402,7 @@ abstract class Route implements IRoute
|
||||
*/
|
||||
public function where(array $options)
|
||||
{
|
||||
return $this->where($options);
|
||||
return $this->setWhere($options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -442,7 +438,7 @@ abstract class Route implements IRoute
|
||||
$this->originalParameters = $parameters;
|
||||
}
|
||||
|
||||
$this->parameters = $parameters;
|
||||
$this->parameters = array_merge($this->parameters, $parameters);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -99,4 +99,16 @@ class RouterUrlTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testRegEx()
|
||||
{
|
||||
SimpleRouter::router()->reset();
|
||||
SimpleRouter::request()->setMethod('get');
|
||||
SimpleRouter::request()->setUri('/my/custom-path');
|
||||
|
||||
SimpleRouter::get('/my/{path}', 'DummyController@start')->where(['path' => '[a-zA-Z\-]+']);
|
||||
|
||||
SimpleRouter::start();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user