1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
--[[ Basic MVC framework
Written for Alpine Configuration Framework (ACF) -- see www.alpinelinux.org
Copyright (C) 2007 Nathan Angelacos
Licensed under the terms of GPL2
]]--
module(..., package.seeall)
-- the constructor
--[[ Builds a new MVC object. If "module" is given, then tries to load
self.conf.appdir .. module "-controller.lua" in c.worker and
self.conf.appdir .. module "-model.lua" in c.model
The returned .conf table is guaranteed to have the following
appdir - where the application lives
confdir - where the configuration file is
sessiondir - where session data and other temporary stuff goes
appname - the name of the application
]]
new = function (self, modname)
local c = {}
c.worker = {}
c.model = {}
-- make defaults if the parent doesn't have them
if self.conf == nil then
c.conf = { appdir = "", confdir = "", tempdir = "", appname = "" }
end
-- If no clientdata, then clientdata is a null table
if self.clientdata == nil then
c.clientdata = {}
end
-- If we don't have an application name, use the modname
if (self.conf == nil ) or (self.conf.appname == nil) then
c.conf.appname = modname
end
-- load the module code here
if (modname) then
c.worker = self:soft_require( modname .. "-controller") or {}
c.model = self:soft_require( modname .. "-model" ) or {}
end
-- The magic that makes all the metatables point in the correct
-- direction. c.model -> c.worker -> parent -> parent.worker ->
-- grandparent -> grandparent -> worker (and so on)
-- The model looks in worker for missing
setmetatable (c.model, c.model )
c.model.__index = c.worker
-- the worker looks in the main table for missing
setmetatable (c.worker, c.worker)
c.worker.__index = c
-- the table looks in the parent worker for missing
setmetatable (c, c)
-- ensure an "mvc" table exists, even if empty
if (type(rawget(c.worker, "mvc")) ~= "table") then
c.worker.mvc = {}
end
setmetatable (c.worker.mvc, c.worker.mvc)
-- If creating a new parent container, then
-- we are the top of the chain.
if (modname) then
c.__index = self.worker
c.worker.mvc.__index = self.worker.mvc
else
c.__index = self
c.worker.mvc.__index = self.mvc
end
-- run the worker on_load code
if type(rawget(c.worker.mvc, "on_load")) == "function" then
c.worker.mvc.on_load(c, self)
c.worker.mvc.on_load = nil
end
return c
end
-- This is a sample front controller/dispatch.
dispatch = function (self)
local controller
local success, err = xpcall ( function ()
self:parse_path_info(ENV["PATH_INFO"])
-- If they didn't provide a controller, and a default was specified
-- use it
if self.conf.controller == "" and self.conf.default_controller then
self.conf.controller = self.conf.default_controller
end
controller = self:new(self.conf.prefix .. self.conf.controller)
local action = controller.conf.action
-- Because of the inheritance, normally the
-- controller.worker.action will flow up, so that all children have
-- actions of all parents. We sue rawget to make sure that only
-- controller defined actions are used on dispatch
-- If the controller or action are missing, raise an error
if ( type(rawget(controller.worker, action)) ~= "function") then
self.conf.type = "dispatch"
error (self.conf)
end
-- run the (first found) pre_exec code, starting at the controller
-- and moving up the parents
if type(controller.worker.mvc.pre_exec) == "function" then
controller.worker.mvc.pre_exec ( controller )
end
-- run the action
local viewtable = controller.worker[action](controller)
-- run the post_exec code
if type(controller.worker.mvc.post_exec) == "function" then
controller.worker.mvc.post_exec ( controller )
end
local viewfunc = controller:view_resolver(viewtable)
viewfunc (viewtable)
end,
self:soft_traceback(message)
)
if not success then
local handler
if controller then
handler = controller.worker or controller
end
handler = handler or self.worker or mvc
handler:exception_handler(err)
end
end
-- Tries to see if name exists in the self.conf.appdir, and if so, it loads it.
-- otherwise, returns nil, but no error
soft_require = function (self, name )
local filename, file
filename = self.conf.appdir .. name .. ".lua"
file = io.open(filename)
if file then
file:close()
local PATH=package.path
-- FIXME - this should really try to open the lua file, and if it doesnt exist-
-- then silently fail -- This version allows things from /usr/local/lua/5.1 to
-- be loaded - but that's because a module might require another module
package.path = self.conf.appdir .. "?" .. ".lua;" .. package.path
local t = require(name)
package.path = PATH
return t
end
return nil
end
-- see man basename.1
basename = function (string, suffix)
string = string or ""
local basename = string.gsub (string, "[^/]*/", "")
if suffix then
basename = string.gsub ( basename, suffix, "" )
end
return basename
end
-- see man dirname.1
dirname = function ( string)
string = string or ""
-- strip trailing / first
string = string.gsub (string, "/$", "")
local basename = basename ( string)
string = string.sub(string, 1, #string - #basename - 1)
return(string)
end
-- look in various places for a config file, and store it in self.conf
read_config = function( self, appname )
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",
-- dirname(ENV["SCRIPT_FILENAME"] or "") .. appname .. ".conf",
ENV["ROOT"] or "" .. "/etc/" .. appname .. "/" .. appname .. ".conf",
ENV["ROOT"] or "" .. "/etc/" .. appname .. ".conf"
}
for i, filename in ipairs (confs) do
local file = io.open (filename)
if (file) then
self.conf.confdir = dirname(filename) .. "/"
for line in file:lines() do
key, value = string.match(line, "([^[=]*)=[ \t]*(.*)")
if key then -- ugly way of finding blank spots between key and =
repeat
local space = string.find ( key, "%s", -1)
if space then key=string.sub(key,1,space-1) end
until space == nil
self.conf[key] = value
end
end
file:close()
break
end
end
end
-- parse a "URI" like string into a prefix, controller and action
-- store them (or blanks) in self.conf
parse_path_info = function( self, string )
string = string or ""
-- If it ends in a /, then add another to force
-- a blank action (the user gave a controller without action)
if string.match (string, "[^/]/$" ) then
string = string .. "/"
end
self.conf.action = basename(string)
local temp = dirname(string)
self.conf.controller = basename(temp)
self.conf.prefix = dirname(temp) .. "/"
return self.conf.prefix, self.conf.controller, self.conf.action
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
end
end
-- Generates a debug.traceback if called with no arguments
soft_traceback = function (self, message )
if message then
return message
else
return debug.traceback
end
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")
if (type(message) == "table" ) then
if (message.type == "dispatch") then
print ('controller: "' .. message.controller .. '" does not have a "' ..
message.action .. '" action.')
else
print ("An error of type: '" .. (tostring(message.type) or "nil") .. "' was raised." )
end
else
print (tostring(message))
end
end
|