diff options
author | Ted Trask <ttrask01@yahoo.com> | 2012-01-19 16:11:08 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2012-01-19 16:11:08 +0000 |
commit | 2b3a7472f9b4c3c25e1fe7d607d5d0df7eac90e8 (patch) | |
tree | bb02ada58a7fc3540a866c1a2a7a962a6f6064be | |
parent | 7f7a661bc5ced049d776da54ac44db2fbb1e4b81 (diff) | |
download | acf-core-2b3a7472f9b4c3c25e1fe7d607d5d0df7eac90e8.tar.bz2 acf-core-2b3a7472f9b4c3c25e1fe7d607d5d0df7eac90e8.tar.xz |
Starting to reorganize mvc.lua and acf_www controllers to move www-specific things from mvc to acf_www, and visa versa
Goal is to have mvc.lua more usable from a lua app
Also affects acf-cli and acf_cli controller
-rw-r--r-- | app/acf_cli-controller.lua | 7 | ||||
-rw-r--r-- | app/acf_www-controller.lua | 56 | ||||
-rwxr-xr-x | bin/acf-cli | 8 | ||||
-rwxr-xr-x | lua/mvc.lua | 129 |
4 files changed, 116 insertions, 84 deletions
diff --git a/app/acf_cli-controller.lua b/app/acf_cli-controller.lua index 3d4a3e9..295bd47 100644 --- a/app/acf_cli-controller.lua +++ b/app/acf_cli-controller.lua @@ -8,7 +8,8 @@ mvc.on_load = function (self, parent) self.conf.libdir = self.conf.libdir or ( string.match(self.conf.appdir, "[^,]+/") .. "/lib/" ) self.conf.script = "" self.conf.default_prefix = "/acf-util/" - self.conf.default_controller = "welcome" + self.conf.default_controller = "welcome" + self.conf.viewtype = "serialized" -- this sets the package path for us and our children for p in string.gmatch(self.conf.libdir, "[^,]+") do @@ -25,13 +26,13 @@ end mvc.post_exec = function () end - +--[[ view_resolver = function(self) return function (viewtable) print(session.serialize("result", viewtable)) end end - +--]] --[[ The parent exception handler is just fine exception_handler = function (self, message ) print(session.serialize("exception", message)) diff --git a/app/acf_www-controller.lua b/app/acf_www-controller.lua index 12c80ea..3aeded5 100644 --- a/app/acf_www-controller.lua +++ b/app/acf_www-controller.lua @@ -13,6 +13,7 @@ require "posix" -- We use the parent exception handler in a last-case situation local parent_exception_handler +local parent_create_helper_library local function build_menus(self) m=require("menubuilder") @@ -78,7 +79,7 @@ end -- look for a template -- ctlr-action-view, then ctlr-view, then action-view, then view --- cannot be local function because of recursion +local find_template find_template = function ( appdir, prefix, controller, action, viewtype ) if string.find(appdir, ",") then local template @@ -114,35 +115,6 @@ find_template = function ( appdir, prefix, controller, action, viewtype ) return find_template ( appdir, prefix, controller, action, viewtype ) end --- look for a view --- ctlr-action-view, then ctlr-view -local find_view = function ( appdir, prefix, controller, action, viewtype ) - for p in string.gmatch(appdir, "[^,]+") do - local names = { p .. prefix .. controller .. "-" .. - action .. "-" .. viewtype .. ".lsp", - p .. prefix .. controller .. "-" .. - viewtype .. ".lsp" } - local file - -- search for view - for i,filename in ipairs (names) do - file = io.open(filename) - if file then - file:close() - return filename - end - end - end - return nil -end - -local has_view = function(self) - for p in string.gmatch(self.conf.appdir, "[^,]+") do - local file = posix.stat(p .. self.conf.prefix .. self.conf.controller .. "-" .. self.conf.action .. "-" .. (self.conf.viewtype or "html") .. ".lsp", "type") - if file == "regular" or file == "link" then return true end - end - return false -end - -- This function is made available within the view to allow loading of components local dispatch_component = function(self, str, clientdata, suppress_view) -- Before we call dispatch, we have to set up conf and clientdata like it was really called for this component @@ -172,8 +144,18 @@ local dispatch_component = function(self, str, clientdata, suppress_view) return viewtable end -local create_helper_library = function ( self ) - local library = {} +local has_view = function(self) + for p in string.gmatch(self.conf.appdir, "[^,]+") do + local file = posix.stat(p .. self.conf.prefix .. self.conf.controller .. "-" .. self.conf.action .. "-" .. self.conf.viewtype .. ".lsp", "type") + if file == "regular" or file == "link" then return true end + end + return false +end + +-- Override the mvc create_helper_library function to add our functions +create_helper_library = function ( self ) + -- Call the mvc version + local library = parent_create_helper_library(self) --[[ -- If we have a separate library, here's how we could do it local library = require("library_name") for name,func in pairs(library) do @@ -187,8 +169,8 @@ local create_helper_library = function ( self ) return library end --- Our local view resolver called by our dispatch -local view_resolver = function(self) +-- Our local view resolver called by our dispatch - add the template and skin +view_resolver = function(self) local template, viewname, viewlibrary local viewtype = self.conf.viewtype or "html" @@ -212,7 +194,7 @@ local view_resolver = function(self) end -- create the view helper library - viewlibrary = create_helper_library ( self ) + viewlibrary = self:create_helper_library() local pageinfo = { viewfile = viewname, controller = self.conf.controller, @@ -252,9 +234,7 @@ mvc.on_load = function (self, parent) self.conf.clientip = ENV.REMOTE_ADDR parent_exception_handler = parent.exception_handler - - -- this sets the package path for us and our children - package.path = string.gsub(self.conf.libdir, ",", "/?.lua;") .. "/?.lua;" .. package.path + parent_create_helper_library = parent.create_helper_library sessionlib=require ("session") diff --git a/bin/acf-cli b/bin/acf-cli index c804c97..a0560a7 100755 --- a/bin/acf-cli +++ b/bin/acf-cli @@ -19,12 +19,7 @@ Output will be a serialized Lua table. end require("posix") ---local PATH = package.path ---local p = posix.dirname(arg[0]) ---if p:sub(1,1) ~= "/" then p = posix.getcwd().."/"..p end ---package.path = p.."/?.lua;" .. package.path mvc = require("acf.mvc") ---package.path = PATH -- this is to get around having to store -- the config file in /etc/helloworld/helloworld.conf @@ -38,7 +33,6 @@ APP=FRAMEWORK:new("acf_cli") -- (put into ENV.PATH_INFO) -- followed by parameters -- (put into APP.clientdata) -ENV.PATH_INFO = arg[1] APP.clientdata = {} for i=2,#arg do a,v = string.match(arg[i], "([^=]*)=(.*)") @@ -48,6 +42,6 @@ for i=2,#arg do APP.clientdata[arg[i]] = true end end -APP:dispatch() +APP:dispatch(APP.parse_path_info(arg[1])) APP:destroy() FRAMEWORK:destroy() diff --git a/lua/mvc.lua b/lua/mvc.lua index 41aa9db..ca5bc8e 100755 --- a/lua/mvc.lua +++ b/lua/mvc.lua @@ -128,14 +128,9 @@ dispatch = function (self, userprefix, userctlr, useraction) local controller = nil local success, err = xpcall ( function () - if userprefix == nil then - self.conf.prefix, self.conf.controller, self.conf.action = - parse_path_info(ENV["PATH_INFO"]) - else - self.conf.prefix = userprefix - self.conf.controller = userctlr or "" - self.conf.action = useraction or "" - end + self.conf.prefix = userprefix or "/" + self.conf.controller = userctlr or "" + self.conf.action = useraction or "" -- If they didn't provide a controller, and a default was specified -- use it @@ -182,13 +177,17 @@ dispatch = function (self, userprefix, userctlr, useraction) controller.worker.mvc.post_exec ( controller ) end - local viewfunc = controller:view_resolver() - + if not self.conf.suppress_view then + local viewfunc, p1, p2, p3 = controller:view_resolver() + viewfunc (viewtable, p1, p2, p3) + end + -- we're done with the controller, destroy it controller:destroy() controller = nil - - viewfunc (viewtable) + + return viewtable + end, self:soft_traceback(message) ) @@ -237,17 +236,14 @@ soft_require = function (self, name ) end -- look in various places for a config file, and store it in self.conf -read_config = function( self, appname ) +read_config = function( self, appname, home ) appname = appname or self.conf.appname self.conf.appname = self.conf.appname or appname - - local confs = { (ENV["HOME"] or ENV["PWD"] or "") .. "/." .. - appname .. "/" .. appname .. ".conf", - ( ENV["HOME"] or ENV["PWD"] or "") .. "/" .. - appname .. ".conf", - ENV["ROOT"] or "" .. "/etc/" .. appname .. "/" .. - appname .. ".conf", - ENV["ROOT"] or "" .. "/etc/" .. appname .. ".conf" + + local confs = { (home or "") .. "/." .. appname .. "/" .. appname .. ".conf", + (home or "") .. "/" .. appname .. ".conf", + "/etc/" .. appname .. "/" .. appname .. ".conf", + "/etc/" .. appname .. ".conf" } for i, filename in ipairs (confs) do local file = io.open (filename) @@ -265,6 +261,11 @@ read_config = function( self, appname ) end end + -- this sets the package path for us and our children + if self.conf.libdir then + package.path = string.gsub(self.conf.libdir, ",", "/?.lua;") .. "/?.lua;" .. package.path + end + if (#self.conf.confdir) then -- check for an appname-hooks.lua file self.conf.app_hooks = {} setmetatable (self.conf.app_hooks, {__index = _G}) @@ -298,16 +299,73 @@ parse_path_info = function( str ) return prefix, controller, action end +-- look for a view +-- ctlr-action-view, then ctlr-view +find_view = function ( appdir, prefix, controller, action, viewtype ) + if not viewtype then return nil end + for p in string.gmatch(appdir, "[^,]+") do + local names = { p .. prefix .. controller .. "-" .. + action .. "-" .. viewtype .. ".lsp", + p .. prefix .. controller .. "-" .. + viewtype .. ".lsp" } + local file + -- search for view + for i,filename in ipairs (names) do + file = io.open(filename) + if file then + file:close() + return filename + end + end + end + return nil +end + +create_helper_library = function ( self ) + local library = {} +--[[ -- If we have a separate library, here's how we could do it + local library = require("library_name") + for name,func in pairs(library) do + if type(func) == "function" then + library.name = function(...) return func(self, ...) end + end + end +--]] + return library +end + +-- The view of last resort +auto_view = function() +end + -- The view resolver of last resort. view_resolver = function(self) - return function() - if ENV["REQUEST_METHOD"] then - io.write ("Content-type: text/plain\n\n") - end - io.write ("Your controller and application did not specify a view resolver.\n") - io.write ("The MVC framework has no view available. sorry.\n") - return + local viewname, viewlibrary + + -- search for view + viewname = find_view ( self.conf.appdir, self.conf.prefix, + self.conf.controller, self.conf.action, self.conf.viewtype ) + + local func = auto_view + if viewname then + func = haserl.loadfile (viewname) end + + -- create the view helper library + viewlibrary = create_helper_library ( self ) + + local pageinfo = { viewfile = viewname, + controller = self.conf.controller, + action = self.conf.action, + prefix = self.conf.prefix, + script = self.conf.script, + wwwprefix = self.conf.wwwprefix or "", + staticdir = self.conf.staticdir or "", + orig_action = self.conf.orig_action or self.conf.prefix .. self.conf.controller .. "/" .. self.conf.action, + clientdata = self.clientdata, + } + + return func, viewlibrary, pageinfo, self.sessiondata end -- Generates a debug.traceback if called with no arguments @@ -321,21 +379,20 @@ end -- The exception hander of last resort exception_handler = function (self, message ) - if ENV["REQUEST_METHOD"] then - print ("Content-Type: text/plain\n\n") - end - print ("The following unhandled application error occured:\n\n") + self.logevent ("The following unhandled application error occured:\n\n") if (type(message) == "table" ) then if (message.type == "dispatch") then - print ('controller: "' .. message.controller .. '" does not have a "' .. - message.action .. '" action.') + logevent ('controller: "' .. message.controller .. '" does not have a "' .. message.action .. '" action.') else - print ("An error of type: '" .. (tostring(message.type) or "nil") .. "' was raised." ) + logevent ("An error of type: '" .. (tostring(message.type) or "nil") .. "' was raised." ) end else - print (tostring(message)) + logevent (tostring(message)) end + + -- Pass the exception to the calling function + error(message) end -- create a Configuration Framework Entity (cfe) |