Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Code reformatting for better readability

Change some comments here and there, formatted the code differently to
have better readability. Mainly for comments and var/function types
  • Loading branch information...
commit 11eb6e8319a8da4a1f6b428768cc994cc62eeb08 1 parent ccc7f94
@njuneau authored
View
2  build.hxml
@@ -13,6 +13,6 @@
#--------------------- Templates ---------------------#
--resource src/server/blog/templates/index.ntt@blog_view_index
+-resource src/server/test/templates/index.ntt@blog_view_index
#--------------------- Javascript ---------------------#
View
0  .htaccess → htaccess
File renamed without changes
View
4 src/hawk/exceptions/Exception.hx
@@ -6,7 +6,7 @@ package hawk.exceptions;
*/
class Exception {
- private var message:String;
+ private var message : String;
/**
* Creates a new exception
@@ -28,7 +28,7 @@ class Exception {
* Returns the Exception's message
* @return the Exception's message
*/
- public function getMessage():String {
+ public function getMessage() : String {
return this.message;
}
}
View
4 src/hawk/server/AbstractModule.hx
@@ -22,7 +22,7 @@ class AbstractModule implements Module {
* Returns all the URLs mapping that belongs to this module
* @return A list of all the URL mappings contained in the module
*/
- public function getURLMappings():Iterable<URLMapping> {
+ public function getURLMappings() : Iterable<URLMapping> {
return this.urlMappings;
}
@@ -32,7 +32,7 @@ class AbstractModule implements Module {
* @param controller The controller to call
* @param controllerFunctionName The controller's function to call
*/
- private function addURLMapping(urlReg : EReg, controllerClass : Class<Controller>, controllerFunctionName : String) {
+ private function addURLMapping(urlReg : EReg, controllerClass : Class<Controller>, controllerFunctionName : String) : Void {
this.urlMappings.add(new URLMapping(urlReg, controllerClass, controllerFunctionName));
}
View
30 src/hawk/server/AbstractServerConfiguration.hx
@@ -10,12 +10,16 @@ import hawk.exceptions.Exception;
*/
class AbstractServerConfiguration implements ServerConfiguration {
- /*************************************/
- /* PRIVATE FIELDS */
- /*************************************/
+ /**************************************************************************/
+ /* PRIVATE FIELDS */
+ /**************************************************************************/
- //The list of modules of the server side of your application
- private var modules:List<Module>;
+ // The list of modules of the server side of your application
+ private var modules : List<Module>;
+
+ /**************************************************************************/
+ /* PUBLIC METHODS */
+ /**************************************************************************/
/**
* Constructs a default implementation of the server configuration
@@ -24,10 +28,6 @@ class AbstractServerConfiguration implements ServerConfiguration {
this.modules = new List<Module>();
}
- /*************************************/
- /* PUBLIC METHODS */
- /*************************************/
-
/**
* Dispatch event - called when the queried URL corresponds to a controller
* (the URL has been dispatched). Call done before the controller is called
@@ -49,9 +49,9 @@ class AbstractServerConfiguration implements ServerConfiguration {
*/
public function onClose() : Void {}
- /*************************************/
- /* GETTERS */
- /*************************************/
+ /**************************************************************************/
+ /* GETTERS */
+ /**************************************************************************/
/**
* Returns the modules contained in the application
@@ -61,9 +61,9 @@ class AbstractServerConfiguration implements ServerConfiguration {
return this.modules;
}
- /*************************************/
- /* PRIVATE METHODS */
- /*************************************/
+ /**************************************************************************/
+ /* PRIVATE METHODS */
+ /**************************************************************************/
/**
* Adds a server module in the configuration's list of modules
View
2  src/hawk/server/Module.hx
@@ -12,5 +12,5 @@ interface Module {
* Returns all the URLs mapping that belongs to this module
* @return A list of all the URL mappings contained in the module
*/
- public function getURLMappings():Iterable<URLMapping>;
+ public function getURLMappings() : Iterable<URLMapping>;
}
View
12 src/hawk/server/ServerMain.hx
@@ -18,16 +18,16 @@ import server.UserConfiguration;
*/
class ServerMain {
- //This contains the user's server-side configuration
- private static var userConfiguration:ServerConfiguration;
- //Our URL dispatcher
- private static var urlDispatcher:URLDispatcher;
+ // This contains the user's server-side configuration
+ private static var userConfiguration : ServerConfiguration;
+ // Our URL dispatcher
+ private static var urlDispatcher : URLDispatcher;
/**
* Program's entry point, starts up pretty much everything and handles
* the recieved request.
*/
- public static function main() {
+ public static function main() : Void {
// Load the configuration
userConfiguration = new UserConfiguration();
@@ -62,7 +62,7 @@ class ServerMain {
* @param url The url in which to append the slash
* @return The url, with the trailing slash
*/
- private static function appendSlash(url:String):String {
+ private static function appendSlash(url : String) : String {
if(url.charAt(url.length - 1) != "/") {
url += "/";
View
63 src/hawk/url/URLDispatcher.hx
@@ -11,16 +11,16 @@ import hawk.exceptions.ServerErrorException;
*/
class URLDispatcher {
- /*************************************/
- /* PRIVATE FIELDS */
- /*************************************/
+ /**************************************************************************/
+ /* PRIVATE FIELDS */
+ /**************************************************************************/
// The server's configuration
- private var serverConfiguration:ServerConfiguration;
+ private var serverConfiguration : ServerConfiguration;
//The modules that this dispatcher handles
- private var modules:Iterable<Module>;
+ private var modules : Iterable<Module>;
//The URL that is currently being processed
- private var currentURL:String;
+ private var currentURL : String;
/**
* Constructor
@@ -31,45 +31,45 @@ class URLDispatcher {
this.serverConfiguration = serverConfiguration;
}
- /*************************************/
- /* BEHAVIOR */
- /*************************************/
+ /**************************************************************************/
+ /* PUBLIC METHODS */
+ /**************************************************************************/
/**
* Dipatches the URL to the correct view
* @param url The URL to process
*/
- public function dispatch(url:String):Void {
+ public function dispatch(url:String) : Void {
this.currentURL = url;
var dispatched:Bool = false;
var moduleIterator:Iterator<Module> = this.serverConfiguration.getModules().iterator();
- //Scan all the URLS
+ // Scan all the URLS
while(!dispatched && moduleIterator.hasNext()) {
dispatched = this.scanURLs(moduleIterator.next());
}
- //If the URL could not have been dispatched, throw some kind of 404
+ // If the URL could not have been dispatched, throw a 404 error
if(!dispatched) {
throw new NotFoundException();
}
}
- /*************************************/
- /* PRIVATE METHODS */
- /*************************************/
+ /**************************************************************************/
+ /* PRIVATE METHODS */
+ /**************************************************************************/
/**
* Try matching the URLS with one of the module's regexes.
* @return Scan status (true if URL has been found, false otherwize)
*/
- private function scanURLs(module:Module):Bool {
+ private function scanURLs(module : Module) : Bool {
var foundURL = false;
var mappingIterator : Iterator<URLMapping> = module.getURLMappings().iterator();
var currentMapping : URLMapping = null;
var controller : Controller = null;
- var controllerFunction:Dynamic = null;
- var controllerFunctionParams:Array<String> = null;
+ var controllerFunction : Dynamic = null;
+ var controllerFunctionParams : Array<String> = null;
// Try matching a pattern
while(!foundURL && mappingIterator.hasNext()) {
@@ -83,6 +83,7 @@ class URLDispatcher {
// Call the dispatch event
this.serverConfiguration.onDispatch(currentMapping);
+ // Create the controller instance and find its function
controller = Type.createInstance(currentMapping.getControllerClass(), [this.serverConfiguration]);
controllerFunction = Reflect.field(controller, currentMapping.getControllerFunctionName());
@@ -90,11 +91,11 @@ class URLDispatcher {
if(Reflect.isFunction(controllerFunction)) {
// Handle request
if(controller.handleRequest()) {
- Reflect.callMethod(
- controller,
- controllerFunction,
- this.extractParameters(currentMapping)
- );
+ Reflect.callMethod(
+ controller,
+ controllerFunction,
+ this.extractParameters(currentMapping)
+ );
}
} else {
// Controller function was not found - error!
@@ -111,18 +112,18 @@ class URLDispatcher {
* Extract the parameters tha the controller needs from the URL
* @param urlMapping The URLMapping in which to extract the parameters
*/
- private function extractParameters(urlMapping:URLMapping):Array<String> {
- var parameters:Array<String> = new Array();
- var regEx:EReg = urlMapping.getURLReg();
- var counter:Int = 0;
- var parameter:String = regEx.matched(counter + 1);
+ private function extractParameters(urlMapping:URLMapping) : Array<String> {
+ var parameters : Array<String> = new Array();
+ var regEx : EReg = urlMapping.getURLReg();
+ var counter : Int = 1;
+ var parameter : String = null;
// Get trough all the groups and fill the needed parameters
- while(parameter != null) {
+ do {
+ parameter = regEx.matched(counter);
parameters.push(parameter);
counter++;
- parameter = regEx.matched(counter + 1);
- }
+ } while(parameter != null);
return parameters;
}
View
28 src/hawk/url/URLMapping.hx
@@ -9,16 +9,20 @@ import hawk.server.Controller;
*/
class URLMapping {
- /*************************************/
- /* PRIVATE FIELDS */
- /*************************************/
+ /**************************************************************************/
+ /* PRIVATE FIELDS */
+ /**************************************************************************/
//The regular expression that corresponds to the URL
- private var urlReg:EReg;
+ private var urlReg : EReg;
//The controller class to instanciate if the URL is matched
- private var controllerClass:Class<Controller>;
+ private var controllerClass : Class<Controller>;
//The controller's function to call
- private var controllerFunctionName:String;
+ private var controllerFunctionName : String;
+
+ /**************************************************************************/
+ /* PUBLIC METHODS */
+ /**************************************************************************/
/**
* Construct a new URL Mapping
@@ -26,17 +30,21 @@ class URLMapping {
* @param controller The controller to call
* @param controllerFunctionName The controller's function to call
*/
- public function new(urlReg:EReg, controllerClass:Class<Controller>, controllerFunctionName:String) {
+ public function new(urlReg : EReg, controllerClass : Class<Controller>, controllerFunctionName : String) {
this.urlReg = urlReg;
this.controllerClass = controllerClass;
this.controllerFunctionName = controllerFunctionName;
}
+ /**************************************************************************/
+ /* GETTERS */
+ /**************************************************************************/
+
/**
* Returns the URL regex contained in the mapping
* @return The URL regex contained in the mapping
*/
- public function getURLReg():EReg {
+ public function getURLReg() : EReg {
return this.urlReg;
}
@@ -44,7 +52,7 @@ class URLMapping {
* Returns the controller contained in the mapping
* @return The controller contained in the mapping
*/
- public function getControllerClass():Class<Controller> {
+ public function getControllerClass() : Class<Controller> {
return this.controllerClass;
}
@@ -52,7 +60,7 @@ class URLMapping {
* Returns the regex groups contained in the mapping
* @return The regex groups contained in the mapping
*/
- public function getControllerFunctionName():String {
+ public function getControllerFunctionName() : String {
return this.controllerFunctionName;
}
}
View
2  src/hawk/views/View.hx
@@ -29,7 +29,7 @@ class View extends ViewComposite {
* @param macros Template macros
* @return The rendered template
*/
- public override function render(context : Dynamic, macros : Dynamic = null) {
+ public override function render(context : Dynamic, macros : Dynamic = null) : String {
var subResult : String = this.renderTemplate(context, macros);
// Set child view in the template context
View
8 src/server/UserConfiguration.hx
@@ -2,11 +2,11 @@ package server;
import hawk.server.AbstractServerConfiguration;
-import server.blog.Blog;
+import server.test.Test;
/**
- * This contains the user's configuration - do it the way you want,
- * as long as you implement de needed methods, described in the interface.
+ * This contains the user's configuration. Refer to the ServerConfiguration
+ * interface to see the available methods
*/
class UserConfiguration extends AbstractServerConfiguration {
@@ -15,7 +15,7 @@ class UserConfiguration extends AbstractServerConfiguration {
*/
public function new() {
super();
- this.addModule(new Blog());
+ this.addModule(new Test());
}
}
View
0  src/server/blog/Blog.hx → src/server/test/Blog.hx
File renamed without changes
View
19 src/server/test/Test.hx
@@ -0,0 +1,19 @@
+package server.test;
+
+import hawk.server.AbstractModule;
+import server.test.controllers.IndexController;
+import server.test.controllers.ParameterController;
+
+/**
+ * Test demo application
+ */
+class Test extends AbstractModule {
+
+ public function new() {
+ super();
+
+ this.addURLMapping(~/^\/hawk\/$/, IndexController, "handleARequest");
+ this.addURLMapping(~/^\/hawk\/([a-zA-Z]+)\/$/, ParameterController, "handleARequest");
+ this.addURLMapping(~/^\/hawk\/([a-zA-Z]+)\/([0-9]+)\/$/, ParameterController, "handleAnotherRequest");
+ }
+}
View
7 src/server/blog/controllers/IndexController.hx → src/server/test/controllers/IndexController.hx
@@ -1,13 +1,16 @@
-package server.blog.controllers;
+package server.test.controllers;
import php.Lib;
import hawk.server.Controller;
import hawk.views.ViewComposite;
+/**
+ * Index controller of the application
+ */
class IndexController extends Controller {
- public function handleARequest():Void {
+ public function handleARequest() : Void {
//Just the test
var indexView : ViewComposite = new ViewComposite("blog_view_index");
Lib.print(indexView.render({param:""}));
View
5 src/server/blog/controllers/ParameterController.hx → src/server/test/controllers/ParameterController.hx
@@ -1,10 +1,13 @@
-package server.blog.controllers;
+package server.test.controllers;
import php.Lib;
import hawk.server.Controller;
import hawk.views.ViewComposite;
+/**
+ * Test controller that receives parameters from the request
+ */
class ParameterController extends Controller {
public function handleARequest(parameter:String):Void {
View
0  src/server/blog/templates/index.ntt → src/server/test/templates/index.ntt
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.