Skip to content

Commit

Permalink
feat: introducing manual routing
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Apr 11, 2023
1 parent fe625d2 commit 8ee9ccd
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 2 deletions.
46 changes: 46 additions & 0 deletions src/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class RouteCollection
*/
private $controllers = [];

/**
* Stores all manual route.
*/
private $route = [];

/*
* Stores the path of dirctory containing controllers
*/
Expand Down Expand Up @@ -231,4 +236,45 @@ public function enableCacheWith($cache)
}
throw new \Exception('Cache engine must be an instance of Psr\SimpleCache\CacheInterface');
}

//---------------------------------------------------------------//
private function registerManual($method,$route,$callable){
$this->route[$method][$route] = $callable;
}

//---------------------------------------------------------------//
public function get($route,$callable){
$this->registerManual('get',$route,$callable);
}

//---------------------------------------------------------------//
public function post($route,$callable){
$this->registerManual('post',$route,$callable);
}

//---------------------------------------------------------------//
public function put($route,$callable){
$this->registerManual('put',$route,$callable);
}

//---------------------------------------------------------------//
public function delete($route,$callable){
$this->registerManual('delete',$route,$callable);
}

//---------------------------------------------------------------//
public function getRoute($route,$method){
if(isset($this->route[$method][$route])){
return $this->route[$method][$route];
}
return false;
}

//---------------------------------------------------------------//
public function getRoutes(){
return $this->route;
}
}



2 changes: 1 addition & 1 deletion src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function dispatch($type = null)

$controller = $controller->getController($this->request);
$arguments = $arguments->getArguments($this->request, $controller);
$content = $controller(...$arguments);
$content = call_user_func($controller,...$arguments);
if ($type == null) {
$type = array('content-type' => 'text/html');
}
Expand Down
37 changes: 36 additions & 1 deletion src/RouterEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public function route()
$this->path_info = explode('/', $this->request->getPathInfo());

array_shift($this->path_info);

if(!$this->routeManual()){
$this->setRequestArguments();
}

return true;
}
Expand Down Expand Up @@ -255,5 +258,37 @@ private function getMethod($controller)
}
}


private function routeManual(){
$controller = null;
$arguments = array();
$routes = $this->collection->getRoutes();
$collection_route = $this->collection->getRoute($this->request->getPathInfo(), $this->request_method);
if ($collection_route) {
$controller = $collection_route;
} elseif ($routes) {
$tokens = array(
':string' => '([a-zA-Z]+)',
':number' => '([0-9]+)',
':alpha' => '([a-zA-Z0-9-_]+)'
);

foreach ($routes[$this->request_method] as $pattern => $handler_name) {
$pattern = strtr($pattern, $tokens);
if (preg_match('#^/?' . $pattern . '/?$#', $this->request->getPathInfo(), $matches)) {
$controller = $handler_name;
$arguments = $matches;
break;
}
}
}

if(is_callable($controller)){
$this->request->attributes->set('_controller', $controller);
unset($arguments[0]);
$this->request->attributes->set('_arguments', implode(',',$arguments));
return true;
}

return false;
}
}
90 changes: 90 additions & 0 deletions tests/Unit/ManualRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
use Symfony\Component\HttpFoundation\Request;

it('tests manual route ', function (bool $cache) {

$collection = getCollection($cache);
$collection->get('/testo',function(){
return 'Hello';
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/testo',
'GET'
));

$response = $this->router->dispatch();

expect($response->getContent())->toBe('Hello');

$collection->post('/testo',function(){
return 'Hello post';
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/testo',
'POST'
));

$response = $this->router->dispatch();

expect($response->getContent())->toBe('Hello post');

$collection->delete('/testo',function(){
return 'Hello delete';
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/testo',
'DELETE'
));

$response = $this->router->dispatch();

expect($response->getContent())->toBe('Hello delete');

$collection->put('/testo',function(){
return 'Hello put';
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/testo',
'PUT'
));

$response = $this->router->dispatch();

expect($response->getContent())->toBe('Hello put');

})->with(['cacheEnabled'=>true,'cacheDisabled'=>false]);

it('tests manual get with parameters', function (bool $cache) {

$collection = getCollection($cache);
$collection->get('/testo/:alpha',function($name){
return 'Hello '.$name;
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/testo/sam',
'GET'
));

$response = $this->router->dispatch();

expect($response->getContent())->toBe('Hello sam');

$collection->get('/test/num/:number',function($num){
return 'num '.$num;
});

$this->router = new \Scrawler\Router\Router($collection, Request::create(
'/test/num/5',
'GET'
));

$response = $this->router->dispatch();
expect($response->getContent())->toBe('num 5');


})->with(['cacheEnabled'=>true,'cacheDisabled'=>false]);
2 changes: 2 additions & 0 deletions tests/Unit/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

})->with(['cacheEnabled'=>true,'cacheDisabled'=>false]);



0 comments on commit 8ee9ccd

Please sign in to comment.