X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=b7d525ea494b3bf50c0f98f9b21939b6318302b2;hb=1927c219b552ac80dff59c235a51d422ac5b9d25;hp=2248f443b588edea03fa14fe8772e13bab50522e;hpb=d2d570d4496d90acb9f03fa10bf58b90ba8a1361;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 2248f44..b7d525e 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -95,6 +95,65 @@ sub component { } } +=item $c->dispatch + +Dispatch request to actions. + +=cut + +sub dispatch { + my $c = shift; + my $action = $c->req->action; + my $namespace = ''; + $namespace = ( join( '/', @{ $c->req->args } ) || '/' ) + if $action eq 'default'; + unless ($namespace) { + if ( my $result = $c->get_action($action) ) { + $namespace = _class2prefix( $result->[0]->[0]->[0] ); + } + } + my $default = $action eq 'default' ? $namespace : undef; + my $results = $c->get_action( $action, $default ); + $namespace ||= '/'; + if ( @{$results} ) { + + # Execute last begin + $c->state(1); + if ( my $begin = @{ $c->get_action( 'begin', $namespace ) }[-1] ) { + $c->execute( @{ $begin->[0] } ); + return if scalar @{ $c->error }; + } + + # Execute the auto chain + for my $auto ( @{ $c->get_action( 'auto', $namespace ) } ) { + $c->execute( @{ $auto->[0] } ); + return if scalar @{ $c->error }; + last unless $c->state; + } + + # Execute the action or last default + if ( ( my $action = $c->req->action ) && $c->state ) { + if ( my $result = @{ $c->get_action( $action, $default ) }[-1] ) { + $c->execute( @{ $result->[0] } ); + } + } + + # Execute last end + if ( my $end = @{ $c->get_action( 'end', $namespace ) }[-1] ) { + $c->execute( @{ $end->[0] } ); + return if scalar @{ $c->error }; + } + } + else { + my $path = $c->req->path; + my $error = $path + ? qq/Unknown resource "$path"/ + : "No default action defined"; + $c->log->error($error) if $c->debug; + $c->error($error); + } +} + =item $c->error =item $c->error($error, ...) @@ -144,10 +203,14 @@ sub execute { else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) } }; if ( my $error = $@ ) { - chomp $error; - $error = qq/Caught exception "$error"/; + + unless ( ref $error ) { + chomp $error; + $error = qq/Caught exception "$error"/; + } + $c->log->error($error); - $c->error($error) if $c->debug; + $c->error($error); $c->state(0); } return $c->state; @@ -350,6 +413,7 @@ sub forward { return 0; } my $caller = caller(0); + my $global = $command =~ /^\// ? 0 : 1; my $namespace = '/'; if ( $command =~ /^\// ) { $command =~ /^(.*)\/(\w+)$/; @@ -357,7 +421,7 @@ sub forward { $command = $2; } else { $namespace = _class2prefix($caller) || '/' } - my $results = $c->get_action( $command, $namespace ); + my $results = $c->get_action( $command, $namespace, $global ); unless ( @{$results} ) { my $class = $command; if ( $class =~ /[^\w\:]/ ) { @@ -376,38 +440,51 @@ sub forward { } } for my $result ( @{$results} ) { - $c->state( $c->execute( @{ $result->[0] } ) ); + $c->execute( @{ $result->[0] } ); + return if scalar @{ $c->error }; + last unless $c->state; } return $c->state; } -=item $c->get_action( $action, $namespace ) +=item $c->get_action( $action, $namespace, $global ) Get an action in a given namespace. =cut sub get_action { - my ( $c, $action, $namespace ) = @_; + my ( $c, $action, $namespace, $global ) = @_; return [] unless $action; $namespace ||= ''; if ($namespace) { - $namespace = '' if $namespace eq '/'; - my $parent = $c->tree; - my @results; - my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; - push @results, [$result] if $result; - my $visitor = Tree::Simple::Visitor::FindByPath->new; - for my $part ( split '/', $namespace ) { - $visitor->setSearchPath($part); - $parent->accept($visitor); - my $child = $visitor->getResult; - my $uid = $child->getUID if $child; - my $match = $c->actions->{private}->{$uid}->{$action} if $uid; - push @results, [$match] if $match; - $parent = $child if $child; + if ($global) { + my @results; + for my $uid ( keys %{ $c->actions->{private} } ) { + if ( my $result = $c->actions->{private}->{$uid}->{$action} ) { + push @results, [$result]; + } + } + return \@results; + } + else { + $namespace = '' if $namespace eq '/'; + my $parent = $c->tree; + my @results; + my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; + push @results, [$result] if $result; + my $visitor = Tree::Simple::Visitor::FindByPath->new; + for my $part ( split '/', $namespace ) { + $visitor->setSearchPath($part); + $parent->accept($visitor); + my $child = $visitor->getResult; + my $uid = $child->getUID if $child; + my $match = $c->actions->{private}->{$uid}->{$action} if $uid; + push @results, [$match] if $match; + $parent = $child if $child; + } + return \@results; } - return \@results; } elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] } elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] } @@ -445,43 +522,7 @@ sub handler { my $handler = sub { my $c = $class->prepare($engine); $c->{stats} = \@stats; - my $action = $c->req->action; - my $namespace = ''; - $namespace = ( join( '/', @{ $c->req->args } ) || '/' ) - if $action eq 'default'; - unless ($namespace) { - if ( my $result = $c->get_action($action) ) { - $namespace = _class2prefix( $result->[0]->[0]->[0] ); - } - } - my $default = $action eq 'default' ? $namespace : undef; - my $results = $c->get_action( $action, $default ); - $namespace ||= '/'; - if ( @{$results} ) { - for my $begin ( @{ $c->get_action( 'begin', $namespace ) } ) { - $c->state( $c->execute( @{ $begin->[0] } ) ); - } - if ( my $action = $c->req->action ) { - for my $result ( - @{ $c->get_action( $action, $default ) }[-1] ) - { - $c->state( $c->execute( @{ $result->[0] } ) ); - last unless $default; - } - } - for my $end ( reverse @{ $c->get_action( 'end', $namespace ) } ) - { - $c->state( $c->execute( @{ $end->[0] } ) ); - } - } - else { - my $path = $c->req->path; - my $error = $path - ? qq/Unknown resource "$path"/ - : "No default action defined"; - $c->log->error($error) if $c->debug; - $c->error($error); - } + $c->dispatch; return $c->finalize; }; if ( $class->debug ) { @@ -767,7 +808,7 @@ sub set_action { $absolute = 1; } $absolute = 1 if $flags{global}; - my $name = $absolute ? $path : "$prefix/$path"; + my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path; $c->actions->{plain}->{$name} = [ $namespace, $code ]; } if ( my $regex = $flags{regex} ) { @@ -862,7 +903,7 @@ sub setup_components { $self->components->{ ref $comp } = $comp; $self->setup_actions($comp); } - my $t = Text::ASCIITable->new({ hide_HeadRow => 1, hide_HeadLine => 1}); + my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); $t->setCols('Class'); $t->setColWidth( 'Class', 75, 1 ); $t->addRow( wrap( $_, 75 ) ) for keys %{ $self->components }; @@ -917,6 +958,10 @@ sub setup_components { if ( @{ $regexes->{tbl_rows} } && $self->debug ); } +=item $c->state + +Contains the return value of the last executed action. + =item $c->stash Returns a hashref containing all your data.