X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FApache%2FMVC.pm;h=467f0f97b83ffb5f01c2d55b59a3bb191c3513f8;hb=d635ca733a7199c4f9893e8d5a50e96a1295f092;hp=f567badb87ceaf00c650ca6f10b9b8d99b2d8074;hpb=feefb416b3c1aafdea07aa47378d007ee760e9f9;p=maypole.git diff --git a/lib/Apache/MVC.pm b/lib/Apache/MVC.pm index f567bad..467f0f9 100644 --- a/lib/Apache/MVC.pm +++ b/lib/Apache/MVC.pm @@ -2,15 +2,26 @@ package Apache::MVC; use base qw(Class::Accessor Class::Data::Inheritable); use attributes (); use Class::DBI::Loader; +use UNIVERSAL::require; +use Apache::Constants ":common"; use strict; use warnings; our $VERSION = "1.0"; - __PACKAGE__->mk_classdata($_) for qw( _config init_done view_object ); -__PACKAGE__->mk_accessors ( qw( config ar params objects model_class args )); +__PACKAGE__->mk_accessors ( qw( config ar params objects model_class +args action template )); __PACKAGE__->config({}); __PACKAGE__->init_done(0); + +sub import { + my $real = shift; + if ($real ne "Apache::MVC") { + no strict 'refs'; + *{$real."::handler"} = sub { Apache::MVC::handler($real, @_) }; + } +} + # This is really dirty. sub config { my $self = shift; @@ -21,25 +32,31 @@ sub config { sub set_database { my ($calling_class, $dsn) = @_; $calling_class = ref $calling_class if ref $calling_class; - $calling_class->config->{dsn} = $dsn; - $calling_class->config->{loader} = Class::DBI::Loader->new( + my $config = $calling_class->config; + $config->{model} ||= "Apache::MVC::Model::CDBI"; + $config->{model}->require; + $config->{dsn} = $dsn; + $config->{loader} = Class::DBI::Loader->new( namespace => $calling_class, dsn => $dsn ); + $config->{classes} = [ $config->{loader}->classes ]; + for my $subclass (@{$config->{classes}}) { + no strict 'refs'; + unshift @{$subclass."::ISA"}, $config->{model}; + $config->{model}->adopt($subclass) + if $config->{model}->can("adopt"); + } } sub init { my $class = shift; my $config = $class->config; - $config->{model} ||= "Apache::MVC::Model::CDBI"; $config->{view} ||= "Apache::MVC::View::TT"; - $config->{classes} = [ $class->config->{loader}->classes ]; + $config->{view}->require; $config->{display_tables} ||= [ $class->config->{loader}->tables ]; - for my $class (@{$config->{classes}}) { - no strict 'refs'; - push @{$class."::ISA"}, $class->config->{model}; - } $class->view_object($class->config->{view}->new); + $class->init_done(1); } @@ -50,19 +67,22 @@ sub class_of { sub handler { # See Apache::MVC::Workflow before trying to understand this. - my $class = (caller(0))[0]; + my $class = shift; $class->init unless $class->init_done; my $r = bless { config => $class->config }, $class; $r->get_request(); $r->parse_location(); + $r->model_class($r->class_of($r->{table})); my $status = $r->is_applicable; - return $status unless $status == 200; + return $status unless $status == OK; $status = $r->call_authenticate; - return $status unless $status == 200; - $r->find_objects(); + return $status unless $status == OK; $r->additional_data(); - $r->class->process($r); + + $r->model_class->process($r); + $r->view_object->process($r); + return $r; # For debugging. } sub get_request { @@ -73,36 +93,47 @@ sub get_request { sub parse_location { my $self = shift; - my @pi = split /\//, $self->{ar}->uri(); + my $uri = $self->{ar}->path_info(); + my @pi = split /\//, $uri; shift @pi while @pi and !$pi[0]; $self->{table} = shift @pi; $self->{action} = shift @pi; $self->{args} = \@pi; + + $self->{params} = { $self->{ar}->content }; } sub is_applicable { my $self = shift; - require Apache::Constants; - Apache::Constants->import(":common"); my $config = $self->config; - my %ok = map {$_ => 1} @{$config->{displaying_tables}}; - return DECLINED() unless exists $ok{$self->{table}}; + $config->{ok_tables} = {map {$_ => 1} @{$config->{display_tables}}}; + warn "We don't have that table ($self->{table})" + unless $config->{ok_tables}{$self->{table}}; + return DECLINED() unless exists $config->{ok_tables}{$self->{table}}; # Does the action method exist? + # XXX We should set the method class to the class for the table my $cv = $self->model_class->can($self->{action}); + warn "We don't have that action ($self->{action})" unless $cv; return DECLINED() unless $cv; # Is it exported? $self->{method_attribs} = join " ", attributes::get($cv); + do { warn "$self->{action} not exported"; return DECLINED() - unless $self->{method_attribs} =~ /\b(Exported|Class|Single|Multiple)\b/i; + } unless $self->{method_attribs} =~ /\bExported\b/i; return OK(); } -sub find_objects { - # First, how many arguments are we? +sub call_authenticate { + my $self = shift; + return $self->model_class->authenticate($self) if + $self->model_class->can("authenticate"); + return $self->authenticate(); } -sub authenticate { return 200 } +sub additional_data {} + +sub authenticate { return OK } 1;