X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=83e7efb16c0b75d93174e4cd43cd8914571bcf4a;hb=9b2bc37b4bcee56036419a392d922f053a130420;hp=a1ec31353e1192a1fc56dd37d962269cc10a55b2;hpb=fc7ec1d96ee55d1bf42af3abce155ecb717b9e2b;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index a1ec313..83e7efb 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -3,7 +3,6 @@ package Catalyst::Engine; use strict; use base qw/Class::Data::Inheritable Class::Accessor::Fast/; use UNIVERSAL::require; -use B; use Data::Dumper; use HTML::Entities; use HTTP::Headers; @@ -68,8 +67,7 @@ sub action { $_[1] ? ( $action = {@_} ) : ( $action = shift ); if ( ref $action eq 'HASH' ) { while ( my ( $name, $code ) = each %$action ) { - my $class = B::svref_2object($code)->STASH->NAME; - my $caller = caller(0); + my $class = caller(0); if ( $name =~ /^\/(.*)\/$/ ) { my $regex = $1; $self->actions->{compiled}->{qr/$regex/} = $name; @@ -77,19 +75,18 @@ sub action { } elsif ( $name =~ /^\?(.*)$/ ) { $name = $1; - $name = _prefix( $caller, $name ); + $name = _prefix( $class, $name ); $self->actions->{plain}->{$name} = [ $class, $code ]; } elsif ( $name =~ /^\!\?(.*)$/ ) { $name = $1; - $name = _prefix( $caller, $name ); + $name = _prefix( $class, $name ); $name = "\!$name"; $self->actions->{plain}->{$name} = [ $class, $code ]; } else { $self->actions->{plain}->{$name} = [ $class, $code ] } $self->actions->{reverse}->{"$code"} = $name; - $self->log->debug( - qq/"$caller" defined "$name" as "$code" from "$class"/) + $self->log->debug(qq/"$class" defined "$name" as "$code"/) if $self->debug; } } @@ -340,6 +337,13 @@ sub forward { $command = _prefix( $caller, $command ); $command = "\!$command"; } + elsif ( $command =~ /^\!(.*)$/ ) { + my $try = $1; + my $caller = caller(0); + my $prefix = _class2prefix($caller); + $try = "!$prefix/$command"; + $command = $try if $c->actions->{plain}->{$try}; + } my ( $class, $code ); if ( my $action = $c->action($command) ) { ( $class, $code ) = @{ $action->[0] }; @@ -378,9 +382,11 @@ sub handler { eval { my $handler = sub { my $c = $class->prepare($r); - if ( $c->req->action ) { + if ( my $action = $c->action( $c->req->action ) ) { my ( $begin, $end ); - if ( my $prefix = $c->req->args->[0] ) { + my $class = ${ $action->[0] }[0]; + my $prefix = _class2prefix($class); + if ($prefix) { if ( $c->actions->{plain}->{"\!$prefix/begin"} ) { $begin = "\!$prefix/begin"; } @@ -392,6 +398,12 @@ sub handler { } elsif ( $c->actions->{plain}->{'!end'} ) { $end = '!end' } } + else { + if ( $c->actions->{plain}->{'!begin'} ) { + $begin = '!begin'; + } + if ( $c->actions->{plain}->{'!end'} ) { $end = '!end' } + } $c->forward($begin) if $begin; $c->forward( $c->req->action ) if $c->req->action; $c->forward($end) if $end; @@ -400,7 +412,7 @@ sub handler { my $action = $c->req->path; my $error = $action ? qq/Unknown resource "$action"/ - : "Congratulations, you're on Catalyst!"; + : "No default action defined"; $c->log->error($error) if $c->debug; $c->errors($error); } @@ -479,7 +491,7 @@ sub prepare_action { my @path = split /\//, $c->req->path; $c->req->args( \my @args ); while (@path) { - my $path = join '/', @path; + $path = join '/', @path; if ( my $result = $c->action($path) ) { # It's a regex @@ -710,11 +722,17 @@ sub stash { sub _prefix { my ( $class, $name ) = @_; + my $prefix = _class2prefix($class); + $name = "$prefix/$name" if $prefix; + return $name; +} + +sub _class2prefix { + my $class = shift; $class =~ /^.*::[(M)(Model)(V)(View)(C)(Controller)]+::(.*)$/; my $prefix = lc $1 || ''; $prefix =~ s/\:\:/_/g; - $name = "$prefix/$name" if $prefix; - return $name; + return $prefix; } =head1 AUTHOR