Skip to content

Commit

Permalink
Still refactoring. Getting closer.
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Nov 11, 2008
1 parent ef1a940 commit 2191b78
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 210 deletions.
4 changes: 2 additions & 2 deletions apps/backend/BackEndApplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class BackEndApplication extends Application {

public $controllersPrefix = 'backend.controllers';
public $controllersPrefix = 'backend.controllers.';

public $routingPrefix = '/backend/';
public $routingPrefix = 'backend';

}
?>
10 changes: 7 additions & 3 deletions apps/backend/controllers/LoginController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

class LoginController extends Controller {

/** !Route GET, login */
function showLogin() {
echo 'login'; exit;
/** !Route GET, login/$name */
function showLogin($name) {
echo 'login welcomes ' . $name; exit;
}

function kris($lastname) {
echo 'kris' . $lastname; exit;
}

}
Expand Down
4 changes: 2 additions & 2 deletions apps/frontend/FrontEndApplication.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class FrontEndApplication extends Application {

public $controllersPrefix = 'frontend.controllers';
public $controllersPrefix = 'frontend.controllers.';

public $routingPrefix = '/';
public $routingPrefix = '';

}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

class RecessIdeApplication extends Application {

public $controllersPrefix = 'recess.framework.apps.ide.controllers';
public $controllersPrefix = 'recess.apps.ide.controllers.';

public $routingPrefix = '/ide/';
public $routingPrefix = 'recess';

}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
Library::import('recess.framework.controllers.Controller');
Library::import('app.controllers.RouteAnnotation', true);
Library::import('recess.http.responses.NotFoundResponse');
Library::import('recess.framework.ResponseData');
Library::import('recess.http.responses.OkResponse');

class RecessReflectorController extends Controller {
/** !Route GET, reflector/ */
public function home() {
echo 'ROUTED home';
}

/** !Route GET, reflector/package/$package */
public function package($package) {
echo 'package/' . $package; exit;
}

/** !Route GET, reflector/model/$fullyQualifiedModel */
public function model($fullyQualifiedModel) {
if(!Library::classExists($fullyQualifiedModel)) {
return new NotFoundResponse($request);
}

$model = Library::getClassName($fullyQualifiedModel);
$reflection = new RecessReflectionClass($model);

$data = new stdclass;
$data->reflection = $reflection;
$data->relationships = Model::getRelationships($model);
$data->columns = Model::getColumns($model);
$data->table = Model::tableFor($model);

return $this->ok();

// $response = new OkResponse($this->request, $data);
// $response->meta->app = new RecessIdeApplication();
// $response->meta->viewName = 'reflector/model';
// return $response;
}

}

?>
15 changes: 12 additions & 3 deletions lib/recess/framework/Application.class.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
abstract class Application {

public $controllersPrefix = 'controllers'; // OVERRIDE THIS
public $controllersPrefix = 'controllers.'; // OVERRIDE THIS

public $routingPrefix = '/';

function addRoutes(RoutingNode $router) {
function addRoutesToRouter(RoutingNode $router) {
$classes = Library::findClassesIn($this->controllersPrefix);

foreach($classes as $class) {
Library::import($this->controllersPrefix . '.' . $class);
Library::import($this->controllersPrefix . $class);
$instance = new $class;
if($instance instanceof Controller) {
foreach(Controller::getRoutes($instance) as $route) {
Expand All @@ -21,5 +21,14 @@ function addRoutes(RoutingNode $router) {
return $router;
}

function getController($controller) {
return Library::importAndInstantiate($this->controllersPrefix . $controller);
}

function getView($view) {
$view = Library::importAndInstantiate($view);
return $view;
}

}
?>
4 changes: 2 additions & 2 deletions lib/recess/framework/Coordinator.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class Coordinator {
* @package recess
* @static
*/
public static function main(IPolicy $policy, Request $request, RoutingNode $routing = null, Plugins $plugins = null) {
public static function main(Request $request, IPolicy $policy, array $apps, RoutingNode $routes, array $plugins = array()) {

$pluggedPolicy = $policy;

Expand All @@ -29,7 +29,7 @@ public static function main(IPolicy $policy, Request $request, RoutingNode $rout
$request = $pluggedPolicy->preprocess($request);

// $controller = $pluggedPolicy->getControllerFor($request, $routing);
$controller = $pluggedPolicy->getControllerFor($request);
$controller = $pluggedPolicy->getControllerFor($request, $apps, $routes);

$response = $controller->serve($request);

Expand Down
174 changes: 174 additions & 0 deletions lib/recess/framework/DefaultPolicy.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
Library::import('recess.framework.controllers.Controller');
Library::import('recess.framework.views.View');
Library::import('recess.framework.interfaces.IPolicy');

class DefaultPolicy implements IPolicy {

/**
* Used to pre-process a request.
* This may involve extracting information and transforming values.
* For example, Transforming the HTTP method from POST to PUT based on a POSTed field.
*
* @param Request The Request to refine.
* @return Request The refined Request.
*/
public function preprocess(Request $request) {

$this->getHttpMethodFromPost($request);

$this->getFormatFromResourceString($request);

if($request->format != Formats::xhtml) {
$this->reparameterizeForFormat($request);
}

return $request;
}

public function getControllerFor(Request $request, array $applications, RoutingNode $routes) {

$routeResult = $routes->findRouteFor($request);

if($routeResult->routeExists) {
if($routeResult->methodIsSupported) {
$controller = $this->getControllerFromRouteResult($request, $routeResult);
} else {
// TODO: Shortwire a result here for a method not supported HTTP response.
throw new RecessError('METHOD not supported.');
}
} else {
$controller = $this->getControllerFromResourceString($request, $applications);
}

return $controller;
}

public function getViewFor(Response $response) {
$response->meta->app->loadView($response->meta->viewClass);
return new $response->meta->viewClass;
}

/////////////////////////////////////////////////////////////////////////
// Helper Methods

const HTTP_METHOD_FIELD = '_METHOD';

protected function getHttpMethodFromPost(Request &$request) {
if(array_key_exists(self::HTTP_METHOD_FIELD, $request->post)) {
$request->method = $request->post[self::HTTP_METHOD_FIELD];
unset($request->post[self::HTTP_METHOD_FIELD]);
if($request->method == Methods::PUT) {
$request->put = $request->post;
}
}
return $request;
}

protected function getFormatFromResourceString(Request &$request) {
$lastPartIndex = count($request->resourceParts) - 1;
if($lastPartIndex < 0) return $request;

$lastPart = $request->resourceParts[$lastPartIndex];

$lastDotPosition = strrpos($lastPart, Library::dotSeparator);
if($lastDotPosition !== false) {
$substring = substr($lastPart, $lastDotPosition + 1);
if(in_array($substring, Formats::$all)) {
$request->format = $substring;
$request->setResource(substr($request->resource, 0, strrpos($request->resource, Library::dotSeparator)));
} else {
$request->format = Formats::xhtml;
}
}
return $request;
}

protected function reparameterizeForFormat(Request &$request) {
// TODO: Think about how parameter passing via json/xml/post-vars can be streamlined
if($request->format == Formats::json) {
if(array_key_exists('json', $request->post)){
$request->post = json_decode($request->post['json']);
}
} else if ($request->format == Formats::xml) {
// TODO: XML reparameterization in request transformer
}
return $request;
}

protected function getControllerFromRouteResult(Request &$request, RoutingResult $routeResult) {
$request->meta->controllerMethod = $routeResult->route->function;
$request->meta->controllerMethodArguments = $routeResult->arguments;
$request->meta->useAssociativeArguments = true;
return Library::importAndInstantiate($routeResult->route->class);
}

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];
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];
return $applications[$appKey]->getController('HomeController');
} else {
$defaultAppKey = array_search('',$prefixes);
if($defaultAppKey !== false) {
$request->meta->controllerMethod = 'home';
$request->meta->app = $applications[$defaultAppKey];
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];
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];
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];
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];
return $applications[$defaultAppKey]->getController($request->resourceParts[0] . 'Controller');
}
}
break;
}
}
}

?>
9 changes: 0 additions & 9 deletions lib/recess/framework/ResponseData.class.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
Library::import('recess.framework.policies.default.annotations.ControllerAnnotation');
Library::import('recess.framework.annotations.ControllerAnnotation');
Library::import('recess.framework.routing.Route');

class RouteAnnotation extends ControllerAnnotation {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
Library::import('recess.framework.policies.default.annotations.ControllerAnnotation');
Library::import('recess.framework.annotations.ControllerAnnotation');

class ViewAnnotation extends ControllerAnnotation {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Library::import('recess.lang.RecessClass');
Library::import('recess.lang.RecessReflectionClass');
Library::import('recess.lang.Annotation', true);
Library::import('recess.framework.policies.default.annotations.ViewAnnotation', true);
Library::import('recess.framework.policies.default.annotations.RouteAnnotation', true);
Library::import('recess.framework.annotations.ViewAnnotation', true);
Library::import('recess.framework.annotations.RouteAnnotation', true);
class ControllerDescriptor extends RecessClassDescriptor {
public $routes;
public $viewClass = 'NativeView';
Expand Down
2 changes: 1 addition & 1 deletion lib/recess/framework/interfaces/IPolicy.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
interface IPolicy {
public function preprocess(Request $request);

public function getControllerFor(Request $request);
public function getControllerFor(Request $request, array $applications, RoutingNode $routes);

public function getViewFor(Response $response);
}
Expand Down
Loading

0 comments on commit 2191b78

Please sign in to comment.