Merge pull request #351 from skipperbent/v3

V3
This commit is contained in:
Simon Sessingø
2018-01-06 03:33:23 +01:00
committed by GitHub
3 changed files with 36 additions and 14 deletions
+12 -14
View File
@@ -33,7 +33,7 @@ abstract class Route implements IRoute
* *
* @var bool * @var bool
*/ */
protected $filterEmptyParams = false; protected $filterEmptyParams = true;
/** /**
* Default regular expression used for parsing parameters. * Default regular expression used for parsing parameters.
@@ -86,12 +86,20 @@ abstract class Route implements IRoute
return null; return null;
} }
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
/* Render callback function */ /* Render callback function */
if (is_callable($callback) === true) { if (is_callable($callback) === true) {
/* When the callback is a function */ /* When the callback is a function */
return call_user_func_array($callback, $this->getParameters()); return call_user_func_array($callback, $parameters);
} }
/* When the callback is a class + method */ /* When the callback is a class + method */
@@ -108,16 +116,6 @@ abstract class Route implements IRoute
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);
} }
$parameters = $this->getParameters();
/* Filter parameters with null-value */
if ($this->filterEmptyParams === true) {
$parameters = array_filter($parameters, function ($var) {
return ($var !== null);
});
}
return call_user_func_array([$class, $method], $parameters); return call_user_func_array([$class, $method], $parameters);
} }
+10
View File
@@ -545,6 +545,16 @@ class Router
$this->bootManagers[] = $bootManager; $this->bootManagers[] = $bootManager;
} }
/**
* Get routes that has been processed.
*
* @return array
*/
public function getProcessedRoutes()
{
return $this->processedRoutes;
}
/** /**
* @return array * @return array
*/ */
+14
View File
@@ -125,6 +125,20 @@ class RouterRouteTest extends PHPUnit_Framework_TestCase
TestRouter::debug('/my/custom-path', 'get'); TestRouter::debug('/my/custom-path', 'get');
} }
public function testParameterDefaultValue() {
$defaultVariable = null;
TestRouter::get('/my/{path?}', function($path = 'working') use(&$defaultVariable) {
$defaultVariable = $path;
});
TestRouter::debug('/my/');
$this->assertEquals('working', $defaultVariable);
}
public function testDefaultParameterRegex() public function testDefaultParameterRegex()
{ {
TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']); TestRouter::get('/my/{path}', 'DummyController@param', ['defaultParameterRegex' => '[\w\-]+']);