Skip to content

Commit

Permalink
feat:optional parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Feb 3, 2021
1 parent 77091ba commit f2483e1
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions src/RouterEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,38 @@ public function __construct(Request $request, RouteCollection $collection, $apiM
*/
public function route()
{
try{
try {

// Get URL and request method.
$this->request_method = strtolower($this->request->getMethod());
// Get URL and request method.
$this->request_method = strtolower($this->request->getMethod());

if ($this->check_manual()) {
return true;
}
if ($this->check_manual()) {
return true;
}

//Break URL into segments
$this->path_info = explode('/', $this->request->getPathInfo());
if ($this->apiMode) {
array_shift($this->path_info);
}

//Break URL into segments
$this->path_info = explode('/', $this->request->getPathInfo());
if ($this->apiMode) {
array_shift($this->path_info);
}

array_shift($this->path_info);
$this->getController();
$this->method = $this->getMethod($this->controller);
$this->request->attributes->set('_controller', $this->controller . '::' . $this->method);

$this->getController();
$this->method = $this->getMethod($this->controller);
$this->request->attributes->set('_controller', $this->controller . '::' . $this->method);
$this->setArguments();
return true;
} catch (\Exception $e) {

$this->setArguments();
return true;
} catch(\Exception $e){
if($this->apiMode){
return false;
}else{
throw $e;
if ($this->apiMode) {
return false;
} else {
throw $e;
}
}
}


}

//---------------------------------------------------------------//
Expand Down Expand Up @@ -200,14 +200,17 @@ protected function error($message)
private function setArguments()
{
$controller = new $this->controller;

$arguments = [];
for ($j = 2; $j < count($this->path_info); $j++) {
array_push($arguments, $this->path_info[$j]);
}
//Check weather arguments are passed else throw a 404 error
$classMethod = new \ReflectionMethod($controller, $this->method);
if (count($arguments) < count($classMethod->getParameters())) {
$docblock = $this->phpdoc_params($classMethod);

//Optional parameter introduced in version 3.0.2
if (count($arguments) < count($classMethod->getParameters()) && isset($docblock['@param']) && $docblock['@param'][0] != 'optional') {
$this->error('Not enough arguments given to the method');
}
// finally fix the long awaited allIndex bug !
Expand Down Expand Up @@ -276,4 +279,31 @@ private function check_manual()
}
return false;
}

//------------------------------------------------------------------//
private function phpdoc_params(\ReflectionMethod $method): array
{
// Retrieve the full PhpDoc comment block
$doc = $method->getDocComment();

// Trim each line from space and star chars
$lines = array_map(function ($line) {
return trim($line, " *");
}, explode("\n", $doc));

// Retain lines that start with an @
$lines = array_filter($lines, function ($line) {
return strpos($line, "@") === 0;
});

$args = [];

// Push each value in the corresponding @param array
foreach ($lines as $line) {
list($param, $value) = explode(' ', $line, 2);
$args[$param][] = $value;
}

return $args;
}
}

0 comments on commit f2483e1

Please sign in to comment.