-
Notifications
You must be signed in to change notification settings - Fork 0
/
Assumptions.pm
558 lines (471 loc) · 12.8 KB
/
Assumptions.pm
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package Assumptions;
use strict;
# make these dependend on goings on in import
use CGI;
use Cache::FileCache;
use Template;
use DBI;
use CGI::Session;
use Cwd;
use Data::Dumper;
our $AUTOLOAD;
our $debug = 0;
#use vars qw($cgi $template $dbh $sent_headers $sent_body);
sub new {
my $class = shift;
bless {
has_setup => undef,
sent_headers=> undef,
sent_body=> undef,
caller=> caller,
server_errors=>[],
client_errors=>[],
} , $class;
}
sub setup {
# print @_;
print "SETTING UP\n" if $debug;
my $app = shift;
eval {
$app->configure if not $app->{has_setup};
};
eval {
&{shift()};
};
$app->{has_setup} = 1;
$app->set_cgi;
$app->set_template;
$app->parse_url;
#$app->caller(caller);
}
#~ sub configure {
#~ }
sub log_server_error {
my $app = shift;
push @{$app->{server_errors}}, @_;
}
sub log_client_error {
my $app = shift;
push @{$app->{client_errors}}, @_;
}
sub dump {
my $app = shift;
if (scalar(@_)) {
return Dumper(@_);
} else {
return Dumper($app);
}
}
sub parse_url {
my $app = shift;
if (!$app->has_parsed_url) {
my $url = $app->cgi->url(-path_info=>1);
my $path_info = $app->cgi->path_info;
$url =~ s/$path_info$//i;
$app->{url} = $url;
my @facts = split m|/|, $path_info;
shift @facts; # remove empty leading element
$app->{action} = shift @facts;
$app->{ident} = shift @facts;
$app->{path_params} = \@facts;
$app->{has_parsed_url} = 1;
}
# $app->{script} = $ENV
return ($app->action, $app->ident, $app->object_type);
}
# TODO implementable url manipulation
sub route {
}
# Simplyfy to plain concat (much.easier.to.use)
# If needs be I can homegrow other ones that do "smarts"
sub url_for {
my $app = shift;
my $url = $app->url . '/' . join('/', @_);
$url =~ s|([^:])/+|$1/|g; # all double slashes without a leading : to singles
return $url;
}
sub set_cache {
my $app = shift;
return $app->cache if $app->cache;
my $root = shift;
my $namespace = shift;
my $expiration = shift || '10 days'; # arbitrary
$app->cache(new Cache::FileCache( {cache_root=> $root, namespace => $namespace, 'default_expires_in' => $expiration }));
}
sub set_dbh {
my ($app, $dbname, $user, $password) = @_;
return $app->dbh if $app->dbh;
$app->dbh(DBI->connect("dbi:mysql:$dbname", $user, $password));
}
sub set_dbix {
my ($app, $dbname, $user, $password) = @_;
use DBIx::Abstract;
$app->set_dbh($dbname, $user, $password);
$app->dbix(DBIx::Abstract->connect($app->dbh));
}
sub db {
my $app = shift;
return $app->{db} if $app->{db};
$app->{db} = bless {app=>$app} , 'Assumptions::DB';
return $app->{db};
}
sub check_db_err {
my $app = shift;
if ($app->dbh && $app->dbh->err) {
$app->log_server_error($app->dbh->err);
}
}
sub set_template {
my ($app, $settings) = @_;
print "Template dir : $settings\n" if $debug;
return $app->template if $app->template;
$app->use_data_templates(1) if not defined($settings);
if (ref($settings)) {
$app->template(Template->new($settings));
} else {
$app->template(Template->new({ INCLUDE_PATH=>$settings,
INTERPOLATE => 1, # expand "$var" in plain text
POST_CHOMP => 1, # cleanup whitespace
# PRE_PROCESS => 'header', # prefix each template
EVAL_PERL => 1, # evaluate Perl code blocks
}));
}
}
sub set_cgi {
my $app = shift;
return $app->cgi if $app->cgi;
$app->cgi(new CGI) if not $app->cgi;
# TODO various translation and url computation data prebaked
}
sub set_session {
my $app = shift;
my $config = shift;
$app->set_cgi;
if (ref($config) eq 'DBI::db') {
$app->{session} = CGI::Session->new("driver:mysql", $app->cgi, { Handle => $config } );
} else {
$app->{session} = CGI::Session->new(undef, $app->cgi, {Directory=>$config});
}
}
sub handle_errors {
my $app = shift;
print $app->cgi->header('text/plain');
print join "\n---\n" , @{$app->server_errors};
$app->sent_headers(1);
$app->sent_body(1);
}
sub headers {
my $app = shift;
return 1 if $app->sent_headers;
if ($app->session) {
$app->session->flush();
print $app->session->header(@_);
} else {
print $app->cgi->header(@_);
}
$app->sent_headers(1);
}
sub redirect {
my $app = shift;
if (!$app->sent_headers) {
my $url = shift;
print $app->cgi->redirect($url);
$app->sent_headers(1);
$app->sent_body(1);
}
}
sub render {
my $app = shift;
if ($app->use_data_templates) { # use inline templates
print "no templates" if $debug;
my $export = {app=>$app};
$app->template->process(\*main::DATA, $export);
} else {
my $templatename = shift;
if ($templatename !~ /\./) {
$templatename .= '.html';
}
$app->{templatename} = $templatename;
print "template '$templatename'\n" if $debug;
my $export = {app=>$app};
if (!$app->sent_body) {
$app->template->process($templatename, $export) || $app->log_server_error($app->template->error);
}
}
$app->sent_body(1);
}
sub handle {
my $app = shift;
if (scalar(@{$app->server_errors})) {
$app->handle_errors;
} else {
$app->headers;
$app->render(@_);
}
if (scalar(@{$app->server_errors})) {
$app->handle_errors;
}
}
sub dispatch {
print "DISPATCHING\n" if $debug;
my $app = shift;
$app->setup if not $app->{has_setup};
$app->{in_dispatch} = 'true';
my $action = $app->action || 'index' ;
$action =~ s/\.html$//i; # this is a hack - for when my htaccess rule stupidly thinks i'm looking for the action index.html
my $namespace = $app->object_type || $app->caller;
#TODO - parse out package for data instead
my $data = undef;
eval {
no strict 'refs';
$app->{trying_to_call} = $app->caller . '::' . $action;
$data = &{$namespace . '::' . $action}($app);
$app->{got_data} = Dumper($data);
# print "Data = $data\n";
use strict 'refs';
};
$app->log_server_error($@) if $@; # $app->{controller_error} = $@;
$app->check_db_err;
$app->data($data);
$app->handle($action);
}
# TODO - avoid using autoload - or think about it and think about lvalues at least
sub AUTOLOAD {
my $app = shift;
my $value = shift;
my $key = $AUTOLOAD;
$key =~ s/^.*::([^:]+)$/$1/;
if (defined $value) {
$app->{$key} = $value;
}
return $app->{$key};
}
sub DESTROY {
}
# World's smallest DB framework, write your own SQL - except when it is tediously obvious
# utility package, DB live object with a save method, knows which table it belongs to
package Assumptions::DB;
our $AUTOLOAD;
sub _exists {
}
#~ sub _conversions {
#~ my $self = shift;
#~ if (scalar(@_)) {
#~ %conversions = @_;
#~ $self->{conversions} = \%conversions;
#~ }
#~ return $self->{conversions};
#~ }
# build a table object and return it
sub AUTOLOAD {
#~ print "WHAT??";
#~ my $package = shift;
my $self = shift;
my $key = $AUTOLOAD;
print "\n\nTABLE :: $key\n\n" if $debug;
$key =~ s/^.*::([^:]+)$/$1/;
#~ print "\n\nTABLE :: $key\n\n";
bless {app=>$self->{app}, name=>$key}, 'Assumptions::Table';
}
sub DESTROY {
}
package Assumptions::Table;
sub get {
my $self = shift;
# call DBIx::Abstract::select
#~ die ref($self->{app}->dbix;
my $parms = $self->{app}->dbix->select('*', $self->{name}, @_)->fetchrow_hashref;
$self->{app}->check_db_err;
if ($parms) {
return new Assumptions::Record $self, $parms;
} else {
return undef
}
}
sub add {
my $self = shift;
my $parms = shift || {};
return new Assumptions::Record $self, $parms;
}
# return existing or new record
sub addmod {
my $self = shift;
my $record = $self->get(@_);
if ($record) {
return $record;
}
else {
return $self->add;
}
}
# support cursors
sub get_all {
my $self = shift;
my $sql = shift;
my @records;
my $finder = $self->{app}->dbh->prepare($sql);
$finder->execute(@_);
$self->{app}->check_db_err;
while (my $parms = $finder->fetchrow_hashref) {
push @records , new Assumptions::Record $self, $parms;
}
$finder->finish;
return @records;
}
package Assumptions::Record;
our $AUTOLOAD;
sub new {
my $class = shift;
my $table = shift;
my $parms = shift;
# maybe statement handle for cursor
bless {
table =>$table,
fields => $parms
} , $class;
}
# for now just use get_all on the table class
sub new_cursor {
die 'TODO';
my $class = shift;
my $table = shift;
my $sth = shift;
# maybe statement handle for cursor
my $parms = $sth->fetchrow_hashref;
bless {
table =>$table,
sth => $sth,
fields => $parms,
} , $class;
}
sub set {
my $self = shift;
my %data = @_;
foreach my $key (keys %data) {
#~ print "setting $key = $data{$key}\n";
$self->fields->{$key} = $data{$key};
}
}
# optionally restrict by fieldnames
sub save {
my $self = shift;
my @fieldnames = @_;
my $fields = {id=>$self->{fields}->{id}};
if (scalar(@fieldnames)) {
foreach my $name (@fieldnames) {
#~ print "$name = $self->fields->{$name};";
$fields->{$name} = $self->fields->{$name};
}
} else {
$fields = $self->{fields};
}
if ($fields->{id}) {
#~ print "updating @fieldnames";
$self->{table}->{app}->dbix->update($self->{table}->{name}, $fields, {id=>$fields->{id}});
$self->{table}->{app}->check_db_err;
} else {
$self->{table}->{app}->dbix->insert($self->{table}->{name}, $fields);
$self->{table}->{app}->check_db_err;
$self->{fields}->{id} = $self->{table}->{app}->dbix->select_one_to_arrayref(' last_insert_id();')->[0];
$self->{table}->{app}->check_db_err;
}
}
# ? do cursors
# for now I just use get_all
sub next {
die 'TODO';
my $self = shift;
return undef if not $self->{sth};
if (my ($parms) = $self->{sth}->fetchrow_hashref) {
$self->{parms} = $parms;
return 1;
} else {
$self->{table}->{app}->check_db_err;
return undef;
}
}
# TODO - avoid using autoload
sub AUTOLOAD : lvalue { # yes, AUTOLOAD also supports the lvalue attribute
my $rec = shift;
my $value = shift;
my $key = $AUTOLOAD;
$key =~ s/^.*::([^:]+)$/$1/;
#~ return undef if not defined $rec->{fields}->{$key};
$rec->{fields}->{$key};
}
sub fields {
my $self = shift;
return $self->{fields};
}
sub DESTROY {
}
1;
__END__
Documentation
use Assumptions
# no structure behind. Directly in callme.cgi
sub index {
my $app = shift # get Assumptions
}
$app = new Assumptions;
$app->setup;
$app->dispatch;
(should be : Assumptions->dispatch)
(OR nothing at all because of an END clause in Assumptions - but then it's CGI only)
__END__
templates
or with a little more setup
callme.cgi
use MyApp;
sub index {
}
sub other {
return {stuff => "for template output"};
}
$app = new MyApp;
$app->setup;
$app->dispatch;
in MyApp.pm
use base Assumptions;
sub configure {
my $app = shift;
$app->set_template(dirname);
or
$app->set_template($app->scriptdir + '/templates');
or
$app->set_template({template toolkit options hash});
$app->set_cache
or
$app->set_cache($cachedir, $namespace)
$app->set_session
or
$app->set_session($session_cache_dir);
$app->set_dbix(mysqldbname, user, password)
}
sub use_utilities {
my $app = shift;
$my_row = $app->db->table_name->get({column=>value});
$other_row = $abb->db->table_name->new;
$my_row->property(set);
$other_row->property(define);
$other_row->save;
$my_row->save;
* cursors
? $value = $app->getcache("name");
if ($value) {
go on
} else {
create $value;
? $app->cache->setcache("name", $value, $expiration);
}
? $app->session->get("sessionvar", $user_specific_value);
? $app->session->set("sessionvar", $user_specific_value);
$app->header(CGI headers);
$app->render(Specific template);
$app->redirect;
* $app->mail(to=>someone, from=>someone, subject=>something, body=> something indeeed, anythingelse=>a header);
return {hash=> \%fortemplates, value=>1}
}
in templates
[% app.data.hash.key %] [% app.data.value %]
[% app.url_for(partialurl) %]