Skip to content

Commit

Permalink
cleans up Router
Browse files Browse the repository at this point in the history
  • Loading branch information
henderjon committed Sep 7, 2014
1 parent 0790d9c commit 0834cd5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 90 deletions.
47 changes: 47 additions & 0 deletions src/Router/AbstractRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,51 @@ abstract class AbstractRouter {
use DefaultActionAwareTrait;
use DefaultFormatAwareTrait;

/**
* take the $_SERVER[REQUEST_URI] and parse it into a path, a file, and an extension
* along with an array representing the query string. Everything after the domain
* is assumed to be a Name\Space\Controller::action()
*
* domain.com/users/profiles/edit.html?id=15 should translate to Users\Profiles::edit()
*
* The router pays no mind to IF the class exists or should be called
*
* @note ^([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:\.([\w]*))?(?:\?(.*))?
*
* @param string $path A string representing the path to be parsed -- $_SERVER[REQUEST_URI]
* @return array
*/
protected function parseRequest($path){
$url = parse_url($path);

$parts = explode("/", $url["path"]);
$action = array_pop($parts); // methods are lowercase
array_walk($parts, function(&$v){ //, $k
$v = ucwords($v);
});
$class = implode("\\", $parts);

$controller = "";
if($class){
$controller = $class;
}

$method = $this->default_action;
$format = $this->default_format;
if($action){
$method = $action;
if(($pos = strpos($action, ".")) !== false){
$method = substr($action, 0, $pos);
$format = substr($action, ++$pos);
}
}

$query = [];
if(isset($url["query"])){
parse_str($url["query"], $query);
}

return [$controller, $method, $format, $query];
}

}
44 changes: 2 additions & 42 deletions src/Router/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,9 @@ class CliRouter extends AbstractRouter implements RouterInterface {
* @return Route
*/
function match($path, array $params = []){
list($controller, $action) = $this->parseRequestUri($path);
list($controller, $action, $format, $query) = $this->parseRequest($path);
// $format and $query are ignored in the CLI
return new Route($controller, $action, null, $params);
}

/**
* take the $_SERVER[REQUEST_URI] and parse it into a path, a file, and an extension
* along with an array representing the query string. Everything after the domain
* is assumed to be a Name\Space\Controller::action()
*
* domain.com/users/profiles/edit.html?id=15 should translate to Users\Profiles::edit()
*
* The router pays no mind to IF the class exists or should be called
*
* @param string $path A string representing the path to be parsed -- $_SERVER[REQUEST_URI]
* @return array
*/
protected function parseRequestUri($path){
$url = parse_url($path);

$parts = explode("/", $url["path"]);
$action = array_pop($parts); // methods are lowercase
array_walk($parts, function(&$v){ //, $k
$v = ucwords($v);
});
$class = implode("\\", $parts);

$controller = "";
if($class){
$controller = $class;
}

$method = $this->default_action;
// $format = $this->default_format;
if($action){
$method = $action;
if(($pos = strpos($action, ".")) !== false){
$method = substr($action, 0, $pos);
// $format = substr($action, ++$pos);
}
}

return [$controller, $method];

}

}
49 changes: 1 addition & 48 deletions src/Router/WebRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,55 +30,8 @@ class WebRouter extends AbstractRouter implements RouterInterface {
* @return Route
*/
function match($path){
list($controller, $action, $format, $parameters) = $this->parseRequestUri($path);
list($controller, $action, $format, $parameters) = $this->parseRequest($path);
return new Route($controller, $action, $format, $parameters);
}

/**
* take the $_SERVER[REQUEST_URI] and parse it into a path, a file, and an extension
* along with an array representing the query string. Everything after the domain
* is assumed to be a Name\Space\Controller::action()
*
* domain.com/users/profiles/edit.html?id=15 should translate to Users\Profiles::edit()
*
* The router pays no mind to IF the class exists or should be called
*
* @note ^([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(?:\.([\w]*))?(?:\?(.*))?
*
* @param string $path A string representing the path to be parsed -- $_SERVER[REQUEST_URI]
* @return array
*/
protected function parseRequestUri($path){
$url = parse_url($path);

$parts = explode("/", $url["path"]);
$action = array_pop($parts); // methods are lowercase
array_walk($parts, function(&$v){ //, $k
$v = ucwords($v);
});
$class = implode("\\", $parts);

$controller = "";
if($class){
$controller = $class;
}

$method = $this->default_action;
$format = $this->default_format;
if($action){
$method = $action;
if(($pos = strpos($action, ".")) !== false){
$method = substr($action, 0, $pos);
$format = substr($action, ++$pos);
}
}

$query = [];
if(isset($url["query"])){
parse_str($url["query"], $query);
}

return [$controller, $method, $format, $query];
}

}

0 comments on commit 0834cd5

Please sign in to comment.