public string loadModule(string path, PathMode pathMode) { var page = this.parsePath(path); if (pathMode != PathMode.direct) { string mod = page.get("mod"), func = page.get("func"); mod = mod.Length > 0 ? mod.Substring(0, 1).ToUpper() + mod.Substring(1) : appName(); if (mod == "") { mod = "qetrix"; } // Handle keywords, add trailing underscore; '/list' in URL => function list_(Dict $args) var keywords = new string[] { "abstract", "and", "as", "bool", "break", "case", "catch", "class", "const", "continue", "declare", "default", "die", "do", "echo", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach", "endif", "endswitch", "endwhile", "eval", "exit", "extends", "false", "final", "float", "for", "foreach", "function", "global", "goto", "if", "implements", "include", "include_once", "int", "instanceof", "insteadof", "interface", "isset", "list", "mixed", "namespace", "new", "null", "numeric", "or", "print", "private", "protected", "public", "require", "require_once", "resource", "return", "scalar", "static", "string", "switch", "throw", "trait", "true", "try", "unset", "use", "var", "while", "xor" }; if (keywords.Contains(mod)) { mod = mod + "_"; } keywords = keywords.Union(new string[] { "ds", "init", "page", "qmodule", "qpage", "stage" }).ToArray(); if (keywords.Contains(func)) { func = func + "_"; } var myType = loadAssembly(AssemblyType.module, mod); string methodName = ""; var args = new Dict(this._args); if (page.has("id")) { args.set("id", page.get("id")); } try { var modObject = Activator.CreateInstance(myType, this); MethodInfo method; method = myType.GetMethod("init"); method.Invoke(modObject, new object[] { args }); method = myType.GetMethod(func); if (method == null) { method = myType.GetMethod("main"); args.set("0", func); // TODO: Will it be always "0"?? func = "main"; } if (method == null) { throw new Exception(string.Format("Method {0} not found in module {1}", page.get("func"), mod)); } methodName = method.Name; var output = method.Invoke(modObject, new object[] { args }); this.sessionManager(); // Write session data return(output.ToString()); } catch (TargetParameterCountException ex) { return(string.Format("Err#xx: Method \"{0}\" must have following signature: public string {0}(Dict args);", methodName));; } catch (Exception ex) { var exx = ex.InnerException == null ? ex : (ex.InnerException.InnerException == null ? ex.InnerException : ex.InnerException.InnerException); return("Exception: " + exx.Message + " at " + exx.Source + "\n" + String.Join("\n", exx.StackTrace)); } } _statusCode = 404; return("404 Not Found"); }
public Dict parsePath(string path) { var aPath = new List <string>(path.Trim(new char[] { '/' }).Split('/')); var page = new Dict(); if (aPath.Count == 0) { return(page); } if (this._config["appName"] == "") { this._config["appName"] = aPath[0]; aPath.RemoveAt(0); } page.set("app", this._config["appName"]); if (aPath.Count > 0) { page.set(int.TryParse(aPath[0], out int n) ? "id" : "mod", aPath[0]); aPath.RemoveAt(0); } else { page.set("mod", this._config["appName"]); } if (aPath.Count > 0) { page.set(int.TryParse(aPath[0], out int n) ? "id" : "func", aPath[0]); aPath.RemoveAt(0); } if (aPath.Count > 0) { if (!page.has("id") && int.TryParse(aPath[0], out int n)) { page.set("id", aPath[0]); aPath.RemoveAt(0); } else if (!page.has("func")) { page.set("func", aPath[0]); } } if (!page.has("func")) { page.set("func", "main"); } // TODO: Process App Config // ... this.sessionManager(); // Read session data (Must be AFTER config!) if (aPath.Count > 0) { this._args = aPath.Select((s, i) => new { s, i }).ToDictionary(x => x.i.ToString(), x => x.s); this._args.ToList().ForEach(x => this._data.Add(x.Key, x.Value)); //this._args = aPath.Distinct().ToDictionary(x => x); } return(page); }