Skip to content

Commit

Permalink
Controller now responsible for returning all routes (including defaul…
Browse files Browse the repository at this point in the history
…ts) as opposed to policy trying to find mappings with heuristics.
  • Loading branch information
KrisJordan committed Dec 7, 2008
1 parent cc13847 commit 732ee9c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 78 deletions.
79 changes: 3 additions & 76 deletions recess/lib/recess/framework/DefaultPolicy.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function getControllerFor(Request &$request, array $applications, RtNode
$controller = $this->getControllerFromRouteResult($request, $routeResult);
} else {
// TODO: Shortwire a result here for a method not supported HTTP response.
throw new RecessError('METHOD not supported.');
throw new RecessException('METHOD not supported.', get_defined_vars());
}
} else {
$controller = $this->getControllerFromResourceString($request, $applications);
throw new RecessException('Resource does not exist.', get_defined_vars());
}

$this->controller = $controller;
Expand Down Expand Up @@ -112,80 +112,7 @@ protected function getControllerFromRouteResult(Request &$request, RoutingResult
$controller = new $controllerClass($routeResult->route->app);
return $controller;
}

protected function getControllerFromResourceString(Request &$request, array $applications) {
$partsCount = count($request->resourceParts);

$prefixes = array_map(create_function('$app','return $app->routingPrefix;'), $applications);

$request->meta->controllerMethodArguments = array();
$request->meta->useAssociativeArguments = false;
// TODO: Clean this up. Throw better errors to reveal internal logic.
switch($partsCount) {
case 0: // DefaultApp, HomeController, home method
$defaultAppKey = array_search('',$prefixes);
if($defaultAppKey !== false) {
$request->meta->controllerMethod = 'home';
$request->meta->app = $applications[$defaultAppKey];
Library::beginNamedRun('HomeController');
return $applications[$defaultAppKey]->getController('HomeController');
}
break;
case 1: //
$appKey = array_search($request->resourceParts[0], $prefixes);
if($appKey !== false) {
$request->meta->controllerMethod = 'home';
$request->meta->app = $applications[$appKey];
Library::beginNamedRun($request->resourceParts[0] . 'Controller');
return $applications[$appKey]->getController('HomeController');
} else {
$defaultAppKey = array_search('',$prefixes);
if($defaultAppKey !== false) {
$request->meta->controllerMethod = 'home';
$request->meta->app = $applications[$defaultAppKey];
Library::beginNamedRun($request->resourceParts[0] . 'Controller');
return $applications[$defaultAppKey]->getController($request->resourceParts[0] . 'Controller');
}
}
break;
case 2:
$appKey = array_search($request->resourceParts[0], $prefixes);
if($appKey !== false) {
$request->meta->controllerMethod = 'home';
$request->meta->app = $applications[$appKey];
Library::beginNamedRun($request->resourceParts[1] . 'Controller');
return $applications[$appKey]->getController($request->resourceParts[1] . 'Controller');
} else {
$defaultAppKey = array_search('',$prefixes);
if($defaultAppKey !== false) {
$request->meta->controllerMethod = $request->resourceParts[1];
$request->meta->app = $applications[$defaultAppKey];
Library::beginNamedRun($request->resourceParts[0] . 'Controller');
return $applications[$defaultAppKey]->getController($request->resourceParts[0] . 'Controller');
}
}
break;
default:
$appKey = array_search($request->resourceParts[0], $prefixes);
if($appKey !== false) {
$request->meta->controllerMethodArguments = array_slice($request->resourceParts,3);
$request->meta->controllerMethod = $request->resourceParts[2];
$request->meta->app = $applications[$appKey];
Library::beginNamedRun($request->resourceParts[1] . 'Controller');
return $applications[$appKey]->getController($request->resourceParts[1] . 'Controller');
} else {
$defaultAppKey = array_search('',$prefixes);
if($defaultAppKey !== false) {
$request->meta->controllerMethodArguments = array_slice($request->resourceParts,2);
$request->meta->controllerMethod = $request->resourceParts[1];
$request->meta->app = $applications[$defaultAppKey];
Library::beginNamedRun($request->resourceParts[0] . 'Controller');
return $applications[$defaultAppKey]->getController($request->resourceParts[0] . 'Controller');
}
}
break;
}
}

}

?>
24 changes: 24 additions & 0 deletions recess/lib/recess/framework/controllers/Controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,37 @@ protected static function buildClassDescriptor($class) {

$reflectedMethods = $reflection->getMethods(false);
$methods = array();
$unreachableMethods = array('serve','urlTo','__call','__construct');
foreach($reflectedMethods as $reflectedMethod) {
if(in_array($reflectedMethod->getName(),$unreachableMethods)) continue;
$annotations = $reflectedMethod->getAnnotations();
foreach($annotations as $annotation) {
if($annotation instanceof ControllerAnnotation) {
$annotation->massage(Library::getFullyQualifiedClassName($class), $reflectedMethod->name, $descriptor);
}
}

if( empty($annotations) &&
$reflectedMethod->isPublic() &&
!$reflectedMethod->isStatic()
) {
$parameters = $reflectedMethod->getParameters();
$parameterNames = array();
foreach($parameters as $parameter) {
$parameterNames[] = '$' . $parameter->getName();
}
if(!empty($parameterNames)) {
$parameterPath = '/' . implode('/',$parameterNames);
} else {
$parameterPath = '';
}
// Default Routing for Public Methods Without Annotations
$descriptor->routes[] =
new Route( $class,
$reflectedMethod->getName(),
Methods::GET,
$descriptor->routesPrefix . $reflectedMethod->getName() . $parameterPath);
}
}

return $descriptor;
Expand Down
8 changes: 6 additions & 2 deletions recess/lib/recess/http/Methods.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
* @todo Add all other methods like OPTIONS
*/
class Methods {
const PUT = 'PUT';
const POST = 'POST';
const OPTIONS = 'OPTIONS';
const GET = 'GET';
const HEAD = 'HEAD';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const TRACE = 'TRACE';
const CONNECT = 'CONNECT';
}
?>

0 comments on commit 732ee9c

Please sign in to comment.