Skip to content

Commit

Permalink
feat: tweaks for scrawler framework api mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Oct 31, 2020
1 parent ea9cdd9 commit 3e007a4
Showing 1 changed file with 53 additions and 51 deletions.
104 changes: 53 additions & 51 deletions src/RouterEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,40 @@ class RouterEngine
*/
private $method;


/**
* Stores dir mode
*/
private $dirMode = false;

/**
* Stores api mode
*/
private $apiMode = false;

/**
* Store Dirctory during dir Mode
*/
private $dir = '';
private $dir = '';

/**
* Stores if not found error occured
*/
private $not_found;

//---------------------------------------------------------------//

/**
* constructor overloading for auto routing.
*/
public function __construct(Request $request, RouteCollection $collection)
public function __construct(Request $request, RouteCollection $collection, $apiMode = false)
{
$this->request = $request;
$this->collection = $collection;
$this->apiMode = $apiMode;
}

//---------------------------------------------------------------//


/**
* Detects the URL and call the corrosponding method
* of corrosponding controller.
Expand All @@ -90,13 +98,22 @@ public function route()

//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->request->attributes->set('_controller', $this->controller . '::' . $this->method);

$this->setArguments();

if($this->not_found)
return true;
else
return false;
}

//---------------------------------------------------------------//
Expand All @@ -109,13 +126,12 @@ public function route()
private function getNamespace()
{
if ($this->dirMode) {
return $this->collection->getNamespace().'\\'.$this->dir;
return $this->collection->getNamespace() . '\\' . $this->dir;
}

return $this->collection->getNamespace();
}


//---------------------------------------------------------------//

/**
Expand All @@ -131,33 +147,30 @@ private function getController()
$this->dir = ucfirst($this->path_info[0]);
$this->dirMode = true;
if (isset($this->path_info[1])) {
$this->controller = $this->dir.'/'.ucfirst($this->path_info[1]);
$this->controller = $this->dir . '/' . ucfirst($this->path_info[1]);
}
array_shift($this->path_info);
}


//Set corrosponding controller
if (isset($this->path_info[0]) && !empty($this->path_info[0])) {
$this->controller = $this->collection->getController($this->controller);
} else {
$this->controller = $this->getNamespace().'\Main';
$this->controller = $this->getNamespace() . '\Main';
}


//Sets the Request attribute according to the route
if (!class_exists($this->controller)) {
$this->controller = $this->getNamespace().'\Main';
$this->controller = $this->getNamespace() . '\Main';
if (class_exists($this->controller)) {
array_unshift($this->path_info, '');
} else {
$this->error('No Controller could be resolved:'.$this->controller);
$this->error('No Controller could be resolved:' . $this->controller);
}
}
//$this->error('No Controller could be resolved:'.$this->controller);
}


//---------------------------------------------------------------//

/**
Expand All @@ -167,7 +180,11 @@ private function getController()
*/
protected function error($message)
{
throw new NotFoundException('Oops its an 404 error! :'.$message);
$this->not_found = true;

if(!$this->apiMode)
throw new NotFoundException('Oops its an 404 error! :' . $message);

}

//---------------------------------------------------------------//
Expand All @@ -188,12 +205,11 @@ private function setArguments()
$classMethod = new \ReflectionMethod($controller, $this->method);
if (count($arguments) < count($classMethod->getParameters())) {
$this->error('Not enough arguments given to the method');
}
}
// finally fix the long awaited allIndex bug !
elseif (count($arguments) > count($classMethod->getParameters())) {
$this->error('Not able to resolve any method for'.$this->controller.'controller');
}
else {
$this->error('Not able to resolve any method for' . $this->controller . 'controller');
} else {
$this->request->attributes->set('_arguments', implode(",", $arguments));
}
}
Expand All @@ -212,49 +228,35 @@ private function getMethod($controller)

//Set Method from second argument from URL
if (isset($this->path_info[1])) {
if (method_exists($controller, $function = $this->request_method . ucfirst($this->path_info[1]))) {
if (method_exists($controller, $function = $this->request_method . ucfirst($this->path_info[1]))) {
return $function;
}
if (method_exists($controller, $function = 'all' . ucfirst($this->path_info[1]))) {
return $function;
}
}
//If second argument not set switch to Index function ( Before v2.1.1)
// if (!isset($this->path_info[1])) {
// if (method_exists($controller, $function = $this->request_method . 'Index')) {
// array_shift($this->path_info);
// return $function;
// }
// if (!isset($this->path_info[0])) {
// if (method_exists($controller, $function = 'allIndex')) {
// array_shift($this->path_info);
// return $function;
// }
// }
// }

//Introduced in v2.1.2
//Give Scrawler last chance to resolve index method before declaring not found
//Store the last tested function before all index used for better debugging
// if (!isset($this->path_info[1])) {
if (isset($function)) {
$last_function = $function;
}
if (method_exists($controller, $function = $this->request_method . 'Index')) {
array_unshift($this->path_info, '');
return $function;
}
//Last attempt to invoke allIndex
if (method_exists($controller, $function = 'allIndex')) {
array_unshift($this->path_info, '');
return $function;
}
// }

if (isset($function)) {
$last_function = $function;
}
if (method_exists($controller, $function = $this->request_method . 'Index')) {
array_unshift($this->path_info, '');
return $function;
}
//Last attempt to invoke allIndex
if (method_exists($controller, $function = 'allIndex')) {
array_unshift($this->path_info, '');
return $function;
}

if (isset($last_function)) {
$this->error('Neither '.$function.' method nor '.$last_function.' method you found in '.$controller.' controller');
}else{
$this->error($function.' method not found in '.$controller.' controller');
$this->error('Neither ' . $function . ' method nor ' . $last_function . ' method you found in ' . $controller . ' controller');
} else {
$this->error($function . ' method not found in ' . $controller . ' controller');

}
}
Expand Down

0 comments on commit 3e007a4

Please sign in to comment.