X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FDispatcher.pm;h=66e87210ecc712af9ee79796c1f3a9f43da8269d;hb=5d1266aecbc3b839f0b904093ccf282a73e06c91;hp=96793faf1dbbbd05132179e064e23fa834824885;hpb=2633d7dc3bb9c0cf7bf3e7cf936d6411fe3ba5aa;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 96793fa..66e8721 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -6,9 +6,8 @@ use Catalyst::Exception; use Catalyst::Utils; use Catalyst::Action; use Catalyst::ActionContainer; -use Catalyst::DispatchType::Path; -use Catalyst::DispatchType::Regex; use Catalyst::DispatchType::Default; +use Catalyst::DispatchType::Index; use Text::ASCIITable; use Tree::Simple; use Tree::Simple::Visitor::FindByPath; @@ -16,7 +15,13 @@ use Tree::Simple::Visitor::FindByPath; # Stringify to class use overload '""' => sub { return ref shift }, fallback => 1; -__PACKAGE__->mk_accessors(qw/actions tree dispatch_types/); +__PACKAGE__->mk_accessors(qw/tree dispatch_types/); + +# Preload these action types +our @PRELOAD = qw/Path Regex/; + +# Postload these action types +our @POSTLOAD = qw/Index Default/; =head1 NAME @@ -248,18 +253,32 @@ sub prepare_action { sub get_action { my ( $self, $c, $action, $namespace, $inherit ) = @_; return [] unless $action; - $namespace ||= '/'; - $inherit ||= 0; + $namespace ||= ''; + $namespace = '' if $namespace eq '/'; + $inherit ||= 0; my @match = $self->get_containers($namespace); - my @results; + if ($inherit) { # Return [ [ $act_obj ], ... ] for valid containers + return [ + map { [ $_->{$action} ] } # Make [ $action_obj ] + grep { defined $_->{$action} } # If it exists in the container + map { $_->actions } # Get action hash for container + @match + ]; + } + else { + my $node = $match[-1]->actions; # Only bother looking at the last one - foreach my $child ( $inherit ? @match : $match[-1] ) { - my $node = $child->actions; - push( @results, [ $node->{$action} ] ) if defined $node->{$action}; + if ( defined $node->{$action} + && ( $node->{$action}->prefix eq $namespace ) ) + { + return [ [ $node->{$action} ] ]; + } + else { + return []; + } } - return \@results; } =item $self->get_containers( $namespace ) @@ -320,7 +339,19 @@ sub set_action { # Parse out :Foo(bar) into Foo => bar etc (and arrayify) + my %initialized; + $initialized{ ref $_ }++ for @{ $self->dispatch_types }; + if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+)\s*\))?$/ ) ) { + + # Initialize types + my $class = "Catalyst::DispatchType::$key"; + unless ( $initialized{$class} ) { + eval "require $class"; + push( @{ $self->dispatch_types }, $class->new ) unless $@; + $initialized{$class}++; + } + if ( defined $value ) { ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ ); } @@ -393,18 +424,16 @@ sub set_action { sub setup_actions { my ( $self, $class ) = @_; - # These are the core structures - $self->actions( - { - plain => {}, - private => {}, - regex => {}, - compiled => [] - } - ); + $self->dispatch_types( [] ); - $self->dispatch_types( - [ map { "Catalyst::DispatchType::$_"->new } qw/Path Regex Default/ ] ); + # Preload action types + for my $type (@PRELOAD) { + my $class = "Catalyst::DispatchType::$type"; + eval "require $class"; + Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) + if $@; + push @{ $self->dispatch_types }, $class->new; + } # We use a tree my $container = @@ -433,27 +462,28 @@ sub setup_actions { } for my $namespace ( keys %namespaces ) { - for my $sym ( values %{ $namespace . '::' } ) { - if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { - $name = *{$sym}{NAME}; $class->set_action( $name, $code, $comp, $attrs ); last; } - } - } - } + } + # Postload action types + for my $type (@POSTLOAD) { + my $class = "Catalyst::DispatchType::$type"; + eval "require $class"; + Catalyst::Exception->throw( message => qq/Couldn't load "$class"/ ) + if $@; + push @{ $self->dispatch_types }, $class->new; } return unless $class->debug; - my $actions = $self->actions; my $privates = Text::ASCIITable->new; $privates->setCols( 'Private', 'Class' ); $privates->setColWidth( 'Private', 36, 1 ); @@ -474,34 +504,11 @@ sub setup_actions { }; $walker->( $walker, $self->tree, '' ); - $class->log->debug( "Loaded private actions:\n" . $privates->draw ) + $class->log->debug( "Loaded Private actions:\n" . $privates->draw ) if ( @{ $privates->{tbl_rows} } ); - my $publics = Text::ASCIITable->new; - $publics->setCols( 'Public', 'Private' ); - $publics->setColWidth( 'Public', 36, 1 ); - $publics->setColWidth( 'Private', 37, 1 ); - - for my $plain ( sort keys %{ $actions->{plain} } ) { - my $action = $actions->{plain}->{$plain}; - $publics->addRow( "/$plain", "/$action" ); - } - - $class->log->debug( "Loaded public actions:\n" . $publics->draw ) - if ( @{ $publics->{tbl_rows} } ); - - my $regexes = Text::ASCIITable->new; - $regexes->setCols( 'Regex', 'Private' ); - $regexes->setColWidth( 'Regex', 36, 1 ); - $regexes->setColWidth( 'Private', 37, 1 ); - - for my $regex ( sort keys %{ $actions->{regex} } ) { - my $action = $actions->{regex}->{$regex}; - $regexes->addRow( $regex, "/$action" ); - } - - $class->log->debug( "Loaded regex actions:\n" . $regexes->draw ) - if ( @{ $regexes->{tbl_rows} } ); + # List all public actions + $_->list($class) for @{ $self->dispatch_types }; } =back