Compare commits

...

10 Commits

Author SHA1 Message Date
Simon Sessingo c1c25e19ff Enabled fallback _method for every request-type. 2017-12-17 10:38:13 +01:00
Simon Sessingo dc7faf52c0 Bugfixes
- Fixed filtering for get-inputs in all method.
- Fixed utf-8 header issue for response()->json() method (issue: #333).
2017-12-16 12:50:48 +01:00
Simon Sessingø 539e905b45 Router->json now accepts either array or \JsonSerializable (issue: #284) 2017-08-31 12:01:49 +01:00
Simon Sessingø 5977efc942 Fixed: replace empty parameter-values with null. 2017-02-12 20:45:56 +01:00
Simon Sessingø bf069c2d42 Fixed getExtension method in InputFile retuning empty value. 2017-02-01 07:51:35 +01:00
Simon Sessingø 523d49359b Development
- Fixed where method calling itself instead of setWhere.
- Added unit-tests for regex.
2017-01-19 18:40:47 +01:00
Simon Sessingø 7cc2652bcd getRoute optimisations. 2016-11-28 14:13:58 +01:00
Simon Sessingø e26d55a810 getUrl will now use default-parameter value if specificially set to null when using url(). 2016-11-28 09:37:05 +01:00
Simon Sessingø 8f3ce68a5e Optimised route-match behavior 2016-11-28 06:43:26 +01:00
Simon Sessingø d25351f4f9 Fixed parameters not merged with default values 2016-11-28 05:53:02 +01:00
7 changed files with 52 additions and 45 deletions
+5 -13
View File
@@ -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;
}
}
+1 -1
View File
@@ -127,7 +127,7 @@ class InputFile implements IInputItem
*/
public function getExtension()
{
return pathinfo($this->getName(), PATHINFO_EXTENSION);
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
}
/**
+1 -1
View File
@@ -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()
+13 -6
View File
@@ -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);
}
/**
+15 -15
View File
@@ -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, '/') . '/';
}
+5 -9
View File
@@ -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;
}
+12
View File
@@ -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();
}
}